Date post: | 06-Jan-2018 |
Category: |
Documents |
Upload: | brett-palmer |
View: | 220 times |
Download: | 0 times |
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)
– 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. =
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 liberallyint z = ((3 + 5) * (6)); (z = 48)
Equal priority operations are evaluated left-to-right in the absence of parenthesesint 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 doubleint 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 intint 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. 5b. 0.0c. 5.0d. 0.5e. error
More expressions
int g = 12 + 2.5;What is the value of g?a. 0b. 12c. 14d. 14.5e. error
int x = 8 * (7 – 6 + 5) % (4 + 3 / 2) – 1;What is the value of x?a. -1b. 0c. 2d. 3e. none of the above
int n = 1 – 2 * 3 – (4 + 5); What is the value of n?a. -4b. -2c. 2d. 4e. 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
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 -703230 1 2 3 4 5
arrayelementsarrayindices
A =
Declaring arrays
Declare an array of 5 integersint[] A = new int[5];
Size must be knownint[] A = new int[n];– error if n has not been assigned a value!
Size must be integer valuedouble 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 integersint[] A = new int[5];
Assign values to array elementsA[0] = 2;A[1] = 4;A[2] = -9;A[3] = 0;A[4] = 2;
Variable initializer syntaxint[] 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 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.5 2.4 2.0 1.99 3.1
3.1 1.99 2.0 2.4 3.5
3.5 2.4 2.0 1.99 3.1
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.1 6.5 -0.7 2.9 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 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;
elsereturn 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 numberis 16+2*5-1 = 25
The 6th square numberis 25+2*6-1 = 36
Recursive solution
int SquareNumber( int n ){
if( n==1 )return 1;
elsereturn 2*n-1 + SquareNumber( n-1 );
}
Exercise
The following statements call the subroutine given belowx = 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;elsereturn 1 + something( a-b, b);
}