Share & learn

Through Innovative Digital Library

What is Math class in Java?

Explain about Math class in Java.
 



Answers

Posted by Paul on April 26th 2018 13:28 PM

Math class in Java : For performing basic mathematical operations, Java has provided java.lang.Math class. This class contains the basic methods needed for performing basic numeric operations. e.g. of numeric operations that can be performed using java.lang.Math class : elementary exponential, logarithm, square root and trigonometric functions Class Declaration : Here is the declaration of Math class : public final class Math extends Object Methods of Math class : 1. abs() : This method will return the absolute values of passed arguments. 2. cos() : This method will return the cosine of a value. 3. sin() : This method will return the sine of a value. 4. tan() : This method will return the tangent of a value. 5. cbrt : This method will return the cube root of a passed value. 6. ceil() : This method will return the smallest ( i.e closest to negative infinity ) value which is greater than or equal to the argument and is equal to a mathematical integer. 7. floor() : This method will return the largest ( i.e. closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer. 8. max() : This method will return the maximum number of two values. 9. min() : This method will return the minimum of two values. 10. exp() : This method will return the base of natural log (e) to the power of argument 11. Log() : This method will return the natural log of the argument 12. Pow() : This method will takes 2 arguments as input and returns the value of the first argument raised to the power of the second argument 13. Sqrt() : This method will return the square root of the argument Here is code to implement these methods :

//Java program to implement basic functionalities
//of Math claass
public class MathClassDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
int num1 54;
        
int num2 = -25;
        
double num3 34.6;
        
float num4 12.45f;

        
// calculate absolute values
        
System.out.println("Absolute value of " num1 " : " Math.abs(num1));
        
System.out.println("Absolute value of " num2 " : " Math.abs(num2));
        
System.out.println("Absolute value of " num3 " : " Math.abs(num3));
        
System.out.println("Absolute value of " num4 " : " Math.abs(num4));

        
// calculate ceil and floor
        
double num5 22.34;
        
double num6 0.98;

        
System.out.println("\n\nCeil value of " num5 " : " Math.ceil(num5));
        
System.out.println("Floor value of " num5 " : " Math.floor(num5));
        
System.out.println("Ceil value of " num6 " : " Math.ceil(num6));
        
System.out.println("Floor value of " num6 " : " Math.floor(num6));

        
// find min and max
        
System.out.println("\n\nMinimum out of " num1 " and " num2 " : " Math.min(num1num2));
        
System.out.println("Maximum out of " num1 " and " num2 " : " Math.max(num1num2));
        
System.out.println("Minimum out of " num5 " and " num6 " : " Math.min(num5num6));
        
System.out.println("Maximum out of " num5 " and " num6 " : " Math.max(num5num6));

        
// calculate square root and cube root

        
int num7 64;
        
int num8 125;
        
System.out.println("\n\nSquare Root of " num7 " is : " Math.sqrt(num7));
        
System.out.println("Cube Root of " num8 " is : " Math.cbrt(num7));

    }

}

Posted by Paul on April 26th 2018 13:29 PM

// Java program to implement basic functionalities
// of Math claass
public class MathClassDemo2 {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        
double num1 34.6;
        
double num2 0.77;

        
// calculate exp, log and power
        
System.out.println("exp(" num1 ") = " Math.exp(num1));
        
System.out.println("log(" num2 ") = " Math.log(num2));
        
System.out.println("pow(2, 3) = " Math.pow(2.03.0));


    }

}

Posted by Paul on April 26th 2018 13:30 PM

public class MathClassDemo3 {

    public static 
void main(String[] args) {
        
        
// TODO Auto-generated method stub
        
        
double angle_45 45.0;
        
double radian_45 Math.toRadians(angle_45);

        
// calculate sin, cos and tan
        
System.out.println("sin(45) = " Math.sin(radian_45));
        
System.out.println("cos(45) = " Math.cos(radian_45));
        
System.out.println("tan(45) = " Math.tan(radian_45));


    }

}
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
UJJH

related questions

Most liked questions

Most answered questions

Login
UJJH Refresh
Register
UJJH Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
UJJH Refresh