LECTURE: Arithmetic Expressions and Math Class Purpose: Work with and modify variables, assignments , increment/decrement, Casting and modulus. Math API Resources: Deitel & Deitel “Java How to Program” Chapter 2 Litvin, “Java Methods” Chapter 6 C++ Class Notes Chapter 4 Lambert Comprehensive Chapter 3 Handouts: 1. ArithmeticExpressions.java Java application with var expression examples C:\Dave Java\Lecture Code Examples\ Arithmetic Expressions and Math Class\ ArithmeticExpressions \ Intro: We will modify primitive datatypes using simple math operations. We will also examine the math API in Java (java.lang.Math). Arithmetic Operators and Assignments: + - * / % Order of operations is determined by ( ) and by the rank of the operators. Mult and division are performed 1st (LEFT TO RIGHT) Followed by addition and subtraction ORDER: () evaluate from the inside out *, /, % evaluate LEFT to RIGHT +, evaluate LEFT to RIGHT the minus can also be used for negation Example: x = -(y + 2 * z) / 5; a = -a; Examples: avg = (grade1 + grade2 + grade3 + grade4 + grade5) / numGrades; INTEGER MATH a = 25 b = 10 c = 20 d = 30 e = 0 int e = 0; // compile error if not initialized System.out.println(a + b / c * d); // ANS 25 b/c = 0 0 * 30 = 0 + a = 25 System.out.println((a + b) / c + d); // ANS 31 a+b=35 35/20 = 1 Truncates 1.75 // 1+30=31 System.out.println(a * (c / b) * d); // ANS 1500 c/b=2 a*2 = 50 50*30=1500 System.out.println((a + b) / (c * d)); // ANS 0 a+b=35 c*d=600 35/600 = 0 Tr .058 System.out.println(e + a); // ANS 25 // System.out.println(e + A); UNDEFINED VAR A – compile error 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 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 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 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 if (b % 2 == 0) is true if b is an EVEN number 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 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 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 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 Example: int a = 7, b = 2; double ratio; ratio = (double)a / (double)b; system.out.println(ratio); The result is 3.5 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 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 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 here, the (int) cast truncates the number in the direction of 0 (int) 1.99 = 1 (int) –1.99 = -1 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); Compound Assignment Operators: Instead of coding the following: a = a + b; a = a – b; a = a * b; a = a / b; a = a % b; You can use compound assignments: a += b; a -= b; a *= b; a /= b; a %= b; the compound method enforces the concept that thr RVALUE is being processed in a temporary workspace and that result is the ASSIGNED to the RVALUE variable Increment & Decrement: Shorthand for incrementing or decrementing a variable by 1 Instead of the following: a = a + 1; a = a - 1; You can write: a++; a--; ++a; --a; There is an important difference between the two types of increment and decrement when they are used in expressions 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 Math Class: Java.lang.Math Inherited from java.lang.Object (all java classes originate from java.lang.Object) absolute value sin cos log max min pow floor Examples: double a = 81, answer = 0.0; int b = 50, c = 67; System.out.println("NOW ILLUSTRATING Math Methods: "); // LOG answer = Math.log(a); System.out.println("Log " + answer); // Square Root answer = Math.sqrt(a); System.out.println("Sqr " + answer); // Power answer = Math.pow(10, 2); System.out.println("Pwr " + answer); //Sin answer = Math.sin(90); System.out.println("Sin " + answer); //absolute value answer = Math.abs(-67); System.out.println("Abs " + answer); //min answer = Math.min(b, c); System.out.println("Min " + answer); //max answer = Math.max(b, c); System.out.println("Max " + answer); NOW ILLUSTRATING Math Methods: Log 4.394449154672439 Sqr 9.0 Pwr 100.0 Sin 0.8939966636005579 Abs 67.0 Min 50.0 Max 67.0 Projects: Math Fahrenheit to/from Celsius: Jogging Distance Square Meters to Mow Pathageorin theorum Population Growth BMI litvin p.169 Dog years to human years lit p.170 Pound to/from kilos Income tax calculator lit p.72