Unit 2 - Using Objects

2.9 Using the Math Class

The Math class in Java provides a comprehensive collection of mathematical operations and constants. It's part of the java.lang package, which is automatically imported, so you don't need to manually import the Math class to use it. The methods in the Math class are all static, meaning you can call them directly on the class without needing to create an instance of it.
2.9 Using the Math Class

The Math class in Java provides a comprehensive collection of mathematical operations and constants. It’s part of the java.lang package, which is automatically imported, so you don’t need to manually import the Math class to use it. The methods in the Math class are all static, meaning you can call them directly on the class without needing to create an instance of it.

 

Commonly Used Methods in the Math Class

Basic Mathematical Operations

abs(): Returns the absolute value of a given number.

java
int absValue = Math.abs(-10); // Returns 10

 

pow(): Raises a number to the power of another number.

java
double result = Math.pow(2, 3); // Returns 8.0

 

sqrt(): Calculates the square root of a given number.

java
double squareRoot = Math.sqrt(16); // Returns 4.0

 

max() and min(): Return the maximum or minimum of two numbers, respectively.

java
int maxVal = Math.max(10, 20); // Returns 20
 
int minVal = Math.min(10, 20); // Returns 10

 

Trigonometric Operations

sin(), cos(), tan(): Calculate the sine, cosine, and tangent of an angle in radians.

java
double sine = Math.sin(Math.PI / 2);
// Returns 1.0
 
double cosine = Math.cos(Math.PI); // Returns -1.0
 
double tangent = Math.tan(Math.PI / 4); // Returns 1.0 approximately

 

toRadians() and toDegrees(): Convert angles between degrees and radians.

java
double radians = Math.toRadians(180); // Converts 180 degrees to radians
 
double degrees = Math.toDegrees(Math.PI); // Converts PI radians to degrees

 

Rounding Functions

ceil(): Rounds the floating-point number up to the nearest whole number.

java
double ceiling = Math.ceil(9.1); // Returns 10.0

 

floor(): Rounds the floating-point number down to the nearest whole number.

java
double floor = Math.floor(9.9); // Returns 9.0

 

round(): Rounds a floating-point number to the nearest whole number. It rounds up if the fraction is 0.5 or higher, and rounds down otherwise.

java
long rounded = Math.round(9.5); // Returns 10

 

Generating Random Numbers

random(): Returns a pseudo-random double value greater than or equal to 0.0 and less than 1.0.

java
double randomValue = Math.random(); // Returns a random value between 0.0 (inclusive) and 1.0 (exclusive)

 

Constants in the Math Class

The Math class also provides two commonly used constants:

PI: The value of π (pi), approximately 3.14159.

java
double circumference = 2 * Math.PI * radius;

 

E: The base of natural logarithms, approximately 2.71828.

java
double expValue = Math.exp(1); // Returns Euler's number e^1

 

The Math class significantly simplifies the process of performing mathematical calculations in Java by providing these and many other mathematical operations as static methods, making them easily accessible without the need to create any object instances.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *