Java provides many useful methods in the Math class for performing common mathematical functions. A method is a group of statements that performs a specific task. You have already used the pow(a, b) method to compute a^b, Exponent Operations and the random() method for generating a random number. This section introduces other useful methods in the Math class. They can be categorized as trigonometric methods, exponent methods, and service methods. Service methods include the rounding, min, max, absolute, and random methods. In addition to methods, the Math class provides two useful double constants, PI and E (the base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.
Trigonometric Methods
The Math class contains the following methods as shown below for performing trigonometric functions:
The parameter for sin, cos, and tan is an angle in radians. The return value for asin, acos, and atan is a degree in radians in the range between -pie/2 and pie/2. One degree is equal to pie/180 in radians, 90 degrees is equal to pie/2 in radians, and 30 degrees is equal to p/6 in radians.
For example,
Math.toDegrees(Math.PI / 2) returns 90.0
Math.toRadians(30) returns 0.5236 (same as π/6)
Math.sin(0) returns 0.0
Math.sin(Math.toRadians(270)) returns -1.0
Math.sin(Math.PI / 6) returns 0.5
Math.sin(Math.PI / 2) returns 1.0
Math.cos(0) returns 1.0
Math.cos(Math.PI / 6) returns 0.866
Math.cos(Math.PI / 2) returns 0
Math.asin(0.5) returns 0.523598333 (same as π/6)
Math.acos(0.5) returns 1.0472 (same as π/3)
Math.atan(1.0) returns 0.785398 (same as π/4)
Exponent Methods
There are five methods related to exponents in the Math class as shown below:
For example,
Math.exp(1) returns 2.71828
Math.log(Math.E) returns 1.0
Math.log10(10) returns 1.0
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(4.5, 2.5) returns 22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 4.24
The Rounding Methods
The Math class contains five rounding methods as shown below:
For example,
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(-2.0) returns -2.0
Math.ceil(-2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns -2.0
Math.rint(2.5) returns 2.0
Math.rint(4.5) returns 4.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3 // Returns int
Math.round(2.0) returns 2 // Returns long
Math.round(-2.0f) returns -2 // Returns int
Math.round(-2.6) returns -3 // Returns long
Math.round(-2.4) returns -2 // Returns long
The min, max, and abs Methods
The min and max methods return the minimum and maximum numbers of two numbers (int, long, float, or double). For example, max(4.4, 5.0) returns 5.0, and min(3, 2) returns 2.
The abs method returns the absolute value of the number (int, long, float, or double).
For example,
Math.max(2, 3) returns 3
Math.max(2.5, 3) returns 4.0
Math.min(2.5, 4.6) returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
The random Method
This method generates a random double value greater than or equal to 0.0 and less than 1.0 (0 <= Math.random() < 1.0). You can use it to write a simple expression to generate random numbers in any range.
For example,
(int)(Math.random() * 10) // Returns a random integer
between 0 and 9.
50 + (int)(Math.random() * 50) // Returns a random integer between 50 and 99
In general,
a + Math.random() * b // Returns a random number between a and a + b, excluding a + b.
Case Study: Computing Angles of a Triangle
You can use the math methods to solve many computational problems. Given the three sides of a triangle, for example, you can compute the angles by using the following formula:
Don’t be intimidated by the mathematic formula. You don’t have to know how the mathematical formula is derived in order to write a program for computing this. Here in this example, given the length of three sides, you can use this formula to write a program to compute the angles without having to know how the formula is derived. In order to compute the lengths of the sides, we need to
know the coordinates of three corner points and compute the distances between the points.
Below is a program that prompts the user to enter the x- and y-coordinates of the three corner points in a triangle and then displays the three angles.
Note that the distance between two points (x1, y1) and (x2, y2) can be computed using the formula sqrt of ((x2 - x1)^2 + (y2 - y1)^2). The program computes the distances between two points (lines 19–21), and applies the formula to compute the angles (lines 24–26). The angles are rounded to display up to two digits after the decimal point (lines 29).
The Math class is used in the program, but not imported, because it is in the java. lang package. All the classes in the java.lang package are implicitly imported in a Java program.
Top comments (0)