1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments,...
-
Upload
bernadette-gray -
Category
Documents
-
view
225 -
download
0
Transcript of 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments,...
1
Arithmetic Expressions and Math Class
Work with and modify variables, assignments , increment/decrement, Casting and modulus
Math API
2
GOALS...
This lecture will teach us how to modify primitive datatypes using simple math operations. We will also examine the math API in Java (java.lang.Math).
3
Arithmetic Operators & Assignments:
+ - * / %
Order of operations is determined by ( ) and by the rank of the operators.
Mult and division are performed 1st (LEFT TO RIGHT)
4
Followed by addition and subtraction
ORDER: ( ) evaluate from the inside out
*, /, % evaluate LEFT to RIGHT
5
+, - evaluate LEFT to RIGHT
The minus can also be used for negation
Example:x = -(y + 2 * z) / 5;a = -a;
6
Examples:
avg = (grade1 + grade2 + grade3 + grade4 + grade5) / numGrades;
7
INTEGER MATH a = 25 b = 10 c = 20 d = 30 e = 0
int e = 0; // com error if not init
System.out.println(a + b / c * d);
ANS ?
8
System.out.println(a + b / c * d);
ANS 25 b/c = 0 0 * 30 = 0 + a = 25
9
System.out.println((a + b) / c + d);
ANS ?
10
System.out.println((a + b) / c + d);
ANS 31 a+b=35 35/20 = 1 Truncates 1.75 1+30=31
11
System.out.println(a * (c / b) * d);
ANS ?
12
System.out.println(a * (c / b) * d);
ANS 1500 c/b=2 a*2 = 50 50*30=1500
13
System.out.println((a + b) / (c * d));
ANS ?
14
System.out.println((a + b) / (c * d));
ANS 0 a+b=35 c*d=600 35/600 = 0 Tr .058
15
System.out.println(e + a);
System.out.println(e + A);
16
System.out.println(e + a);
ANS 25
System.out.println(e + A);
UNDEFINED VAR A – compile error
17
DOUBLE MATH a = 25.0 b = 10.0 c = 20.0 d = 30.0 e = .0
double e = .0;
System.out.println(a + b / c * d);
System.out.println((a + b) / c + d);
System.out.println(a * (c / b) * d);
18
DOUBLE MATH a = 25.0 b = 10.0 c = 20.0 d = 30.0 e = .0
double e = .0; compile error if not initialized
System.out.println(a + b / c * d); 40.0
System.out.println((a + b) / c + d); 31.75
System.out.println(a * (c / b) * d); 1500.0
19
DOUBLE MATH a = 25.0 b = 10.0 c = 20.0 d = 30.0 e = .0
System.out.println((a + b) / (c * d));
System.out.println(e + a);
System.out.println(e + A);
20
DOUBLE MATH a = 25.0 b = 10.0 c = 20.0 d = 30.0 e = .0
System.out.println((a + b) / (c * d)); 0.0583
System.out.println(e + a); 25.0
// System.out.println(e + A); UNDEFINED VAR A – compile error
21
addition is performed first because ( ) are executed first, addition is done L to R
division is performed second
the result is Assigned to the LVALUE variable avg
22
modulus % --- remainder (or remainder of partial units of b given a)int a = 7, b = 22;
a % b = 7b % a = 1
modulus is used to determine if a number is evenly divisible by another
23
if (b % 2 == 0) is true if b is an EVEN number
24
Other Examples:
z = p * r % q + w / x - y; What is the order ???3 + 5 * 3 = ?-3 + 5 * 3 = ?3 + 5 * -3 = ?3 + 5 % 3 = ?(3 + 5) % 3 = ?
25
Other Examples:
z = p * r % q + w / x - y; 6 1 2 4 3 53 + 5 * 3 = 18-3 + 5 * 3 = 123 + 5 * -3 = -123 + 5 % 3 = 5(3 + 5) % 3 = 2
26
Type Casting:
Java allows variables of different types to be used in an expression
The type of the result depends on the type of the operands NOT THEIR VALUES
27
Operands of the same type results in an answer of that same type
(int + int = int)
Example:Int a = 7, b = 2;System.out.println(a / b); The result is 3 and NOT 3.5
28
If you have integers but wish a NON INTEGER result of an expression use type casting
You can temporarily cast an integer into a double, for example
29
Example:int a = 7, b = 2;double ratio;ratio = (double)a / (double)b;system.out.println(ratio); The result is 3.5
30
However, the following code STILL produces 3 and NOT 3.5ratio = a / b;
as the RVALUE is performed as an integer !!!
If you have 2 variables of DIFFERENT types, the smaller type is “promoted” to that of the larger one
31
Example:ratio = (double)a / b;ratio = a / (double)b;The results will be 3.5
However, ratio = (double) (a / b); Results in 3 and NOT 3.5 as a/b is still performed as an integer
32
You may also cast from a larger to a smaller data type, from a double to an integer
Example:Int ptsOnDie = 1 + (int)(Math.random() * 6);// 0.0 <= Mah.random() < 1.0
33
Here, the (int) cast truncates the number in the direction of 0
(int) 1.99 = 1
(int) –1.99 = -1
34
You can ROUND a double value to the nearest integer by adding or subtracting .5 AND THEN cast that result into an integer
Example:int value = (int) ((double)count / totalCount * 360 + .5);
35
Compound Assignment Operators:
Instead of coding the following:a = a + b;a = a – b;a = a * b;a = a / b;a = a % b;
36
Compound Assignment Operators:
You can use compound assignments:
a += b;a -= b;a *= b;a /= b;a %= b;
37
Compound Assignment Operators:
The compound method enforces the concept that the RVALUE is being processed in a temporary workspace and that result is the ASSIGNED to the RVALUE variable
38
Increment & Decrement:
Shorthand for incrementing or decrementing a variable by 1
Instead of the following:a = a + 1;a = a - 1;
39
You can write:
a++;a--;++a;--a;
40
There is an important difference between the two types of increment and decrement when they are used in expressions
41
Increment, a++, increments the variable, a, AFTER it has been used in an expression
Increment, ++a, increments the variable, a, BEFORE, it is used in an expression
42
Math Class:
Java.lang.Math
Inherited from java.lang.Object (all java classes originate from
java.lang.Object)
43
Methods Include:absolute valuesincoslogmaxminpow
44
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
// LOGanswer = Math.log(a);
System.out.println("Log " + answer);
45
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
// Square Rootanswer = Math.sqrt(a);
System.out.println("Sqr " + answer);
46
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
// Poweranswer = Math.pow(10, 2);
System.out.println("Pwr " + answer);
47
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
//Sinanswer = Math.sin(90);
System.out.println("Sin " + answer);
48
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
//absolute valueanswer = Math.abs(-67);
System.out.println("Abs " + answer);
49
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
//minanswer = Math.min(b, c);
System.out.println("Min " + answer);
50
Examples:double a = 81, answer = 0.0;int b = 50, c = 67;
//maxanswer = Math.max(b, c);
System.out.println("Max " + answer);
51
Results:
Log 4.394449154672439Sqr 9.0Pwr 100.0Sin 0.8939966636005579Abs 67.0Min 50.0Max 67.0
52
Projects:MathFahrenheit to/from Celsius:Jogging DistanceSquare Meters to MowPathageorin theorumPopulation GrowthBMIDog years to human yearsPound to/from kilosIncome tax calculator
53
TEST IS THE DAY AFTER THE PROJECT IS
DUE !!!