Unit 1.11: Math Class

AP Computer Science A
Using Objects and Methods
Learning Objective 1.11.A
Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.

What is the Math Class?

Essential Knowledge 1.11.A.1
The Math class is part of the java.lang package.
Key Points:
Classes in the java.lang package are available by default
No import statement needed
Contains only static methods
All methods are called using the class name: Math.methodName()
// No import needed! double result = Math.sqrt(25);

Math.abs() - Absolute Value

static int abs(int x)
static double abs(double x)
Returns the absolute value of a number (distance from zero).
Integer Version:
int x = -15; int result = Math.abs(x); // result is 15
Double Version:
double y = -3.7; double result = Math.abs(y); // result is 3.7

Math.pow() - Power Function

static double pow(double base, double exponent)
Returns the value of the first parameter raised to the power of the second parameter.
Example 1: Square a number
double result = Math.pow(5, 2); // result is 25.0 (5 squared)
Example 2: Cube a number
double result = Math.pow(3, 3); // result is 27.0 (3 cubed)
Example 3: Negative exponent
double result = Math.pow(2, -3); // result is 0.125 (1/2 cubed)

Math.sqrt() - Square Root

static double sqrt(double x)
Returns the nonnegative square root of a double value.
Perfect Squares:
double result1 = Math.sqrt(16); // result1 is 4.0 double result2 = Math.sqrt(144); // result2 is 12.0
Non-Perfect Squares:
double result = Math.sqrt(10); // result is 3.1622776601683795

Math.random() - Random Numbers

static double random()
Returns a double value greater than or equal to 0.0 and less than 1.0.
Range: [0.0, 1.0) - includes 0.0, excludes 1.0
Basic Usage:
double rand = Math.random(); // Examples of possible values: // 0.0, 0.234, 0.891, 0.5, 0.999999 // Never equals 1.0

Random Integers in a Range

Essential Knowledge 1.11.A.3
The values returned from Math.random() can be manipulated using arithmetic and casting operators to produce a random int or double in a defined range.
Formula for Random Integer:
(int)(Math.random() * range) + min
Where range = max - min + 1
Example 1: Random int from 1 to 6 (dice roll)
int dice = (int)(Math.random() * 6) + 1; // Range: 6, Min: 1 // Possible values: 1, 2, 3, 4, 5, 6
Example 2: Random int from 10 to 20
int num = (int)(Math.random() * 11) + 10; // Range: 11 (20-10+1), Min: 10 // Possible values: 10-20 inclusive

Practice Problems

Test Your Understanding
Problem 1:
What is the output of: Math.abs(-7.5)
Answer: 7.5
Problem 2:
What does Math.pow(2, 4) return?
Answer: 16.0 (2 to the 4th = 2x2x2x2)
Problem 3:
Write code to generate a random integer from 1 to 100 inclusive.
(int)(Math.random() * 100) + 1
Problem 4:
What is the range of values returned by Math.random()?
Answer: [0.0, 1.0) - includes 0.0, excludes 1.0

Quick Reference Summary

Math Class Methods
Math.abs(x)
Returns absolute value of x
Math.pow(base, exponent)
Returns base raised to the power of exponent
Math.sqrt(x)
Returns nonnegative square root of x
Math.random()
Returns random double in [0.0, 1.0)
Remember:
No import needed (java.lang package)
All methods are static
Call using: Math.methodName()
Random integers: (int)(Math.random() * range) + min
End of Unit 1.11: Math Class
1 / ?