1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments,...

53
1 Arithmetic Expressions and Math Class Work with and modify variables, assignments , increment/decrement, Casting and modulus Math API

Transcript of 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments,...

Page 1: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

1

Arithmetic Expressions and Math Class

Work with and modify variables, assignments , increment/decrement, Casting and modulus

Math API

Page 2: 1 Arithmetic Expressions and Math Class zWork 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).

Page 3: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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)

Page 4: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

4

Followed by addition and subtraction

ORDER: ( ) evaluate from the inside out

*, /, % evaluate LEFT to RIGHT

Page 5: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

5

+, - evaluate LEFT to RIGHT

The minus can also be used for negation

Example:x = -(y + 2 * z) / 5;a = -a;

Page 6: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

6

Examples:

avg = (grade1 + grade2 + grade3 + grade4 + grade5) / numGrades;

Page 7: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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 ?

Page 8: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

8

System.out.println(a + b / c * d);

ANS 25 b/c = 0 0 * 30 = 0 + a = 25

Page 9: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

9

System.out.println((a + b) / c + d);

ANS ?

Page 10: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

10

System.out.println((a + b) / c + d);

ANS 31 a+b=35 35/20 = 1 Truncates 1.75 1+30=31

Page 11: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

11

System.out.println(a * (c / b) * d);

ANS ?

Page 12: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

12

System.out.println(a * (c / b) * d);

ANS 1500 c/b=2 a*2 = 50 50*30=1500

Page 13: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

13

System.out.println((a + b) / (c * d));

ANS ?

Page 14: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

14

System.out.println((a + b) / (c * d));

ANS 0 a+b=35 c*d=600 35/600 = 0 Tr .058

Page 15: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

15

System.out.println(e + a);

System.out.println(e + A);

Page 16: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

16

System.out.println(e + a);

ANS 25

System.out.println(e + A);

UNDEFINED VAR A – compile error

Page 17: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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);

Page 18: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 19: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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);

Page 20: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 21: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 22: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 23: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

23

if (b % 2 == 0) is true if b is an EVEN number

Page 24: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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 = ?

Page 25: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 26: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 27: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 28: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 29: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

29

Example:int a = 7, b = 2;double ratio;ratio = (double)a / (double)b;system.out.println(ratio); The result is 3.5

Page 30: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 31: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 32: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 33: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

33

Here, the (int) cast truncates the number in the direction of 0

(int) 1.99 = 1

(int) –1.99 = -1

Page 34: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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);

Page 35: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

35

Compound Assignment Operators:

Instead of coding the following:a = a + b;a = a – b;a = a * b;a = a / b;a = a % b;

Page 36: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

36

Compound Assignment Operators:

You can use compound assignments:

a += b;a -= b;a *= b;a /= b;a %= b;

Page 37: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 38: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

38

Increment & Decrement:

Shorthand for incrementing or decrementing a variable by 1

Instead of the following:a = a + 1;a = a - 1;

Page 39: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

39

You can write:

a++;a--;++a;--a;

Page 40: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

40

There is an important difference between the two types of increment and decrement when they are used in expressions

Page 41: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

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

Page 42: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

42

Math Class:

Java.lang.Math

Inherited from java.lang.Object (all java classes originate from

java.lang.Object)

Page 43: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

43

Methods Include:absolute valuesincoslogmaxminpow

Page 44: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

44

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

// LOGanswer = Math.log(a);

System.out.println("Log " + answer);

Page 45: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

45

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

// Square Rootanswer = Math.sqrt(a);

System.out.println("Sqr " + answer);

Page 46: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

46

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

// Poweranswer = Math.pow(10, 2);

System.out.println("Pwr " + answer);

Page 47: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

47

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

//Sinanswer = Math.sin(90);

System.out.println("Sin " + answer);

Page 48: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

48

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

//absolute valueanswer = Math.abs(-67);

System.out.println("Abs " + answer);

Page 49: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

49

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

//minanswer = Math.min(b, c);

System.out.println("Min " + answer);

Page 50: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

50

Examples:double a = 81, answer = 0.0;int b = 50, c = 67;

//maxanswer = Math.max(b, c);

System.out.println("Max " + answer);

Page 51: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

51

Results:

Log 4.394449154672439Sqr 9.0Pwr 100.0Sin 0.8939966636005579Abs 67.0Min 50.0Max 67.0

Page 52: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

52

Projects:MathFahrenheit to/from Celsius:Jogging DistanceSquare Meters to MowPathageorin theorumPopulation GrowthBMIDog years to human yearsPound to/from kilosIncome tax calculator

Page 53: 1 Arithmetic Expressions and Math Class zWork with and modify variables, assignments, increment/decrement, Casting and modulus Math API.

53

TEST IS THE DAY AFTER THE PROJECT IS

DUE !!!