Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00 Lecture 4 More on Java Data Types, Control Structures 1
Java Arithmetic Operators Table in precedence order, highest precedence at top Operators ++ -- + (unary) - (unary) * / % + - Meaning increment decrement unary + ( x = +a) unary ( x = -a) multiplication division modulo addition subtraction Associativity Right to left Left to right Left to right Precedence, Associativity Operator precedence is in the order of the previous table Operators in same row have equal precedence Associativity determines order in which operators of equal precedence are applied Parentheses override order of precedence 2
Precedence, Associativity, p.2 Operator precedence is in the order of the previous table Operators in same row have equal precedence Associativity determines order in which operators of equal precedence are applied Parentheses override order of precedence Operator Exercises What is the value of int n: 3
Operator Exercises What is the value of int n: Mixed Arithmetic Promotion: When two operands have different types, Java converts the low capacity to the high capacity type Casting: When you wish to convert a high capacity type to a low capacity type, you must indicate that explicitly Binary operators ( +, -, *, /): If either operand is (double, float, long), other will be converted to (double, float, long) Otherwise both are converted to int 4
Mixed Arithmetic Example Integer Arithmetic Properties Overflows occur from: Division by zero, including 0/0 (undefined) Programmer has responsibility to check and prevent this Java will warn you (by throwing an exception) if it can t do an integer arithmetic operation (discussed later) Accumulating results that exceed the capacity of the integer type being used Programmer has responsibility to check and prevent, as in zero divides No warning is given by Java in this case 5
Integer Overflow Example It s necessary to analyze the range of your results, under worst case circumstances. You often use a long to hold sums of ints, etc. Floating Point Properties Anomalous floating point values: Undefined, such as 0.0/0.0: produces result (Not a Number) Any operation involving produces as result Two values cannot be equal Check if number is by using methods: or Return boolean which is true if argument is Overflow, such as 1.0/0.0: produces result produces result Same rules, results as for Underflow, when result is smaller than smallest possible number we can represent Complex, not handled very well (represented as zero) Rounding errors see following examples 6
Example Float Rounding Program Occurs with doubles too; it s virtually the same, because 1.0 is more precise as a double 7
Doubles as Bad Loop Counters Notice accumulating, increasing error. Never use floats or doubles as loop counters Numerical Problems Problem Zero divide (overflow) 0/0 Overflow Underflow Rounding, accumulation errors Integer Exception thrown. Program crashes unless caught. Exception thrown. Program crashes unless caught No warning. Program gives wrong results. Not possible Not possible Float, double POSITIVE_INFINITY, NEGATIVE_INFINITY NaN (not a number) POSITIVE_INFINITY, NEGATIVE_INFINITY No warning, set to 0 No warning. Program gives wrong results. Common, bad news cases 8
More on Control Structures Three control structures Sequence: execute next statement This is default behavior Branching: if, else statements If, else are the primary construct used Switch statement used if many choices Iteration: while, do, for loops Additional constructs exist to terminate loops prematurely Switch statement Used as substitute for long if-else chains Branch condition must be integer, can t be String, float, etc. No ranges, just single values or expressions in switch C# allows strings as branch condition, but not Java or C++ 9
Terminating Iteration: Break Break statement in for, while or do-while loops transfers control to statement immediately after end of loop Terminating Iteration: Continue Continue statement jumps to end of loop but continues looping 10
Iteration Exercise Recall the definition of a factorial: n! = n * (n-1) * (n-2) * * 1 For example: 4! = 4 * 3 * 2 * 1 = 24 A factorial has the following properties: 0! = 1 n is a positive integer Write a main() that calculates the value of n! for a given n. Use, for example: Calculating the result Input n n >? No Yes fact *= n Print fact? Done 11
Sentinel Controlled Loop (Optional) Suppose that the user doesn t want the program to run just once. Instead he wants to be prompted again to enter a number and, when done, prompted to enter another number. In order to do that, a sentinel controlled loop will be used. The idea of a sentinel controlled loop is that there is a special value (the "sentinel") that is used to say when the loop is done. In this example, the user will enter -1 to tell the program to end. Assume the user enters the number via a JOptionPane. If you re writing the code by hand, don t worry about the exact syntax of JOptionPane; just assume the user enters a valid number. Revise your program. 12