Topic 1.11 — Math Class

Goal: use common Math class methods (as class/static methods), understand their return types, and trace code that uses them.

The big idea

Math is a Java library class that provides useful methods for calculations. These are class (static) methods, so you call them using Math.

Pattern: Math.methodName(...)

Remember from 1.10

  • Class methods are called using the class name: Math.abs(...)
  • Return type tells you what to store it in.
  • Arguments must match the signature (types + order).

Common Math methods (AP-friendly)

You don’t need to memorize every method—focus on how to use them and how to trace them.

Method call What it does Returns
Math.abs(x) Absolute value (distance from 0) int or double (matches type of x)
Math.pow(a, b) a raised to the power b double
Math.sqrt(x) Square root of x double
Math.random() Random number in the range [0.0, 1.0) double
Math.min(a, b) Smaller of two values int or double
Math.max(a, b) Larger of two values int or double

Examples (trace-ready)

int a = Math.abs(-12);      // 12
double b = Math.pow(2, 3);  // 8.0
double c = Math.sqrt(81);   // 9.0

Notice how pow and sqrt return double, even if inputs look like ints.

Random numbers (common AP pattern)

Math.random() returns a double from 0.0 up to (but not including) 1.0. To make a random integer in a range, you scale and cast.

int r = (int) (Math.random() * 10);  // 0 to 9

Multiply first, then cast. The cast truncates.

Common mistake: casting too early

These two are NOT the same:

(int) (Math.random() * 10)   // 0..9 ✅

// WRONG:
(int) Math.random() * 10     // always 0 ❌

Why? (int) Math.random() becomes 0 most of the time (since random is < 1), then 0 * 10 = 0.

Min/Max example

int low = Math.min(4, 9);   // 4
int high = Math.max(4, 9);  // 9

Exam mindset

  • Recognize Math methods as class (static) methods.
  • Pay attention to return types (many return double).
  • When tracing randomness, focus on the range produced by the expression.
  • Parentheses matter with casting.

Quick self-check

  1. What does Math.random() return (range + type)?
  2. Why does Math.pow(2, 3) store nicely in a double?
  3. What is (int) (Math.random() * 5)’s possible range?
  4. What is the difference between Math.min and Math.max?
  5. Why is (int) Math.random() * 10 usually 0?

← Back to Unit 1 topics