You've already learned how to perform some basic
mathematical operations using arithmetic expressions,
but there are some more intermediate operations that we
still need to learn how to do. Most of these operations
can be done with methods that are part of
Java's
Math class.
Pre-Requisites
Before doing this tutorial, make sure you've worked
through the
Arithmetic
Expressions tutorial and that you're comfortable writing statements
and expressions that perform basic mathematical operations.
The Math Class
The Math class is a class that already exists in Java,
and it is a part of the java.lang package.
You can use the Math class to perform mathematical
operations such as absolute value, exponentiation, and
square roots.
The Math class consists of methods
that allow to perform these operations. A
method is a chunk of code that
performs a task. Some methods require data in order to
perform that task. Some methods also give a result after
performing a tasks: this is called "returning a value".
For example, a Math method called pow()
will calculate the result of a number to the power of
another number. The pow() method
requires some information before it can give you any
results: It needs to
know the base number and the exponent. You
can give the pow() method these two
pieces of information and it will return
the result of the base number to the power
of the exponent. The data type of the return
value is always a double.
For example, if you wanted to know
the result of 72, then you would give the
pow() method 7 and 2. The pow()
method would then return the value 49.0.
Math methods are static methods.
This means that to invoke them
or use them, you need to put the name of the
class in front of the method name with a dot or
period. For example, to
use the pow() method, you refer to it as
Math.pow().
If a method needs inputs in order to perform its task,
you would put them in the brackets, separated by commas.
For example, Math.pow(7, 2) invokes the
pow() method using the value 7 as the
base number and 2 as the exponent.
These inputs are often called arguments
or parameters. The pow()
method always knows that the first
argument you give it is the base number and the second argument
is the exponent. When learning about a new method,
you need to check its documentation to see if it requires
inputs, and if so, what order those inputs should go in.
If a method returns a value or gives you a result, you need to
store that result somewhere. The pow() method
returns a double value that is the result of the first
argument to the power of the second argument. You will need
to store this result in a variable most of the time:
double power = Math.pow(7, 2);
You can also output the result instead:
System.out.println(Math.pow(7, 2));
Both of these are correct, but if you need to "remember" the
result for use later, you'll need to use a variable
as in the first example.
What does the following main method do?
public static void main(String[] args) {
Math.pow(7, 2);
}
If you're not sure, try it out!
Remember: if a method returns a value, you need to store it
somewhere or directly send it to an output device!
Common Methods from the Math Class
Besides Math.pow(), there are lots of other
useful methods in the Math class. Here are some that
you might use often:
Common Math Methods
Method
Description
Examples
Math.sqrt(a)
Returns the square root of the double
value a as a double.
double num1 = 25;
double num2 = 81;
System.out.println(Math.sqrt(num1)); // prints 5.0
System.out.println(Math.sqrt(num2)); // prints 9.0
Math.cbrt(a)
Returns the cubed root of the double
value a as a double.
double num1 = 9;
double num2 = 64;
System.out.println(Math.cbrt(num1)); // prints 3.0
System.out.println(Math.cbrt(num2)); // prints 4.0
Math.abs(a)
Returns the absolute value of a.
The return value matches
the data type of the argument a.
int num1 = -5;
int num2 = 3;
System.out.println(Math.abs(num1)); // prints 5
System.out.println(Math.abs(num2)); // prints 3
Math.ceil(a)
Returns the ceiling of the double
value a as a double.
The ceiling of a number is the whole number that is
greater than the original number.
double num1 = 1.5;
double num2 = -1.5;
System.out.println(Math.ceil(num1)); // prints 2.0
System.out.println(Math.ceil(num2)); // prints -1.0
Math.floor(a)
Returns the floor of the double
value a as a double.
The floor of a number is the whole number that is
less than the original number.
double num1 = 1.5;
double num2 = -1.5;
System.out.println(Math.floor(num1)); // prints 1.0
System.out.println(Math.floor(num2)); // prints -2.0
Math.round(a)
Rounds the value of a to the nearest
whole number. If a is a double,
the method returns a long; a
is a float, the method returns an int.
double num1 = 2.9;
double num2 = 2.2;
System.out.println(Math.round(num1)); // prints 3
System.out.println(Math.round(num2)); // prints 2
Math.max(a, b)
Returns a copy of the largest value of the values a
and b. The method returns the same data type as
a and b's data type.
int num1 = 2;
int num2 = 5;
System.out.println(Math.max(num1, num2)); // prints 5
Math.min(a, b)
Returns a copy of the smallest value of the values a
and b. The method returns the same data type as
a and b's data type.
int num1 = 2;
int num2 = 5;
System.out.println(Math.min(num1, num2)); // prints 2
Math.hypot(a, b)
Returns the hypotenuse (the square root of (a2
+ b2) of the double
values a and b as a
double.
1. What Math class methods
would you use to perform the following tasks?
Write the expression for each.
find the square root of 13
find the minimum value of the two numbers stored in the variables
dblNum1 and dblNum2
find the ceiling of -123.45
find the floor of -123.45
find the absolute value of -123.45
2. Write a single statement to perform each of the following
calculations and store each result in a variable
of the appropriate type:
The square root of x - y
The absolute value of a2 - b2,
assuming a and b are integers.
The area of a circle (π multiplied by radius-squared)
3. Write each of the following expressions as a single Java statement:
Text: c equals the root of a squared plus b squared
(a squared plus
b squared is all under the square root symbol)
Text: p equals the square root of the absolute value of the
expression m minus ne
Text: sum equals the result of a division expression:
the numerator is the variable a times the result of the
expression r to the power of n minus 1, and the denominator
is the expression r minus 1
1. Write the expressions:
find the square root of 13 Math.sqrt(13)
find the minimum value of the two numbers stored in the variables
dblNum1 and dblNum2 Math.min(dblNum1, dblNum2)
find the ceiling of -123.45 Math.ceil(-123.45)
find the floor of -123.45 Math.floor(-123.45)
find the absolute value of -123.45 Math.abs(-123.45)
2. Write a single statement to perform each of
the following calculations and store each result in a variable
of the appropriate type:
The square root of x - y double root = Math.sqrt(x - y);
The absolute value of a2 - b2,
assuming a and b are integers. double value = Math.abs(Math.pow(a, 2) - Math.pow(b, 2)); (Math.pow() always returns a double regardless of its arguments).
The area of a circle (π multiplied by radius-squared) double radius = Math.PI * Math.pow(radius, 2);
3. Write each of the following expressions as a single Java statement:
a.
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));