Unit 1.11: Using the Math Class¶
Scope: CS Awesome 2, Section 1.11
Learning Goals¶
By the end of this lesson, you should be able to:
- Call class methods in
Math. - Use
abs,pow,sqrt, andrandom. - Select the correct overloaded
absmethod. - Generate random values in a requested range.
- Avoid common casting and range errors.
The Math Class¶
Math belongs to java.lang, so no import is required. Its methods are class methods and are called with the class name and dot operator.
double root = Math.sqrt(81.0);
Do not create a Math object. Use Math.method(arguments).
AP Java Quick Reference Methods¶
| Method | Return value |
|---|---|
int abs(int x) |
absolute value of an int |
double abs(double x) |
absolute value of a double |
double pow(double base, double exponent) |
base raised to exponent |
double sqrt(double x) |
positive square root |
double random() |
random double in [0.0, 1.0) |
Absolute Value¶
abs is overloaded. Argument type determines which version is called.
int distance1 = Math.abs(-12); // 12
double distance2 = Math.abs(-3.75); // 3.75
The return type matches the selected overload.
int a = Math.abs(-4); // valid
double b = Math.abs(-4.0); // valid
int c = Math.abs(-4.0); // invalid: double result into int
Powers and Square Roots¶
double square = Math.pow(5, 2); // 25.0
double cube = Math.pow(2, 3); // 8.0
double root = Math.sqrt(49); // 7.0
Both pow and sqrt return double, even when their arguments and mathematical results look like whole numbers.
Pythagorean distance:
double width = 6.0;
double height = 8.0;
double diagonal = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
System.out.println(diagonal); // 10.0
Random Numbers¶
double value = Math.random();
The result satisfies:
0.0 <= value < 1.0
1.0 is excluded.
Random Integer from Zero¶
To generate one of 0, 1, 2, 3, 4:
int value = (int) (Math.random() * 5);
Scale first, then cast. Casting Math.random() before multiplication always produces 0.
int wrong = (int) Math.random() * 5; // always 0
Random Integer in an Inclusive Range¶
For an integer from min through max, inclusive:
int value = (int) (Math.random() * (max - min + 1)) + min;
For 5 through 10:
int value = (int) (Math.random() * 6) + 5;
Possible pre-cast values are from 0.0 up to but not including 6.0; the cast yields 0 through 5, and adding 5 gives 5 through 10.
Random Double in a Range¶
For a double from min inclusive to max exclusive:
double value = Math.random() * (max - min) + min;
Practice Missions¶
Mission 1: Method and Type Trace¶
Predict the value and type of each expression:
Math.abs(-9)
Math.abs(-9.0)
Math.pow(3, 2)
Math.sqrt(2.25)
Mission 2: Coordinate Distance¶
Given two points (x1, y1) and (x2, y2), write an expression for the straight-line distance using Math.pow and Math.sqrt.
Mission 3: Random Locker¶
Write expressions for:
- an integer from 0 through 7;
- an integer from 12 through 20;
- a double from -2.0 inclusive to 3.0 exclusive.
For each, prove the minimum and maximum possible result.
Mission 4: Repair the Random Formula¶
The code is intended to produce an integer from 1 through 6, but always prints 1. Explain and fix it.
int die = (int) Math.random() * 6 + 1;
Mission 5: Nested Methods¶
Write one expression that calculates the absolute difference between Math.sqrt(80) and 9. Store the result in a correctly typed variable.
Key Summary¶
| Concept | Core idea |
|---|---|
Math |
java.lang class containing class methods. |
abs |
Overloaded for int and double. |
pow |
Returns a double power. |
sqrt |
Returns a positive double square root. |
random |
Returns a value in [0.0, 1.0). |
| Inclusive integer range | (int)(Math.random() * range) + min. |
Source scope: CS Awesome 2, Unit 1.11