• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

Java has had several advanced usage application including working with complex calculations in physics, architecture/designing of structures, working with Maps and corresponding latitudes/longitudes etc.

In this tutorial, you will learn:

All such applications require using complex calculations/equations that are tedious to perform manually. Programmatically, such calculations, would involve usage of logarithms, trigonometry, exponential equations etc.

Java Math Class Tutorial

Now, you cannot have all the log or trigonometry tables hard-coded somewhere in your application or data. The data would be enormous and complex to maintain.

Java language provides a very useful class for this purpose. It is the Math java class (java.lang.Math).

This class provides methods for performing the operations like exponential, logarithm, roots and trigonometric equations too.

Let us have a look at the methods provided by the Java Math class.

The 2 most fundamental elements in Math are the 'e' (base of the natural logarithm) and 'pi' (ratio of the circumference of a circle to its diameter). These 2 constants are often required in the above calculations/operations.

Hence the Math class java provides these 2 constants as double fields.

Math.E - having a value as 2.718281828459045

Math.PI - having a value as 3.141592653589793

A) Let us have a look at the table below that shows us the Basic methods and its description

Method Description Arguments
abs Returns the absolute value of the argument Double, float, int, long
round Returns the closed int or long (as per the argument) double or float
ceil Returns the smallest integer that is greater than or equal to the argument Double
floor Returns the largest integer that is less than or equal to the argument Double
min Returns the smallest of the two arguments Double, float, int, long
max Returns the largest of the two arguments Double, float, int, long

Below is the code implementation for the above methods:

Note: There is no need to explicitly import java.lang.Math as its imported implicitly. All its methods are static

Integer Variable

int i1 = 27;
int i2 = -45;

Double(decimal) variables

double d1 = 84.6;
double d2 = 0.45;

Math.abs

System.out.println("Absolute value of i1: " + Math.abs(i1));
Absolute value of i1: 27

System.out.println("Absolute value of i2: " + Math.abs(i2));
Absolute value of i2: 45

System.out.println("Absolute value of d1: " + Math.abs(d1));
Absolute value of d1: 84.6

System.out.println("Absolute value of d2: " + Math.abs(d2));
Absolute value of d2: 0.45

Math.round

System.out.println("Round off for d1: " + Math.round java(d1));
Round off for d1: 85

System.out.println("Round off for d2: " + Math.round java(d2));
Round off for d2: 0

Math.ceil & Math.floor

System.out.println("Ceiling of '"+ d1 + "' = " + Math.ceil(d1));
Ceiling of '84.6' = 85.0

System.out.println("Floor of '"+ d1 + "' = " + Math.floor(d1));
Floor of '84.6' = 84.0

System.out.println("Ceiling of '"+ d2 + "' = " + Math.ceil(d2));
Ceiling of '0.45' = 1.0

System.out.println("Floor of '"+ d2 + "' = " + Math.floor(d2));
Floor of '0.45' = 0.0

Math.min

System.out.println("Minimum out of '"+ i1 +"' and '" + i2+"' = "+ Math.min(i1, i2));
Minimum out of '27' and '-45' = -45

System.out.println("Maximum out of '"+ i1 +"' and '" + i2+"' = "+ Math.max(i1, i2));
Maximum out of '27' and '-45' = 27

System.out.println("Minimum out of '"+ d1 +"' and '" + d2+"' = "+ Math.min(d1, d2));
Minimum out of '84.6' and '0.45' = 0.45

System.out.println("Maximum out of '"+ d1 +"' and '" + d2+"' = "+ Math.max(d1, d2));
Maximum out of '84.6' and '0.45' = 84.6

B) Let us have a look at the table below that shows us the Exponential and Logarithmic methods and its description-

Method Description Arguments
exp Returns the base of natural log (e) to the power of argument Double
Log Returns the natural log of the argument double
Pow Takes 2 arguments as input and returns the value of the first argument raised to the power of the second argument Double
floor Returns the largest integer that is less than or equal to the argument Double
Sqrt Returns the square root of the argument Double

Below is the code implementation for the above methods: (The same variables are used as above)

System.out.println("exp(" + d2 + ") = " + Math.exp(d2));
exp(0.45) = 1.5683121854901687

System.out.println("log(" + d2 + ") = " + Math.log(d2));
log(0.45) = -0.7985076962177716

System.out.println("pow(5, 3) = " + Math.pow(5.0, 3.0));
pow(5, 3) = 125.0

System.out.println("sqrt(16) = " + Math.sqrt(16));
sqrt(16) = 4.0

C) Let us have a look at the table below that shows us the Trigonometric methods and its description-

Method Description Arguments
Sin Returns the Sine of the specified argument Double
Cos Returns the Cosine of the specified argument double
Tan Returns the Tangent of the specified argument Double
Atan2 Converts rectangular co-ordinates (x, y) to polar(r, theta) and returns theta Double
toDegrees Converts the arguments to degrees Double
Sqrt Returns the square root of the argument Double
toRadians Converts the arguments to radians Double

Default Arguments are in Radians

Below is the code implementation:

double angle_30 = 30.0;
double radian_30 = Math.toRadians(angle_30);

System.out.println("sin(30) = " + Math.sin(radian_30));
sin(30) = 0.49999999999999994

System.out.println("cos(30) = " + Math.cos(radian_30));
cos(30) = 0.8660254037844387

System.out.println("tan(30) = " + Math.tan(radian_30));
tan(30) = 0.5773502691896257

System.out.println("Theta = " + Math.atan2(4, 2));
Theta = 1.1071487177940904

Now, with the above, you can also design your own scientific calculator - java program.

Generating Random Number Using Java

Well, Java does provide some interesting ways to generate java random numbers, not only for gambling but also several other applications esp. related to gaming security, math's etc.

Let's see how it's done!!There are basically two ways to do it-

  • Using Randomclass (in package java.util).
  • Using Math.random java class (however this will generate double in the range of 0.0 to 1.0 and not integers).

Lets look at respective JAVA Programs one by one -

Example: Using Java Random Class

First we will see the implementation using java.util.Random-Assume we need to generate 10 random numbers between 0 to 100.

import java.util.Random;
public class RandomNumbers{
        public static void main(String[] args) {
        	Random objGenerator = new Random();
            for (int iCount = 0; iCount< 10; iCount++){
              int randomNumber = objGenerator.nextInt(100);
              System.out.println("Random No : " + randomNumber); 
             }
     }
}

An object of Random class is initialized as objGenerator. The Random class has a method as nextInt. This will provide a random number based on the argument specified as the upper limit, whereas it takes lower limit is 0.Thus, we get 10 random numbers displayed.

Example: Using Java Math.Random

Now, if we want 10 random numbers generated in java program but in the range of 0.0 to 1.0, then we should make use of math.random()

You can use the following loop to generate them-

public class DemoRandom{
  public static void main(String[] args) {
    for(int xCount = 0; xCount< 10; xCount++){
      System.out.println(Math.random());
    }
  }
}

Now, you know how programmers generates these strange numbers!

 

YOU MIGHT LIKE: