Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 05: Arithmetic Expressions Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Previously: IDE, class, methods, variables

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 Previously…

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Comments

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Comments • They should be included to explain the purpose of the program and describe processing steps • They do not affect how a program works • Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment several lines */

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Comments public class MyProgram { } public static void main (String[] args) { } /** * comments about the class * */ !"#$%&' (%&) /** * comments about the class */

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Comments public class MyProgram { //comments } // comments about the class *+,--'(%&) // Comments everywhere public static void main (String[] args) { } // here also // Comments could be here too

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Test Yourselves • What is a variable? • What is a data type? • What is a variable declaration?

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Data Types The difference between the various numeric primitive types is their size, and therefore the values they can store: Type Size Min Value Max Value byte 8 bits -128 127 short 16 bits -32,768 32,767 int 32 bits -2^31 2^31 - 1 long 64 bits -2^63 2^63 - 1 float 32 bits +/- 3.4 x 1038 with 7 significant digits double 64 bits +/- 1.7 x 10308 with 15 significant digits

Slide 10

Slide 10 text

Expressions

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Summary An expression is a combination of one or more operands and their operators

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Summary class global variables methods statements local variables instructions expressions arithmetic expression relational expression logical expression An expression is a combination of one or more operands and their operators

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Expressions • Arithmetic expressions compute numeric results and make use of the arithmetic operators: o Addition + o Subtraction – o Minus Unary – o Multiplication * o Division / o Remainder %

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Integer Division and Remainder • Dividend / Divisor = Quotient and Remainder • Dividend = (Divisor x Quotient) + Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals 4 14 % 3 equals 2 8 / 12 equals 0 8 % 12 equals 8

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Test Yourselves • 17 % 4 = • -20 % 3 = • 10 % 5 = • 3 % 8 =

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 Operator Precedence • Operators have a well-defined precedence which determines the order in which they are evaluated • Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Arithmetic operators with the same precedence are evaluated from left to right • Parentheses can be used to force the evaluation order

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Operator Precedence

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 String Concatenation • The plus operator (+) is used to concatenate (append) strings: “Hello” + “ World” • To break a string into two parts in two lines, we need to close with a double quote and use + sign to concatenate (append): System.out.println(“ASU is “ + “in Arizona”);

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 + operator • If two operands of + are numbers, it performs an arithmetic addition. • If at least one of operands of + is a string, it performs a string concatenation. • Examples: o 2 + 3 will be 5 o 2 + “ apples” will be “2 apples” o “number ” + 5 will be “number 5” o “we have “ + 2 + 3 will be “we have 23” o “we have “ + (2 + 3) will be “we have 5”

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Example

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21 Example

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22 One more thing

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 23 One more thing

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 24 Homework Read Chapter 2

Slide 25

Slide 25 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2022 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.