Lesson 1.11: Math Class | AP CSA
Lesson 1.11: Math Class
What you'll learn in this lesson
-
1.11.A. Write expressions that incorporate calls to
Math.abs,Math.pow,Math.sqrt, andMath.random, and determine the values they produce. -
1.11.A. Manipulate
Math.random()values using arithmetic and casting to produce a random integer or double in a defined range.
This lesson is part of the AP CSA Course and Exam Description (effective May 2027).
AP exam weight: The Math class methods are provided on the Java Quick Reference. The most-tested patterns are: nested calls (Math.sqrt(Math.pow(a,2)+Math.pow(b,2))), random range formulas, and return-type traps (Math methods return double, not int).
Key Vocabulary
| Term | Definition |
|---|---|
| Math class | A java.lang utility class with only static methods for mathematical operations; cannot be instantiated. |
| nested method call | A method call used as the argument to another method; the inner call executes first. |
| Math.abs(x) | Returns the absolute value of x; overloaded for int (returns int) and double (returns double). |
| Math.pow(base, exp) | Returns base raised to the power exp as a double. |
| Math.sqrt(x) | Returns the non-negative square root of x as a double. |
| Math.random() | Returns a double in [0.0, 1.0). Multiply and cast to produce a random integer in a defined range. |
| inclusive endpoint | A range boundary that includes the boundary value. |
| exclusive endpoint | A range boundary that excludes the boundary value; 1.0 is the exclusive upper bound of Math.random(). |
Math class: review and deep application
All Math class methods are static and belong to java.lang, so no import is needed. Every method that takes a numeric argument and returns a numeric result returns a double (except the int overload of abs).
Full Quick Reference Table
| Method | Returns | Example |
|---|---|---|
Math.abs(int x) |
int |
Math.abs(-5) → 5 |
Math.abs(double x) |
double |
Math.abs(-2.5) → 2.5 |
Math.pow(double base, double exp) |
double |
Math.pow(2,8) → 256.0 |
Math.sqrt(double x) |
double |
Math.sqrt(25) → 5.0 |
Math.random() |
double in [0.0, 1.0) |
range [0.0, 1.0) |
Nested Math calls
Math methods can be nested. The inner call executes first, its return value becomes the argument to the outer call.
Distance formula (Pythagorean theorem)
double dist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
Inner calls first: Math.pow computes each squared difference. Then Math.sqrt operates on their sum.
Random ranges: inclusive and exclusive endpoints
General Formula
// Random int from min to max inclusive (int)(Math.random() * (max - min + 1)) + min
The range size is max - min + 1. The cast truncates to an integer, then adding min shifts to the correct start.
AP Trap: Math methods always return double (except abs on int)
Math.sqrt, Math.pow, and Math.random always return double. Assigning their result directly to an int variable without a cast is a compile error. Math.abs(int) is the only Math Quick Reference method that returns int when given an int argument.
AP Trap: (int)(Math.random() * n) produces 0 to n-1, not 0 to n
Because Math.random() never returns 1.0, (int)(Math.random() * n) produces values from 0 through n - 1 inclusive. To include n, multiply by n + 1. To shift the minimum, add min.
AP Trap: argument order in Math.pow
Math.pow(base, exponent) — the first argument is the base, the second is the exponent. Math.pow(2, 8) computes 28 = 256. Reversing to Math.pow(8, 2) computes 82 = 64.
Practice Questions
Math.sqrt(Math.pow(6, 2) + Math.pow(8, 2))?(int)(Math.random() * 10) + 5
(int)(Math.random() * 15) + 5
(int)(Math.random() * 10) + 6
(int)(Math.random() * 11) + 5
(int)(Math.random()*11)+5 gives 5–15. A gives 5–14 (misses 15). B gives 5–19 (too wide). C gives 6–15 (misses 5).int result = Math.pow(4, 3);. What happens?Math.pow returns double and no cast is present.result.result.Math.pow always returns double. Assigning 64.0 (double) to int without an explicit cast is a compile error. Fix: int result = (int) Math.pow(4, 3);
a and b (as an int) where int a=3, b=8?I.
Math.abs(a - b)II.
Math.abs(b - a)III.
(int) Math.abs((double)(a - b))
(int)(Math.random() * 8) + 3?(int)(Math.random() * 8) + 3?Math.abs is NOT true?I.
Math.abs(0) returns 0.II.
Math.abs is overloaded — there is both an int and a double version.III.
Math.abs(-3.5) returns an int.Math.random() * 5.0 + 2.0
Math.random() * 5.0
Math.random() * 3.0 + 2.0
Math.random() * 3.0
Math.random() * 3.0 + 2.0 gives [2.0, 5.0). A gives [2.0, 7.0). B gives [0.0, 5.0). D gives [0.0, 3.0).Output Predictor — Math Class
Type the exact output.
Bug Hunt — Math Class
Each snippet has exactly one buggy line. Click it, then submit.
The Geometry Calculator
A student writes code to compute triangle properties.
double a = 5.0, b = 12.0; double hyp = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); int randomSide = (int)(Math.random() * 3) + 1; double area = 0.5 * a * b; System.out.println(hyp);
Part A
Part B
randomSide?Part C: Structured response
The student wants to add a line that computes the perimeter of the right triangle and stores it as a double. Write the line of code using a, b, and hyp, and explain what each component contributes.
Scoring Rubric (3 points)
-
+1: Correct code:
double perimeter = a + b + hyp; -
+1: Explains that
a(5.0) andb(12.0) are the two legs andhyp(13.0) is the hypotenuse previously computed using the Pythagorean theorem. -
+1: Notes that the result is a double because
hypis double and double arithmetic promotes the int additions.
Math.round() and Math.floor()
Math.round(double x) rounds to the nearest long (add 0.5 then truncate is equivalent). Math.floor(double x) rounds down to the nearest integer value, returned as a double. Math.ceil(double x) rounds up. These are not on the AP Quick Reference but are commonly used in real Java programs.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]