The Math class in Java provides built-in static methods to perform common mathematical operations. For AP Computer Science A, the current Java Quick Reference includes the following Math methods: Math.abs(int), Math.abs(double), Math.pow(double, double), Math.sqrt(double), and Math.random().
Key Concepts to Remember Before You Start:
-
Return Types:
Math.pow(),Math.sqrt(), andMath.random()always return adouble. Even if you input integers, the result is adouble. -
Math.random() Range:
This method generates a
doublegreater than or equal to0.0and strictly less than1.0. -
The Casting Trap:
In
(int) Math.random() * 10, the cast applies toMath.random()before the multiplication, so the cast produces0. Use parentheses around the multiplication:(int)(Math.random() * 10).
Test your knowledge with these 10 practice questions. Click an answer to see the explanation and understand the reasoning behind each result.
Topic 1.11 Practice
Math.random() method?Math.random() always returns a double
that includes 0.0 but excludes 1.0. Mathematically, this is represented
as [0.0, 1.0). It will never return exactly 1.0.Math.pow(base, exponent) calculates 3 to the power of 2,
which is 9. Remember that Math.pow() always returns a
double, even if both arguments are integers. Therefore,
it prints 9.0.(int)(Math.random() * totalOutcomes) + min.
A die has 6 outcomes, starting at 1.Math.random() * 6 generates values from
0.0 inclusive to less than 6.0. Casting to (int)
truncates the fractional part, leaving integers 0 through 5.
Adding 1 shifts the range to 1 through 6.System.out.println(result);
Math.random() to an int before the
multiplication, truncating the value to 0. Then it multiplies
0 by 10. The result is always 0.You need parentheses:
(int)(Math.random() * 10).Math.pow(-2, 3) calculates (-2)³, which is
-8.0 because Math.pow() returns a double.Then Math.abs(-8.0) takes the absolute value,
resulting in 8.0.min and max, inclusive, is:(int)(Math.random() * ((max - min) + 1)) + minTo generate values from 5 through 15:
(15 - 5) + 1 = 11possible values.Therefore:(int)(Math.random() * 11) + 5.num in the following code?Math.random() * 4 generates a random decimal from
0.0 up to, but not including, 4.0.Casting to (int) truncates the decimal, leaving
integers {0, 1, 2, 3}.Adding 2 produces:{2, 3, 4, 5}Therefore, the maximum possible value is 5.Math.sqrt() calculates the square root of a number.
Like Math.pow(), it returns a double.It accepts the integer 25, calculates its square root, and returns
5.0.arr. Which of the following correctly generates a valid
random index?0 to
arr.length - 1.Math.random() * arr.length produces a decimal from
0.0 up to, but strictly less than, the length of the array.When cast to an int, the decimal is truncated,
resulting in the range 0 to
length - 1.double b = Math.abs(-10.5);
System.out.println(a + ” ” + b);
Math.pow() and Math.sqrt(),
the Math.abs() method is overloaded.If you pass it an integer, it returns an integer.
If you pass it a double, it returns a double.Therefore, a becomes the integer 10 and
b becomes the double 10.5.Topic 1.11 Practice Complete!
The Math.random() Formula
If you struggled with the random-number questions, use this formula to practice generating an integer in an inclusive range:
int x = (int)(Math.random() * ((MAX - MIN) + 1)) + MIN;
Continue Your AP CSA Practice
After practicing the Math class, continue your AP CSA Unit 1 review with the related object-reference concepts.
Continue your AP CSA Unit 1 review with Topics 1.12–1.14: Objects, References & Instance Methods , covering object creation, references, and instance-method calls.