Lesson 1.11: Math Class | AP CSA

Unit 1 · Lesson 1.11 · Code Mechanics

Lesson 1.11: Math Class

Reading time: 8–10 min·Practice: 8 exercises·Mastery: applied scenario

What you'll learn in this lesson

  • 1.11.A. Write expressions that incorporate calls to Math.abs, Math.pow, Math.sqrt, and Math.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.

▶ Java Code Editor
Try It Yourself
Write real Java, hit Run, and your code executes on a live compiler. Output is checked automatically.
Tier 2 · AP Practice

Practice Questions

What is the result of Math.sqrt(Math.pow(6, 2) + Math.pow(8, 2))?
Evaluate the inner pow calls first, then sum, then take the square root.
A. 10
B. 10.0
C. 100.0
D. 14.0
B. 62+82=36+64=100. sqrt(100)=10.0 (double). A is wrong type (int). C is the sum before sqrt. D adds 6 and 8.
Which expression produces a random integer from 5 to 15 inclusive?
A. (int)(Math.random() * 10) + 5
B. (int)(Math.random() * 15) + 5
C. (int)(Math.random() * 10) + 6
D. (int)(Math.random() * 11) + 5
D. Range size = 15-5+1 = 11. Formula: (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).
A student writes int result = Math.pow(4, 3);. What happens?
A. A compile error because Math.pow returns double and no cast is present.
B. Compiles and runs, storing 64 in result.
C. Compiles and runs, storing 64.0 in result.
D. A run-time error because the result exceeds int range.
A. 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);
Consider these three expressions. Which correctly computes the absolute difference between two variables 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))
A. I only
B. II only
C. I, II, and III
D. I and II only
C. I: Math.abs(3-8)=Math.abs(-5)=5 (int). II: Math.abs(8-3)=Math.abs(5)=5 (int). III: casts to double first, abs gives 5.0, cast back to int gives 5. All three produce 5 as an int.
What is the minimum possible value produced by (int)(Math.random() * 8) + 3?
A. 0
B. 3
C. 4
D. 8
B. Math.random() minimum is 0.0. 0.0*8=0.0, (int)0.0=0, 0+3=3. Minimum is 3.
What is the maximum possible value produced by (int)(Math.random() * 8) + 3?
A. 11
B. 8
C. 9
D. 10
D. Math.random() max approaches but never reaches 1.0. Closest: just under 1.0. (int)(0.999...*8)=(int)(7.999...)=7. 7+3=10. Maximum is 10. This expression produces values 3–10.
Which of the following statements about 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.
A. III only
B. I only
C. I and II only
D. II and III only
A. I is true: Math.abs(0)=0. II is true: Math.abs is overloaded with int and double versions. III is false: Math.abs(-3.5) calls the double version and returns 3.5 (a double), not an int. Only III is not true.
A programmer needs to generate a random double between 2.0 (inclusive) and 5.0 (exclusive). Which expression is correct?
A. Math.random() * 5.0 + 2.0
B. Math.random() * 5.0
C. Math.random() * 3.0 + 2.0
D. Math.random() * 3.0
C. Range width = 5.0 - 2.0 = 3.0. Formula: 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).
PRACTICE WITH A GAME — CHOOSE ONE:

Output Predictor — Math Class

Type the exact output.

Question 1 of 6Score: 0

Bug Hunt — Math Class

Each snippet has exactly one buggy line. Click it, then submit.

Bug 1 of 7Caught: 0

Tier 3 · AP Mastery Challenge

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

What does the program print?
Compute a^2 + b^2 first, then take the square root.
A. 13
B. 13.0
C. 169.0
D. 17.0
B. 5^2+12^2=25+144=169. sqrt(169)=13.0. Math.sqrt always returns double so 13.0, not 13. Classic 5-12-13 right triangle.

Part B

What is the range of possible values for randomSide?
A. 0 to 2 inclusive
B. 0 to 3 inclusive
C. 1 to 4 inclusive
D. 1 to 3 inclusive
D. (int)(Math.random()*3) gives 0-2. Adding 1 gives 1-3.

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.

Extension · Beyond the Exam

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]