Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

2.9 Using the Math Class

5 min readdecember 20, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

So far, we have learned of four classes that will help us with later programs: (in Unit 1), , , and . 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 , then a is returned. Likewise, if number is an , then an 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 and roots. For , we have the following method:

  • Math.pow(base, exponent)

This will compute the operation base^exponent and return it as a . 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 might be useful in writing algorithms:

  1. Representing repeated multiplication: 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: 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 . The input for a is expected to be a . 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 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 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.

Key Terms to Review (7)

Double

: In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.

Exponents

: Exponents are a way to represent repeated multiplication of a number by itself. For example, 2^3 means multiplying 2 by itself three times (2 x 2 x 2 = 8).

Integer

: An integer is a data type in programming that represents whole numbers without any decimal or fractional parts.

Math.abs(number)

: The Math.abs() function returns the absolute value of a number, which is the positive value of the number regardless of its original sign.

Math.random()

: Math.random() is a method in Java that generates a random decimal number between 0 (inclusive) and 1 (exclusive). It is commonly used to generate random numbers in programming.

Scanner

: A Scanner is a class in Java that allows the user to read input from various sources, such as the keyboard or a file.

String

: A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.

2.9 Using the Math Class

5 min readdecember 20, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

So far, we have learned of four classes that will help us with later programs: (in Unit 1), , , and . 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 , then a is returned. Likewise, if number is an , then an 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 and roots. For , we have the following method:

  • Math.pow(base, exponent)

This will compute the operation base^exponent and return it as a . 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 might be useful in writing algorithms:

  1. Representing repeated multiplication: 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: 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 . The input for a is expected to be a . 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 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 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.

Key Terms to Review (7)

Double

: In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.

Exponents

: Exponents are a way to represent repeated multiplication of a number by itself. For example, 2^3 means multiplying 2 by itself three times (2 x 2 x 2 = 8).

Integer

: An integer is a data type in programming that represents whole numbers without any decimal or fractional parts.

Math.abs(number)

: The Math.abs() function returns the absolute value of a number, which is the positive value of the number regardless of its original sign.

Math.random()

: Math.random() is a method in Java that generates a random decimal number between 0 (inclusive) and 1 (exclusive). It is commonly used to generate random numbers in programming.

Scanner

: A Scanner is a class in Java that allows the user to read input from various sources, such as the keyboard or a file.

String

: A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.