Browse By Unit
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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:
To find the absolute value of a number, we find its magnitude. To do this, we use the following method:
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:
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:
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:
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:
This is the most complex method of the Math class. A call of:
To implement random numbers greater than or equal to a number a and less than a number b, we make this call:
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:
<< Hide Menu
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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:
To find the absolute value of a number, we find its magnitude. To do this, we use the following method:
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:
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:
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:
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:
This is the most complex method of the Math class. A call of:
To implement random numbers greater than or equal to a number a and less than a number b, we make this call:
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:
© 2024 Fiveable Inc. All rights reserved.