Set 5: Methods of the Math Class

Skill 5.1 Use the java math class to perform mathematical operations
Skill 5.1 Concepts

The math class is a powerful class of methods for performing mathematical computations. An example of how the math class can be applied is illustrated below. The example below computes the square root of 17. The result is assigned to the double variable p because square roots typically do not result in integer values.

double p = Math.square(17);

In the above example,

Java provides an extensive library of Math operations. Below is a description of some of them. Notice that each method has a corresponding signature. The signature of a method can be interpreted as follows,

Method Signature Description
abs int abs(int x) Returns the absolute value of x
abs double abs(double x) Returns the absolute value of x
pow double pow(double d, double e) Returns d raised to the e power
sqrt double sqrt(double x) Returns the square root of x
ceil double ceil(double x) Returns next highest whole number from x
floor double floor(double x) Returns next lowest whole number from x
min double min(double a, double b) Returns the smaller of a and b
max double max(double a, double b) Returns the larger of a and b
min int min(int a, int b) Returns the smaller of a and b
max int max(int a, int b) Returns the larger of a and b
random double random() Returns a random double (range 0 < r < 1)
round long round(double x) Returns x rounded to the nearest whole number
PI double PI Returns 3.1459625…

Below are examples of each of the methods described above,

  1. double d = -379.22;
    System.out.println(Math.abs(d));//379.22

  2. double d = 42.01;
    double e = 3.728;
    System.out.println(Math.pow(a,e));//1126831.027

  3. double d = 2034.56;
    System.out.println(Math.sqrt(d));//45.10609715

  4. double d = 1.4;
    System.out.println(Math.ceil(d));//2.0

  5. double d = -1.6;
    System.out.println(Math.ceil(d));//-1.0

  6. double d = 1.4;
    System.out.println(Math.floor(d));//1.0

  7. double d = -1.6;
    System.out.println(Math.floor(d));//-2.0

  8. double d = 7.89;
    System.out.println(Math.log(d));//2.065596135 *log is base e

  9. double x = 2038.5;
    double y = -8999.0;
    System.out.println(Math.min(x, y));//-8999.0

  10. double x = 2038.5;
    double y = -8999.0;
    System.out.println(Math.max(x, y));//2038.5

  11. double x = 148.2;
    System.out.println(Math.round(x));//148

    double x = 148.7;
    System.out.println(Math.round(x));//149

    double x = -148.2;
    System.out.println(Math.round(x));//-148

    double x = -148.7;
    System.out.println(Math.round(x));//-149

  12. System.out.println(Math.PI);//3.14159265...

  13. System.out.println(Math.random());//prints a random number between 0 and 1 where 0 is inclusive, but 1 is not

Skill 5.1: Exercise 1
Skill 5.2 Apply the random() method to create a random integer in a specified range
Skill 5.2 Concepts

Many applications you will create will require a random number. For example, what if you needed to write a program to generate a number that represented a face from a 6-sided die, or a card from a 52 card deck?

The random() method in the Math class generates a random double type number between 0 and 1, where 0 is inclusive, but 1 is not. The below code is illustrative,

System.out.println(Math.random());//prints a random double from 0 up to 1

To create a number in a different range, say 0 up to 10, simply multiply the result of Math.random() by the desired range. An example is shown below,

System.out.println(Math.random()*10);//prints a random double from 0 up to 10

Recall, however that the random() method returns a double. The below code illustrates how to generate a random int from 0 up to 10,

int randomNumber = (int)(Math.random()*10);
System.out.println(randomNumber);//prints a random int from 0 up to 10

The above examples, illustrate how to scale the random() method to a specified range. The example below illustrates how to shift the range of the random number.

int randomNumber = (int)(Math.random()*10)+100;
System.out.println(randomNumber);//prints a random int from 100 up to 110

Skill 5.2: Exercise 1
Skill 5.3 Familiarize yourself with additional methods in the java Math class
Skill 5.3 Concepts
Method Signature Description
log double log(double x) Returns log base e of x
sin double sin(double a) Returns the sin of angle a in radians
cos double cos(double a) Returns the cos of angle a in radians
tan double tan(double a) Returns the tangent of angle a in radians
asin double asin(double x) Returns the arcsine of x in the range –PI/2 to PI/2
acos double acos(double x) Returns the arctan of x in the range –PI/2 to PI/2
toDegrees double toDegrees(double angRad) Converts radians to degrees
toRadians double toRadians(double angDeg) Converts degrees to radians
Skill 5.3: Exercise 1