Download presentation
Presentation is loading. Please wait.
Published byBrett Palmer Modified over 9 years ago
1
Operator precedence
2
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 before operators with low priority –operators with equal priority are evaluated left to right Operator priority (highest to lowest) 1. ( ) 2. * / % 3. + - 4. =
3
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
4
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)
5
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)
6
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
7
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 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 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
8
Arrays
9
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
10
Accessing array elements Example: an array A of integers 3 is the element of array A at index 1 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 5 -7 0 3 2 3 0 1 2 3 4 5 array elements array indices A =
11
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];
12
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
13
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; }
14
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 is your 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 is your subroutine should return the array 3.52.42.01.993.1 1.992.02.43.5 2.42.01.993.1
15
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 is your subroutine should return 3 -3.16.5-0.72.92.3
16
Recursion
17
Triangular numbers Write a recursive Java subroutine to compute the n th triangular number Base case –the first triangular number is 1 Recursive case –the n th 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
18
Recursive solution int TriNumber( int n ) { if( n==1 ) return 1; else return n + TriNumber( n-1 ); }
19
Square numbers Write a recursive Java subroutine to compute the n th square number Base case –the first square number is 1 Recursive case –the n th square number is equal to 2n-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
20
Recursive solution int SquareNumber( int n ) { if( n==1 ) return 1; else return 2*n-1 + SquareNumber( n-1 ); }
21
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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.