📚

 > 

💻 

 > 

📱

2.9 Using the Math Class

5 min readjune 18, 2024

Avanish Gupta

Avanish Gupta

Milo Chang

Milo Chang

Avanish Gupta

Avanish Gupta

Milo Chang

Milo Chang


AP Computer Science A 💻

130 resources
See Units

So far, we have learned of four classes that will help us with later programs: Scanner (in Unit 1), String, Double, and Integer. Let's introduce the last class for the time being: the Math class. The Math class will help us perform many mathematical operations that couldn't have been done before. Here is the Java documentation for the Math class. (Courtesy of Java.)
As you can see, all the methods for Math are static because you can't make a Math object due to the fact that there is no constructor for it. There are a few methods that will be useful in this class:

Absolute Values

To find the absolute value of a number, we find its magnitude. To do this, we use the following method:
  • Math.abs(number)
The return type of the method will be the same as the input type for number. If number is a double, then a double is returned. Likewise, if number is an integer, then an integer is returned.
For example, to calculate the absolute value of -5, we would write Math.abs(-5);.
Here are a few examples of when absolute value might be useful in writing algorithms:
  1. Find the distance between two points: When finding the distance between two points in a coordinate system, you can use absolute value to ensure that the distance is always positive, regardless of the signs of the coordinates.
  2. Simplify calculations: Absolute value can be used to simplify calculations by removing the need to consider the sign of a number. For example, if you want to find the difference between two numbers, you can use absolute value to ensure that the result is always positive.
  3. Ensure that certain conditions are met: Absolute value can be used to ensure that certain conditions are met, such as ensuring that a number is within a certain range. For example, you might use absolute value to ensure that a number is within the range of 0 and 1, or to ensure that a number is within a certain distance of another number.

Exponents and Square Roots

Without the Math class, we could only do multiplication, but with the Math class, we can now do exponents and roots. For exponents, we have the following method:
  • Math.pow(base, exponent)
This will compute the operation base^exponent and return it as a double. The inputs for base and exponent are expected to be doubles.
For example, to calculate 2 to the power of 5 (which equals 32), we would write Math.pow(2,5);.
Here are a few examples of when exponents might be useful in writing algorithms:
  1. Representing repeated multiplication: Exponents can be used in CS algorithms to represent repeated multiplication of a number by itself. For example, 2^3 represents 2 multiplied by itself 3 times, or 8. This can be useful in situations where it is necessary to quickly and efficiently calculate the result of repeated multiplication.
  2. To perform calculations with large numbers: Exponents can be used to perform calculations with very large numbers, such as calculating the result of a large number raised to a high power. This can be useful for tasks such as calculating the number of possible combinations of a set of items, or calculating the number of possible outcomes of a game or simulation.
We can do square roots with the following method:
  • Math.sqrt(a)
This will compute the operation a^0.5 and return it as a double. The input for a is expected to be a double. Note that you could also calculate the square root of a by calling Math.pow(a, 0.5).
For example, to calculate the square root of 25, we could write either Math.sqrt(25); or Math.pow(25, 0.5);.
Here are a few examples of when square roots might be useful in writing algorithms:
  1. To calculate the distance between two points: Square roots can be used to calculate the distance between two points in a coordinate system using the Pythagorean theorem. This can be useful for tasks such as finding the shortest path between two points or estimating the distance between two points on a map.
  2. To calculate statistical measures: Square roots can be used to calculate statistical measures such as standard deviation, which is a measure of the dispersion of a set of data.

Random Numbers

This is the most complex method of the Math class. A call of:
  • Math.random()
will return a double greater than or equal to 0 and less than 1. This is useful for when you need a random number generator.
To implement random numbers greater than or equal to a number a and less than a number b, we make this call:
  • (int)(Math.random() * (b - a) + a)
The (int) is optional. You include it if you want random integers greater than or equal to a and less than b. (This is the same as saying you want random integers from a to b-1, inclusive.) If you do not include (int), you will get random doubles from a to b-1, inclusive.
For example, if you want to generate a random integer from 5 to 555, we set a to 5 since our starting value is 5 and we set b to 556 since our ending value is the value 1 less than 556. Thus, we make this call: (int) (Math.random() * (556 - 5) + 5);.
Here are a few examples of when random numbers might be useful in writing algorithms:
  1. To simulate real-world events: Random numbers can be used to simulate real-world events that involve chance or probability, such as rolling dice, flipping coins, or shuffling cards. This can be useful for tasks such as writing games or simulating physical phenomena.
  2. To access data randomly: Random numbers can be used to generate random indices that you can then use to access data at a certain location.
Browse Study Guides By Unit
Unit 1 – Primitive Types
🖥Unit 3 – Boolean Expressions & if Statements
🕹Unit 4 – Iteration
⚙️Unit 5 – Writing Classes
⌚️Unit 6 – Array
💾Unit 7 – ArrayList
💻Unit 8 – 2D Array
🖲Unit 9 – Inheritance
🖱Unit 10 – Recursion
🧐Exam Skills