AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-11)
Unit 1 · Topic 1.11
1.11 Math Class
The Math class gives you absolute value, powers, square roots and random numbers. The random-number formulas are the part most students get wrong, so this topic spends extra time on building a range with the right endpoints.
Key terms
MathclassMath.absMath.powMath.sqrtMath.random- inclusive vs. exclusive
The Math class
Math is in the java.lang package, so it's always available with no import. Every method in it is a class method (static), so you call each one with Math. in front. You need these five, all listed on the Java Quick Reference:
| Method | Returns | Example |
|---|---|---|
| Math.abs(int x) | the absolute value, as an int | Math.abs(-7) is 7 |
| Math.abs(double x) | the absolute value, as a double | Math.abs(-2.5) is 2.5 |
| Math.pow(double base, double exp) | base raised to the power exp, as a double | Math.pow(2, 3) is 8.0 |
| Math.sqrt(double x) | the non-negative square root, as a double | Math.sqrt(25) is 5.0 |
| Math.random() | a random double from 0.0 up to but not including 1.0 | something like 0.7254 |
Watch the return types
pow and sqrt always return a double, even when the answer is a whole number. Math.pow(2, 3) is 8.0, so int p = Math.pow(2, 3); won't compile. Cast it: int p = (int) Math.pow(2, 3);.
You can combine the methods. The distance between two points uses all of them:
int x1 = 1;
int y1 = 2;
int x2 = 4;
int y2 = 6;
double d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
System.out.println(d);
This prints 5.0: the differences are 3 and 4, and the square root of 9 + 16 is 5.
Random numbers in a range
Math.random() returns a value greater than or equal to 0.0 and less than 1.0. Including an endpoint is called inclusive; leaving it out is exclusive. So the range is 0.0 inclusive to 1.0 exclusive.
To get a random int, stretch the range by multiplying, cut off the decimal with (int), then shift it by adding:
int roll = (int) (Math.random() * 6) + 1;
Math.random() * 6 is from 0.0 up to (not including) 6.0. Casting gives one of 0, 1, 2, 3, 4 or 5. Adding 1 gives 1 to 6, like a die.
- For a random
intfrommintomax, both inclusive:(int) (Math.random() * (max - min + 1)) + min. - The number you multiply by is how many different values you want. The number you add is the smallest value.
- For a random
doublefroma(inclusive) tob(exclusive):Math.random() * (b - a) + a. For example,Math.random() * 5 + 20gives a value from 20.0 up to but not including 25.0.
Where the parentheses go
The cast must cover the whole product. (int) Math.random() * 6 casts Math.random() alone, which is always 0, so the whole expression is always 0. Write (int) (Math.random() * 6).
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Reading a random range
What values can
valuehold after this statement runs?int value = (int) (Math.random() * 8) + 3;Show the solutionHide the solution
- Step 1:
Math.random()is at least 0.0 and less than 1.0. - Step 2: Multiplying by 8 gives at least 0.0 and less than 8.0.
- Step 3: Casting truncates, so the result is a whole number from 0 to 7. It can never be 8, because the product never reaches 8.0.
- Step 4: Adding 3 shifts that to 3 through 10.
- Step 5: Check with the formula: 8 values starting at 3 means 3, 4, 5, 6, 7, 8, 9 and 10.
Answer: Any integer from 3 to 10, inclusive: 3, 4, 5, 6, 7, 8, 9 or 10.
- Step 1:
- Example 2
Writing a random range
Write an expression for a random integer from 10 to 20, inclusive.
Show the solutionHide the solution
- Step 1: Count the values: 20 − 10 + 1 = 11. That's the number to multiply by.
- Step 2: The smallest value is 10. That's the number to add.
- Step 3: Put the cast around the product:
(int) (Math.random() * 11) + 10. - Step 4: Check the ends: the smallest product cast is 0, giving 10; the largest is 10, giving 20.
Answer:
(int) (Math.random() * 11) + 10
Common mistakes
- Multiplying by
max - mininstead ofmax - min + 1, which makes the top value impossible. - Casting only
Math.random(), as in(int) Math.random() * 6. That's always 0. - Storing the result of
Math.poworMath.sqrtin anintwithout a cast. - Thinking
Math.random()can return 1.0. It can't; the top end is exclusive.
On the exam
- Expect questions that show a
Math.random()expression and ask for its possible values, or ask which expression produces a given range. Work out the smallest and largest possible results. - The Quick Reference only lists
abs,pow,sqrtandrandom. Don't rely on otherMathmethods in free-response answers; write your own comparison or use a cast instead.
Connected topics
Videos
Check yourself
4 questions on 1.11 Math Class. Pick an answer to see if you got it, and why.
Which of the following expressions evaluates to a random integer from 5 to 12, inclusive, with each value equally likely?
Consider the following code segment.System.out.println(Math.pow(2, 3));What is printed as a result of executing the code segment?
Consider the following code segment.double r = Math.sqrt(Math.abs(-16)) + Math.abs(3 - 7);
System.out.println(r);What is printed as a result of executing the code segment?
Which of the following statements compiles without error?
0 of 4 answered