Arithmetic Expressions and Math Class

advertisement
Arithmetic Expressions
and Math Class
Work with and modify variables,
assignments ,
increment/decrement,
Casting and modulus
Math API
1
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).
2
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)
3
Followed by addition and
subtraction
ORDER:( ) evaluate from
the inside out
*, /, % evaluate LEFT to
RIGHT
4
+, - evaluate LEFT to RIGHT
The minus can also be used for
negation
Example:
x = -(y + 2 * z) / 5;
a = -a;
5
Examples:
avg = (grade1 + grade2 + grade3 +
grade4 + grade5) / numGrades;
6
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 ?
7
System.out.println(a + b / c * d);
ANS 25
b/c = 0 0 * 30 = 0 + a = 25
8
System.out.println((a + b) / c
+ d);
ANS ?
9
System.out.println((a + b) / c +
d);
ANS 31
a+b=35
35/20 = 1 Truncates 1.75
1+30=31
10
System.out.println(a * (c / b) *
d);
ANS ?
11
System.out.println(a * (c / b) *
d);
ANS 1500
c/b=2 a*2 = 50 50*30=1500
12
System.out.println((a + b) / (c *
d));
ANS ?
13
System.out.println((a + b) / (c *
d));
ANS 0
a+b=35 c*d=600 35/600 = 0
Tr .058
14
System.out.println(e + a);
System.out.println(e + A);
15
System.out.println(e + a);
ANS 25
System.out.println(e + A);
UNDEFINED VAR A – compile error
16
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);
17
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
18
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);
19
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
20
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
21
modulus % --- remainder (or
remainder of partial units of b
given a)
int a = 7, b = 22;
a%b=7
b%a=1
modulus is used to determine if a
number is evenly divisible by
another
22
if (b % 2 == 0)
is true if b is an EVEN number
23
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 = ?
24
Other Examples:
z = p * r % q + w / x - y;
6
1
2
4
3
5
3 + 5 * 3 = 18
-3 + 5 * 3 = 12
3 + 5 * -3 = -12
3+5%3 =5
(3 + 5) % 3 = 2
25
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
26
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
27
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
28
Example:
int a = 7, b = 2;
double ratio;
ratio = (double)a / (double)b;
system.out.println(ratio);
The result is 3.5
29
However, the following code STILL
produces 3 and NOT 3.5
ratio = 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
30
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
31
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
32
Here, the (int) cast truncates the
number in the direction of 0
(int) 1.99 = 1
(int) –1.99 = -1
33
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);
34
Compound Assignment Operators:
Instead of coding the following:
a = a + b;
a = a – b;
a = a * b;
a = a / b;
a = a % b;
35
Compound Assignment Operators:
You can use compound
assignments:
a += b;
a -= b;
a *= b;
a /= b;
a %= b;
36
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
37
Increment & Decrement:
Shorthand for incrementing or
decrementing a variable by 1
Instead of the following:
a = a + 1;
a = a - 1;
38
You can write:
a++;
a--;
++a;
--a;
39
There is an important difference
between the two types of
increment and decrement when
they are used in expressions
40
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
41
Math Class:
Java.lang.Math
Inherited from java.lang.Object
(all java classes originate from
java.lang.Object)
42
Methods Include:
absolute value
sin
cos
log
max
min
pow
43
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
// LOG
answer = Math.log(a);
System.out.println("Log " +
answer);
44
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
// Square Root
answer = Math.sqrt(a);
System.out.println("Sqr " + answer);
45
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
// Power
answer = Math.pow(10, 2);
System.out.println("Pwr " +
answer);
46
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
//Sin
answer = Math.sin(90);
System.out.println("Sin " + answer);
47
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
//absolute value
answer = Math.abs(-67);
System.out.println("Abs " +
answer);
48
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
//min
answer = Math.min(b, c);
System.out.println("Min " +
answer);
49
Examples:
double a = 81, answer = 0.0;
int b = 50, c = 67;
//max
answer = Math.max(b, c);
System.out.println("Max " +
answer);
50
Results:
Log 4.394449154672439
Sqr 9.0
Pwr 100.0
Sin 0.8939966636005579
Abs 67.0
Min 50.0
Max 67.0
51
Projects:
Math
Fahrenheit to/from Celsius:
Jogging Distance
Square Meters to Mow
Pathageorin theorum
Population Growth
BMI
Dog years to human years
Pound to/from kilos
Income tax calculator
52
TEST IS THE DAY
AFTER THE
PROJECT IS DUE
!!!
53
Download