AP Computer Science A:
Using the Math Class
This Lesson Will Cover:
● The Math Class
● Calling static methods
● Math class methods
● Math.random
Recap: Instance Methods
Instance (non-static) methods need to be called with an
object of the class as a receiver.
/*rectangle is an instance of the Rectangle class
and getArea() is an instance method */
rectangle.getArea();
Instance methods can also be
called non-static methods.
Class Methods
What if you want to use a method without
creating an instance?
Static Methods
Static methods are the methods in Java that can
be called without creating an object of a class.
They are referenced by the class name itself.
Creating static methods
Instance method:
public int getWidth()
When a method is declared
with the static keyword, it is
known as static method.
Static method:
public static void rectEquations()
Creating static methods
Static method:
public static void rectEquations()
{
System.out.println(“The formula for area is: L * W”);
System.out.println(“The formula for perimeter is: 2(L + W)”);
}
Creating static methods
Now that method public static void rectEquations()
exists, we can call it using the name of the class it belongs
to:
The formula for area is: L * W
Rectangle.rectEquations();
The formula for perimeter is: 2(L + W)
Math class
One class that exclusively contains static methods
is the Math class
The Math Class
The java.lang.Math class contains static
methods for performing basic numeric operations
such as the elementary exponential, logarithm,
square root, and trigonometric functions.
java.lang package: Math Class
Object
Math Number String System
Double Integer
Static Methods Don’t Need an Instance!
int x = Math.abs(-5);
This is the Math class name, not the
name of an object.
Static Methods Don’t Need an Instance!
To call this method we did NOT need to create an instance
of the object. This would be a tedious process every time we
wanted to use the Math class...
Math m = new Math();
int n = m.abs(-5);
System.out.println(n);
Static Methods Don’t Need an Instance!
To call this method we did NOT need to create an instance
of the object. This would be a tedious process every time we
wanted to use the Math class...
Math m = new Math();
int n = m.abs(-5);
System.out.println(n);
Math class Methods
Math Class Methods
Method Use
Math.abs(x) Returns the absolute value of x.
Math.pow(base, exponent) Returns the value of base raised to the
power of exponent.
Math.sqrt(x) Returns the positive square root of x.
Math.random() Returns a double value greater than or
equal to 0.0 and less than 1.0
Math class Methods
Math Class Methods
Method Use
Math.abs(x) Returns the absolute value of x.
Math.pow(base, exponent) Returns the value of base raised to the
power of exponent.
Math.sqrt(x) Returns the positive square root of x.
Math.random() Returns a double value greater than or
equal to 0.0 and less than 1.0
Absolute Value
Math.abs returns the absolute value
of the input value.
The Math.abs method
has a constructor for both
int x = Math.abs(-5); double and int values!
double y = Math.abs(-10.3);
System.out.println(“Absolute value of -5:” + x);
Absolute value of -5: 5
Math class Methods
Math Class Methods
Method Use
Math.abs(x) Returns the absolute value of x.
Math.pow(base, exponent) Returns the value of base raised to the
power of exponent.
Math.sqrt(x) Returns the positive square root of x.
Math.random() Returns a double value greater than or
equal to 0.0 and less than 1.0
Raising to a Power
double pow(double base, double exponent) returns the
value of the first parameter raised to the power of the
second parameter.
double cubed = Math.pow(2, 3);
System.out.println(“2^3 = ” + cubed); 2^3 = 8
Math class Methods
Math Class Methods
Method Use
Math.abs(x) Returns the absolute value of x.
Math.pow(base, exponent) Returns the value of base raised to the
power of exponent.
Math.sqrt(x) Returns the positive square root of x.
Math.random() Returns a double value greater than or
equal to 0.0 and less than 1.0
Square Roots
double sqrt(double x) returns the positive square root of
a double value
double squareRoot = Math.sqrt(255.5);
System.out.println(squareRoot); 15.9843673631
Math class Methods
Math Class Methods
Method Use
Math.abs(x) Returns the absolute value of x.
Math.pow(base, exponent) Returns the value of base raised to the
power of exponent.
Math.sqrt(x) Returns the positive square root of x.
Math.random() Returns a double value greater than or
equal to 0.0 and less than 1.0
Generating Random Numbers
double random() returns a double value greater than or
equal to 0.0 and less than 1.0. The random method does
NOT take any parameters.
Generates a different number
every time Math.random is
double rand = Math.random(); executed.
System.out.println("Random Number:" + rand);
Random Number: 0.5349702067386971
Manipulating Math.random()
What if we don’t want random numbers
generated from 0.0 - 1.0, but instead from 0-9?
Random Numbers in a Defined Range
Generate a random number between 0 and 9:
int rand = (int)(Math.random() * 10);
Random Numbers in a Defined Range
Generate a random number between 0 and 9:
int rand = (int)(Math.random() * 10);
Generates a number between
0.0 and 1.0:
0.5349702067386971
Random Numbers in a Defined Range
Generate a random number between 0 and 9:
int rand = (int)(Math.random() * 10);
Multiplies random number by
10:
5.349702067386971
Random Numbers in a Defined Range
Generate a random number between 0 and 9:
int rand = (int)(Math.random() * 10);
Removes decimals from the
numerical value:
5
Random Numbers in a Defined Range
Generate a random number between 0 and 9:
int rand = (int)(Math.random() * 10);
Call to Math.random() Multiply by 10 Cast to int
0.2396070187920506 2.396070187920506 2
0.0076898059369292 0.076898059369292 0
0.1138854030030303 1.138854030030303 1
0.7881929515623757 7.881929515623757 7
0.9995677392929999 9.995677392929999 9
Creating Random Range
The general formula for generating any range from 0 -
integer is:
int randInteger = (int)(Math.random() * (integer + 1))
Creating Random Range
The general formula for generating any range from 0 -
integer is:
int randInteger = (int)(Math.random() * (integer + 1))
Creates number from 0 - .99...
Creating Random Range
The general formula for generating any range from 0 -
integer is:
int randInteger = (int)(Math.random() * (integer + 1))
Turns initial random number to a
number from 0 - integer.99
Creating Random Range
The general formula for generating any range from 0 -
integer is:
int randInteger = (int)(Math.random() * (integer + 1))
Math.random() = 0 - (almost)1 , so if we multiply by integer, the
random number range increases to become 0 - (almost)integer.
To include the value integer in the range, we need to add
one, so the range becomes 0 - integer(and some change).
Creating Random Range
The general formula for generating any range from 0 -
integer is:
int randInteger = (int)(Math.random() * (integer + 1))
Removes decimal value of
number from 0 - integer.99..
Creating Random Range
The formula for generating any range from 0 - 5 is:
int randInteger = (int)(Math.random() * (5 + 1))
0-5 0 - .99.. 0 - 5.99..
Creating Random Range
If we wanted to change the range from 0-5 to 5-10, we can
add 5 to the end of the equation:
int randInteger = (int)(Math.random() * (5 + 1) + 5)
0 - .99.. 0 - 5.99.. 5 - 10.99..
5 - 10
Creating Random Range
The general formula for generating any range from (start
to start + range) is:
int randInteger = (int)(Math.random() * (range + 1) + start);
The Math Class Documentation
We have additional
documentation for the
Math class in the
CodeHS code editor!
Now It’s Your Turn!
Concepts Learned this Lesson
Term Definition
Static Methods Static methods are the methods in Java that can be called
without creating an object of class. Static methods are called
using the dot operator along with the class name unless they
are defined in the enclosing class.
Math Class The Math class is part of the java.lang package and contains
only static methods.
int abs(int x) Returns the absolute value of an int value
double abs(double x) Returns the absolute value of a double value
double pow(double base, double exponent) Returns the value of the first parameter raised to the power of
the second parameter
double sqrt(double x) Returns the positive square root of a double value
double random() Returns a double value greater than or equal to 0.0 and less
than 1.0
Math.random Can be manipulated to produce a random int or double in a
defined range.
Standards Covered
● (LO) MOD-1.H Call static methods.
● (EK) MOD-1.H.1 Static methods are called using the dot operator along with the class name unless
they are defined in the enclosing class.
● (LO) CON-1.D Evaluate expressions that use the Math class methods.
● (EK) CON-1.D.1 The Math class is part of the java.lang package.
● (EK) CON-1.D.2 The Math class contains only static methods.
● (EK) CON-1.D.3 The following static Math methods—including what they do and when they are
used—are part of the Java Quick Reference:
○ int abs(int x) — Returns the absolute value of an int value
○ double abs(double x) — Returns the absolute value of a double value
○ double pow(double base, double exponent) — Returns the value of the first parameter
raised to the power of the second parameter
○ double sqrt(double x) — Returns the positive square root of a double value
○ double random() — Returns a double value greater than or equal to 0.0 and less than 1.0
● (EK) CON-1.D.4 The values returned from Math.random can be manipulated to produce a random
int or double in a defined range.