Using the Math Class
Currently, we have covered the operators of a four function calculator with the ability to add, subtract, multiply, and divide. And, we added that new operator to compute the remainder, the modulus operator %
. However, there are a lot more math functions that we will want to use if we are going to create simulations, physics related games, or analyze data, just to name a few.
Good news, Java have a Math class.
The methods in the Math
class are all class methods. Class methods belong to the class instead of to an object of the class and are usually called using the class name. The keyword static
is used to create class methods.
 |
Methods that are part of the AP Computer Science A Java subset are denoted by the APCSA label. |
Math Methods
We are going to share some common math methods, but you can find a complete list of methods in the Math API.
static int abs(int a) and static double abs(double a)
This method returns the absolute value of a
. There are also abs
methods for other data types too, such as double
.
Your Turn
- Write a statement that will create a
double
variable and compute the absolute value of 3.5
.
static double ceil(double a) & static double floor(double a)
The ceil
method returns the next largest double
that is equal to a whole number. In other words, it rounds up.
The floor
method return the next smalled double
that is equal to a whole number. In other words, it rounds down or truncates the double
.
Your Turn
- Predict the output of the program code.
- Run the code and check your predictions.
- Write a statement to use the method
floor
on the value 3.75
.
value1
is assigned the value 4.0.
static int max(int a, int b)
This method returns the greater of the two values a
or b
. There are additional max
methods that take other data types, such as double
.
Your Turn
- Predict the output of the following code.
- Run the program and check your predictions.
- Add a statement to compute the maximum value of 3 and 3.
- Add a statement to compute the maximum value of -12 and -10.
value1
is assigned the value 5.0.
static int min(int a, int b)
This method returns the smaller of the two values a
or b
. There are additional min
methods that take other data types, such as double
.
Your Turn
- Predict the output of the following code.
- Run the program and check your predictions.
- Add a statement to compute the minimum value of 3 and 3.
- Add a statement to compute the minimum value of -12 and -10.
value1
is assigned the value 3.0.
static double pow(double a, double b)
This method returns the value of a
raised to the b
power.
Your Turn
- Predict the output of the following code.
- Run the program and check your predictions.
- Add a statement to compute the value of 3 to the 3rd power.
value1
is assigned the value 8.0.
static double random()
This method returns a random value between 0.0
(inclusive) and 1.0
(exclusive).
The following formula can be used to adjust the range of possible values to be an int between two values.
int randomValue = (int)(Math.random() * numberOfPossiblities) + lowestNumber;
Note the placement of the parenthesis is really important. You will want the computer to first multiply by the numberOfPossibilities
before casting to an int
value. If you cast Math.random()
to an int
, you will always get 0
, because all of the possible values are between 0.0
(inclusive) and 1.0
(exclusive). They are all decimal values that will truncate to 0
.
Your Turn
- Evaluate these statements and define the range of values that will be generated.
- Add a statement to generate a value between 1 and 6.
- Add a statement to generate a value between 10 and 50.
randDouble
will be assigned a random double between 0.0
inclusive and 1.0
exclusive.
alwayszero
will be assigned 0
because we are casting a double
that is less than 1.
randInt
will be assigned a value between 0
and 9
.
adjustRange
will be assigned a value between 1
and 10
.
static double sqrt(double a)
This method returns the square root of a
.
Your Turn
- Predict the output of the following code.
- Run the program and check your predictions.
- Add a statement to compute the square root of 100.
value
is assigned 4.0
.
value2
is assigned 14.142135623730951
There are many other methods including those that compute trigonometric functions.
Resources