MATHEMATICAL FUNCTIONS
Objectives
■ To solve mathematical problems by using the methods in the Math class
Common Mathematical Functions
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.
random()method for generating a random number
pow(a, b) method to compute ab
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.
For Example:
System.out.println(Math.PI);
Basic Math Methods
Method Method Descripting
Math.max() It returns the Largest of two values.
Math.min() It is used to return the Smallest of two values.
Math.abs() It will return the Absolute value of the given value.
Math.pow() It returns the value of first argument raised to the power
to second argument.
Math.sqrt() It is used to return the square root of a number.
ceil(x) x is rounded up to its nearest integer. This integer is returned as
a double value.
floor(x) x is rounded down to its nearest integer. This integer is returned
as a double value.
rint(x) x is rounded up to its nearest integer. If x is equally close to two
integers, the even one is returned as a double value.
round(x) Returns (int)Math.floor(x + 0.5) if x is a float and returns
(long)Math.floor(x + 0.5) if x is a double.
Math.random() It returns a double value with a positive sign, greater than or
equal to 0.0 and less than 1.0.
For example,
Math.max(2, 3) returns 3
Math.max(2.5, 3) returns 3.0
Math.min(2.5, 4.6) returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
For example,
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24037034920393
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(4.5, 2.5)
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(2.6) return 3.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 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.
Java provides support for generating random numbers primarily through the
java.lang.Math and java.util.Random classes.
Random Numbers Using the Math Class
Java provides the Math class in the java.util package to generate
random numbers.
The Math class contains the static Math.random() method to generate
random numbers of the double type
The random() method returns a double value with a positive sign,
greater than or equal to 0.0 and less than 1.0.
You can use the Math.random() method with or without passing
parameters. If you provide parameters, the method produces random
numbers within the given parameters.
The code to use the Math.random() method:
public class RandomTest1 {
public static void main(String[] args) {
double x=Math.random();
System.out.println(x);
Random Numbers Within a Given Range
For generating random numbers between a given a range, you need to
specify the range.
A standard expression for accomplishing this is:
(Math.random()*((max-min)+1))+min
Random Double Within a Given Range
//doublel value between 5 and 10
public class RandomTest2 {
public static void main(String[] args) {
double x=(Math.random()*((10-5)+1))+5;
System.out.println(x);
}
}
Random Integer Within a Given Range
• The code to generate a random integer value between a specified range is this.
//int value between 2 and 6
public class RandomTest2 {
public static void main(String[] args) {
int x = (int) (Math.random() * ((6 - 2) + 1)) + 2;
System.out.println(x);
}
}
Random Number Generation Using the Random Class
You can use the java.util.Random class to generate random numbers of different types,
such as int, float, double, long, and boolean.
To generate random numbers, first, create an instance of the Random class and then call
one of the random value generator methods, such as nextInt(), nextDouble(), or
nextLong().
The code to use the nextInt() method is this.
The nextInt() method of Random accepts a bound integer and returns a random integer from 0
(inclusive) to the specified bound (exclusive).
//generate a random number from 0 to upperbound-1
import java.util.Random;
public class GenerateRandomInt {
public static void main(String[] args) {
Random rdn=new Random();
int upperRange=10;
System.out.println(rdn.nextInt(upperRange));
}
}
The nextFloat() and nextDouble() methods allow generating float and double values between 0.0
and 1.0.
The code to use both the methods is:
import java.util.Random;
public class GenerateRandomDouble {
public static void main(String[] args) {
Random rdn=new Random();
System.out.println(rdn.nextDouble());
System.out.println(rdn.nextFloat());
}
Q.Why does the Math class not need to be imported?
Math class allows the use of many common mathematical functions that can be used while
creating programs. Since it is in the java. lang package, the Math class does not need to be
imported.
The Math class is the java.lang package. Any class in the java.lang package is automatically
imported. So there is no need to import it explicitly.
Math class is static class.
A static method can be invoked without the need for creating an instance of a class.
Importing methods in the Java library
importing all classes in a package
• Some complex Java program may use many different methods contained in many
different classes in the same package
It would be a pain to write a long list of import clauses
Example:
import java.lang.Math;
import java.lang.Double;
import java.lang.Integer; ...
import java.util.Scanner;
import java.util.Stack; ...
There is a short hand to import all classes contained inside a package:
import java.lang.* ; // import all classes in java.lang package import java.util.* ; //
import all classes in java.util package
Evaluate the following method calls:
(a) Math.sqrt(4)
(b) Math.sin(2 * Math.PI)
(c) Math.cos(2 * Math.PI)
(d) Math.pow(2, 2)
(e) Math.log(Math.E)
(f) Math.exp(1)
(g) Math.max(2, Math.min(3, 4))
(h) Math.rint(-2.5)
(i) Math.ceil(-2.5)
(j) Math.floor(-2.5)
(k) Math.round(-2.5f)
(l) Math.round(-2.5)
(m) Math.rint(2.5)
(n) Math.ceil(2.5)
(o) Math.floor(2.5)
(p) Math.round(2.5f)
(q) Math.round(2.5)
(r) Math.round(Math.abs(-2.5))
Write an expression that obtains a random integer between 34 and 55.
Write an expression that obtains a random integer between 0 and 999.
Write an expression that obtains a random number between 5.5 and 55.5.
4.6 Why does the Math class not need to be imported?