How do I calculate cube root and square root of a number?

To calculate the cube root and the square root of a double value we can use the Math.cbrt(double a) and Math.sqrt(double a) static method call.

package org.kodejava.math;

public class CubeSquareRootExample {

    public static void main(String[] args) {
        double cube = 125.0d;
        double square = 100.0d;

        // Get the cube root of double value
        double cubeRoot = Math.cbrt(cube);
        System.out.println("Cube root of " + cube + " is " + cubeRoot);

        // Get the square root of double value
        double squareRoot = Math.sqrt(square);
        System.out.println("Square root of " + square + " is " + squareRoot);
    }
}

This snippet will print the following output:

Cube root of 125.0 is 5.0
Square root of 100.0 is 10.0

How do I use trigonometric calculation methods?

This example demonstrates how to use the trigonometric methods of the java.lang.Math class. You can see the use of method such as Math.sin(), Math.cos(), Math.tan(), etc.

package org.kodejava.math;

public class TrigonometricExample {
    public static void main(String[] args) {
        double radians = 1.0d;

        double sine = Math.sin(radians);
        double cosine = Math.cos(radians);
        double tan = Math.tan(radians);

        double asine = Math.asin(sine);
        double acosine = Math.acos(cosine);
        double atan = Math.atan(tan);

        System.out.println("Sine of " + radians + " = " + sine);
        System.out.println("Cosine of " + radians + " = " + cosine);
        System.out.println("Tangent of " + radians + " = " + tan);
        System.out.println("Arcsine of " + sine + " = " + asine);
        System.out.println("Arccosine of " + cosine + " = " + acosine);
        System.out.println("Arctangent of " + tan + " = " + atan);
    }
}

The output of the program are:

Sine of 1.0 = 0.8414709848078965
Cosine of 1.0 = 0.5403023058681398
Tangent of 1.0 = 1.5574077246549023
Arcsine of 0.8414709848078965 = 1.0
Arccosine of 0.5403023058681398 = 1.0
Arctangent of 1.5574077246549023 = 1.0

How do I convert angle from radians to degrees?

The example below show you how to convert an angle measured in radians into degrees and vice versa. We can use the Math.toDegrees() and Math.toRadians() method call to do the conversion.

package org.kodejava.math;

public class RadiansDegreeConversionExample {
    public static void main(String[] args) {
        double radians = 1.0d;
        double degrees = 45d;

        // Converts an angle measured in radians to an
        // approximately equivalent angle measured in
        // degrees.
        double toDegree = Math.toDegrees(radians);

        // Converts an angle measured in degrees to an
        // approximately equivalent angle measured in
        // radians.
        double toRadians = Math.toRadians(degrees);

        System.out.println("Radians " + radians + " in degrees  = " + toDegree);
        System.out.println("Degrees " + degrees + " in radians = " + toRadians);
    }
}

The result of the snippet above are:

Radians 1.0 in degrees  = 57.29577951308232
Degrees 45.0 in radians = 0.7853981633974483

How do I get the PI value?

The number π is a mathematical constant, the ratio of a circle’s circumference to its diameter, commonly approximated as 3.14159. It has been represented by the Greek letter “π” since the mid-18th century, though it is also sometimes spelled out as “pi” (/paɪ/).

From: Wikipedia

The code snippet below show you how to obtain the PI value in Java. We use the Math.PI static field to get the value of PI.

package org.kodejava.math;

public class GetPiExample {
    public static void main(String[] args) {
        // The PI value represented by Math.PI
        System.out.println("PI = " + Math.PI);

        // Using the Math.PI to calculate area of a circle.
        double radius = 8;
        double circleArea = Math.PI * Math.pow(radius, 2);
        System.out.println("Circle Area = " + circleArea);
    }
}

Here is the program output:

PI = 3.141592653589793
Circle Area = 201.06192982974676

How do I get the minimum or maximum value between two numbers?

The code below show you how to use the Math.min() and Math.max() method call the get the minimum and maximum value between two numbers. As other method in the Math class these methods also overloaded to accept many types of primitive data.

package org.kodejava.math;

public class GetMinMaxValueExample {
    public static void main(String[] args) {

        Double value1 = 100.0D;
        Double value2 = 200.0D;

        double max1 = Math.max(value1, value2);
        double min1 = Math.min(value1, value2);

        float max2 = Math.max(value1.floatValue(), value2.floatValue());
        float min2 = Math.min(value1.floatValue(), value2.floatValue());

        int max3 = Math.max(value1.intValue(), value2.intValue());
        int min3 = Math.min(value1.intValue(), value2.intValue());

        long max4 = Math.max(value1.longValue(), value2.longValue());
        long min4 = Math.min(value1.longValue(), value2.longValue());

        System.out.println("Max value in double: " + max1);
        System.out.println("Min value in double: " + min1);
        System.out.println("Max value in float : " + max2);
        System.out.println("Min value in float : " + min2);
        System.out.println("Max value in int   : " + max3);
        System.out.println("Min value in int   : " + min3);
        System.out.println("Max value in long  : " + max4);
        System.out.println("Min value in long  : " + min4);
    }
}

The result of the above program are:

Max value in double: 200.0
Min value in double: 100.0
Max value in float : 200.0
Min value in float : 100.0
Max value in int   : 200
Min value in int   : 100
Max value in long  : 200
Min value in long  : 100