Operator precedence Operator precedence Evaluate a b c
Operator precedence
Operator precedence ® ® Evaluate a + b * c – multiplication first? – addition first? a + (b * c) (a + b) * c Java solves this problem by assigning priorities to operators (operator precedence) Operator priority – operators with high priority (highest to lowest) are evaluated before operators with low priority 1. ( ) – operators with equal priority 2. * / % are evaluated left to right 3. + 4. =
When in doubt, use parentheses a + b * c = a + (b * c) – because * has higher priority than + ® To first perform the + operation we need to use parentheses – (a + b) * c ® If in any doubt use extra parentheses to ensure the correct order of evaluation – parentheses are free! – cause no extra work for the computer when the program is executing – only make it easier for you to work out what is happening ®
Examples Java adheres to traditional order of operations ® * and / have higher priority than + and – int x = 3 + 5 * 6; (x = 33) int y = (3 + 5) * 6; (y = 48) ® Parentheses are free, use them liberally int z = ((3 + 5) * (6)); (z = 48) ® Equal priority operations are evaluated left-to-right in the absence of parentheses int w = 3 * 4 / 2 * 6; (w = 36) int x = 3 * 4 / (2 * 6); (x = 1) int y = 3 * 4 + 2 * 6; (y = 24) int z = 3 * (4 + 2) * 6; (z = 108) ®
Syntax and semantics Addition, subtraction: + and –, int and double int x = 21+4; (x = 25) double y = 14. 1 -2; (y = 12. 1) ® Multiplication: *, int and double int x = 21*4; (x = 84) double y = 14. 1*2. 5; (y = 35. 25) ® Division: /, different for int and double int x = 21/4; (x = 5) double y = 21/4; (y = 5. 0) double y = 21/4. 0; (y = 5. 25) ® Modulus: %, only for int x = 21%4; (x = 1) ®
Automatic type conversion Mixed type expressions are converted to a “higher” compatible type as part of the computation process ® Rules: – if both operands are of type int then the result is of type int – if either operand, or both, are of type double then the result is of type double ® Cannot convert to a “lower” type ® Example: ® Convert Fahrenheit to Celsius Given the following Java statements: double F = 41. 0; double C = (F-32. 0)*(5/9); Question: What is the value of C? a. 5 b. 0. 0 c. 5. 0 d. 0. 5 e. error
More expressions int g = 12 + 2. 5; What is the value of g? a. 0 b. 12 c. 14 d. 14. 5 e. error int n = 1 – 2 * 3 – (4 + 5); What is the value of n? a. -4 b. -2 c. 2 d. 4 e. none of the above int x = 8 * (7 – 6 + 5) % (4 + 3 / 2) – 1; What is the value of x? a. -1 b. 0 c. 2 d. 3 e. none of the above
Arrays
Arrays Aggregate data type – collection of elements stored in one unit ® Arrays store collections of identically typed elements – not a primitive data type (but can store them) – behavior is similar to objects ® Analogies – mailboxes in post office – CD racks with slots ® Advantages – Simplifies naming – Allows use of loops – Holds multiple elements ®
Accessing array elements ® Example: an array A of integers A= 5 3 2 3 0 -7 0 1 2 3 4 5 3 is the element of array A at index 1 ® Access: ® int int w x y z = = A[0] ; A[3] ; A[2]+A[5]; A[10]; (w = 5) (x = 3) (y = -5) error array elements array indices
Declaring arrays Declare an array of 5 integers int[] A = new int[5]; ® Size must be known int[] A = new int[n]; – error if n has not been assigned a value! ® Size must be integer value double n = 5; int[] A = new int[n]; – error: n is not of type int ! ® Correct: int n = 5; int[] A = new int[n]; ®
Assigning array values Declare an array of 5 integers int[] A = new int[5]; ® Assign values to array elements A[0] = 2; A[1] = 4; A[2] = -9; A[3] = 0; A[4] = 2; ® Variable initializer syntax int[] A = {2, 4, -9, 0, 2}; – use when you know what values you want to initially store in the array ®
Using arrays ® Subroutine sum. Array computes the sum of the elements in an array – size of an array (number of elements) is given by the length property double sum. Array(double[] A) { double sum = 0. 0; for(int k = 0; k < A. length; k++) sum = sum + A[k]; return sum; }
Exercises ® Write a Java subroutine min. Array that takes an array of doubles as input and returns the minimum value in the array – for example, if the input array is 3. 5 2. 4 2. 0 1. 99 3. 1 your subroutine should return 1. 99 ® Write a Java subroutine reverse. Array that takes an array of ints as input and returns the array of ints in reverse order – for example, if the input array is 3. 5 2. 4 2. 0 1. 99 3. 1 your subroutine should return the array 3. 1 1. 99 2. 0 2. 4 3. 5
Exercises ® Write a Java subroutine num. Greater that takes as input a double d an array A of doubles and returns the number of elements in A greater than d – the declaration of your subroutine should be int num. Greater(double d, double[] A) { /* you do this part! */ } – example: if d=1 and the input array is -3. 1 6. 5 -0. 7 2. 9 your subroutine should return 3 2. 3
Recursion
Triangular numbers Write a recursive Java subroutine to compute the nth triangular number ® Base case – the first triangular number is 1 ® Recursive case – the nth triangular number is equal to n + the (n-1)st triangular number ® The 4 th triangular number is 6+4 = 10 The 5 th triangular number is 10+5 = 15 The 6 th triangular number is 15+6 = 21
Recursive solution int Tri. Number( int n ) { if( n==1 ) return 1; else return n + Tri. Number( n-1 ); }
Square numbers Write a recursive Java subroutine to compute the nth square number ® Base case – the first square number is 1 ® Recursive case – the nth square number is equal to 2 n-1 + the (n-1)st square number ® The 4 th square number is 9+2*4 -1 = 16 The 5 th square number is 16+2*5 -1 = 25 The 6 th square number is 25+2*6 -1 = 36
Recursive solution int Square. Number( int n ) { if( n==1 ) return 1; else return 2*n-1 + Square. Number( n-1 ); }
Exercise The following statements call the subroutine given below x = something(2, 7); y = something(8, 2); ® What is the value of x? What is the value of y? ® int something( int a, int b ) { if( a < b ) return a+b; else return 1 + something( a-b, b); }
- Slides: 21