210 likes | 263 Views
Operator precedence. Operator precedence. Evaluate a + b * c multiplication first? a + (b * c) addition first? ( a + b) * c Java solves this problem by assigning priorities to operators ( operator precedence )
E N D
Operator precedence • Evaluate a + b * c • multiplication first? a + (b * c) • addition first? (a + b) * c • Java solves this problem by assigning priorities to operators (operator precedence) • operators with high priority are evaluated beforeoperators with low priority • operators with equal priority are evaluated left to right Operator priority (highest to lowest) • ( ) • * / % • + - • =
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 int x = 21%4; (x = 1)
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? 5 0.0 5.0 0.5 error Automatic type conversion
int g = 12 + 2.5; What is the value of g? 0 12 14 14.5 error int x = 8 * (7 – 6 + 5) % (4 + 3 / 2) – 1; What is the value of x? -1 0 2 3 none of the above More expressions int n = 1 – 2 * 3 – (4 + 5); What is the value of n? -4 -2 2 4 none of the above
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
arrayelements A = arrayindices -7 3 0 3 2 5 0 1 2 3 4 5 Accessing array elements • Example: an array A of integers • 3 is the element of array A at index1 • Access: int w = A[0] ; (w = 5) int x = A[3] ; (x = 3) int y = A[2]+A[5]; (y = -5) int z = A[10]; error
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 sumArray computes the sum of the elements in an array • size of an array (number of elements) is given by the length property double sumArray(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 minArray that takes an array of doubles as input and returns the minimum value in the array • for example, if the input array isyour subroutine should return 1.99 • Write a Java subroutine reverseArray that takes an array of ints as input and returns the array of ints in reverse order • for example, if the input array isyour subroutine should return the array
Exercises • Write a Java subroutine numGreater that takes as input a double d and an array A of doubles and returns the number of elements in A greater than d • the declaration of your subroutine should be int numGreater(double d, double[] A) { /* you do this part! */ } • example: if d=1 and the input array isyour subroutine should return 3
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 4th triangular number is 6+4 = 10 The 5th triangular number is 10+5 = 15 The 6th triangular number is 15+6 = 21
Recursive solution int TriNumber( int n ) { if( n==1 ) return 1; else return n + TriNumber( 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 2n-1 + the (n-1)st square number The 4th square number is 9+2*4-1 = 16 The 5th square number is 16+2*5-1 = 25 The 6th square number is 25+2*6-1 = 36
Recursive solution int SquareNumber( int n ) { if( n==1 ) return 1; else return 2*n-1 + SquareNumber( 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); }