Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Test Math

Problem : Describe java.lang.Math class methods.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestMath.java  and copy below contents.


/*

 * Test Math methods

 */

public class TestMath {

    public static void main(String[] args) {

        System.out.println("Math functions");
       
        System.out.println(" Max 2,5 - " + Math.max(2,5));
        System.out.println(" Min 2,5 - " + Math.min(2,5));
        System.out.println(" Absolute 3.7 - " + Math.abs(3.7));
        System.out.println(" Exp 10 - " + Math.exp(10));
        System.out.println(" Random Number- " + Math.random());
        System.out.println(" Square Root- " + Math.sqrt(4));
       
       
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestMath.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestMath.java with help of 
    • javac TestMath.java command.Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestMath command.

Output


Math functions
 Max 2,5 - 5
 Min 2,5 - 2
 Absolute 3.7 - 3.7
 Exp 10 - 22026.465794806718
 Random Number- 0.30636837900792924
 Square Root- 2.0


FAQ

What is Math class?

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

Important points
  1.  All method of this class are static. 
  2. This is final class
  3. Since its default constructor is private so no other class can create instance of this class


<<Previous | Next>>
ċ
TestMath.java
(1k)
nakul shukla,
Mar 19, 2009, 7:48 AM
Comments