Arithmetic Notes and Program Assignments Operators Binary Operators: Operation Addition Subtraction Multiplication Division Computer + * / or % - modulus When using the forward slash (/) for dividing two integers, the result will just give the integer quotient for the result. Example: 8 / 2 is 4 but 7 / 2 is 3 (the decimal part of the answer is not given) When dividing two double numbers, the result is a double number so 7.0 / 2.0 is 3.5 Modulus (%) - is used to get the remainder when doing division with integers. Example: 8 % 2 is 0 since 2 goes into 8 evenly, the remainder is 0. 7 % 2 is 1 since 2 does not go into 7 evenly, the remainder is 1. Order of Operation - follows the same rules as Algebra Parenthesis Multiplication and Division – from left to right Addition and Subtraction - from left to right Unary Arithmetic Operators: 1) + or - simply makes the number positive or negative (+ rarely used since the number is automatically positive) 2) increment and decrement operators: ++ and - adds or subtracts 1 from the variable Examples: a) num = 10; num = num + 1; is the same as num++; b) num = 10; num = num – 1; is the same as num--; or ++num; or --num; c) x = 10; y = ++x; //x now has a value of 11 and so does y d) x = 10; y = x++; //x now has a value of 11 and y has a value of 10 // assignment to y done first then 1 added to x e) x = 10; x = x++; // x has a value of 10 at the end of execution! 1|Page Arithmetic Notes and Program Assignments // the right hand side ++ value of 11 is never stored in x Combined Assignment Operators: +=, -=, *=, /= Examples: a) y -= 3 same as y = y – 3 b) y /= 3 same as y = y / 3 c) y += 3 same as y = y + 3 Math Methods No symbol for powers or roots *Use Math.sqrt method - to find the square root double squareRoot = Math.sqrt(x); *Use Math.pow method to raise a number to a power double power = Math.pow(x, n); //x is the base and n is the exponent Other Math methods: Math.sin(x) Math.asin(x) Math.cos(x) Math.acos(x) Math.tan(x) Math.atan(x) Math.round(x) – closest integer Math.ceil(x) – smallest integer Math.toRadians(x) *Math.abs(x) Math.max(x, y) - the larger of x or y Math.min(x, y) – the smaller of x or y Math.exp(x) - ex Math.log(x) – natural log Math.log10(x) – Decimal log Math.floor(x) – largest integer Math.toDegrees(x) *Math.random() – returns a double in the range of [0.0, 1.0) * May be included on the AP exam Formatting numbers: Format String “%d” “%5d” “Quantity: %5d” “%f” “%.2f” “%7.2f” “%s” %d %.2f Example: 2|Page Format Specifier Examples Sample Output Comments 24 Use d with an integer 24 Spaces are added so that the field width is 5 Quantity: 24 Characters inside a format string but outside a format specifier appear in the output 1.21997 Use f with a floating-point number 1.22 Prints two digits after the decimal point 1.22 Spaces are added so that the field width is 7 Hello Use s with a string 24 1.22 You can format multiple values at once System.out.printf(“Quantity: %d Total: %10.2f”, quantity, total);