{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# lecture4 - Administrative Issues Expressions Objects and...

This preview shows pages 1–4. Sign up to view the full content.

1 Expressions, Objects, and Class Methods Lecture 4 Administrative Issues Due Today – Homework 0 New Assignments – Program 1 Assigned First real programming assignment WILL BE TIME CONSUMING! WORK OVER THE LONG WEEKEND! From Last Time… Types, Variables, Assignment • Example: float testAvg; testAvg = 98.43; Identifier Expression Assignment = ; Expressions are much more than just literals! Why Expressions? How do we find the test average? • Example: int test1, test2, test3; float testAvg; test1 = 95; test2 = 84; test3 = 90; testAvg = WHAT GOES HERE? Expressions Expression Combination of one or more operands and operators • Operands Data to be operated on Constants, Variables, Literals, Objects, … • Operators The operation to be performed Addition, Subtraction, Division, … Common Arithmetic Operators 10 % 4 = 2 12 / 4 = 3 3 * 4 = 12 10 – 5 = 5 17 + 2 = 19 Numeric Example (Not Java…) Java Symbol Operation % Remainder / Division * Multiplication - Subtraction + Addition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Operands and Data Types Type of operands determines type of expression result Integer Both Integers Real Mix of Reals/Integers Real Both Reals Result of Expression Type Operand Types Example Expressions 1.33 4.0 / 3.0 1 4 / 3 1.33 4 / 3.0 12.0 4.0 * 3.0 1.0 4.0 – 3 7 4 + 3 Result Expression Longer Expressions Combine multiple operations to form long expressions int test1, test2, test3, sum; float testAvg; test1 = 95; test2 = 84; test3 = 90; sum = test1 + test2 + test3; testAvg = sum / 3.0; Order of Operations int num1; num1 = 10 + 10 / 2; What is the value of num1 ? If addition is first, 10 If division first, 15 Order of Operations Each operator has a precedence level Operations performed in order of level – Like algebra, * before + On same level, operations performed left-to-right Parenthesis used to change order – Example: (5+5)*2 5+5*2 Java’s Precedence Levels 4 3 2 1 Precedence Level = + - + * / % + - Operator Assignment Addition Subtraction String Concatenation Multiplication Division Remainder Unary Plus (Positive) Unary Minus (Neg) Operation
3 Test Average int test1, test2, test3; float testAvg; test1 = 95; test2 = 84; test3 = 90; testAvg = (test1+test2+test3)/3.0; 2 Minute Exercise What value is returned from each expression? 5+7/2 (5+7)/2 (5+7)/2.0 5.0+7/2 5.0+7.0/2.0 (5+4+3+2+1)/2 6%5+4%3+2%1 Back to Data Types Recall, not all integers are the same byte , short , int , long Not all real numbers are the same float , double Difference is number of bytes used to store data More bytes more possible values Data Type Conversion Values can be converted from one type to another Three ways Assignment Conversion Arithmetic Promotion – Casting Assignment Conversion Occurs when a value of one type is assigned to a variable of another • Example: float

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}