## Thursday, March 5, 2009

### Math class

Math class:
The Math class serves as a grouping of mathematical functions and constants.
It is interesting to note that all the variables and methods in Math are static, and the Math class itself is final. This means you can’t derive new classes from Math. Additionally, you can’t instantiate the Math class. It’s best to think of the Math class as just a conglomeration of methods and constants for performing mathematical computations.
The Math class includes the E and PI constants, methods for determining the absolute value of a number, methods for calculating trigonometric functions, and minimum and maximum methods, among others.

usually used Methods:
1.static double sin(double arg)
returns the sine value of the arg.arg is in radius.

2.static double cos(double arg)
returns the cosine value of the arg.arg is in radius.

3.static double tan(double arg)
return the tangent value of the arg .arg is in radius.

4.static double log(double arg)
return the log value of the arg.

5.static double pow(double x,double y)
return x to the power of n value

6.static double sqrt(double arg)
return the square root of the arg.

7.static double abs(double arg)
return the absolute value of arg.

8.static double ceil(double arg)
return the smallest integer which is greater or equal to arg.
ex: Math.ceil(4.5) is 5.0

9.static double floor(double arg)
return the greatest integer which is lower or equal to arg.
Ex: Math.floor(4.5) is 4.

10.static double min(arg1,arg2)
return the minimum of the arg1 and areg2.

11.static double Max(arg1,arg2)
return the maximum of the arg1,arg2.

12.static long round(arg)
return the rounded value of arg.
Ex: Math.round(4.6) is 5.
Math.round(4.3) is 4.

13.static double random()
return random number between 0 and 1.

converts angle in degrees into radians.

Maths class Examples

1.Example code Math class using the above methods.

`package myutil;public class MathDemo {  public static void main(String arg[])  {      float f=6.5f;      int i=-5;      System.out.println("ciel value"+Math.ceil(f));      System.out.println("floor value"+Math.floor(f));      System.out.println("round value"+Math.round(f));      System.out.println("absoult value"+Math.abs(i));      System.out.println("sqire root value"+Math.sqrt(f));      System.out.println("poweer value"+Math.pow(i,2));     }}output:ciel value7.0floor value6.0round value7absoult value5sqire root value2.5495097567963922poweer value25.0`

2.Example code to Generate Random numbers.

`package myutil;public class RandomDemo{public static void main(String arg[])throws Exception{while(true){double d=10*Math.random();int n=(int)d;System.out.println(n);if(n==0)System.exit(0);Thread.sleep(3000);}}}output:53923650`

3.Example on more methods:

`package myutil;public class MathExample{public static void main(String args[]) {char temp = (char)-1;String input = "";Double data = null;System.out.println("Enter any number");/** Gets the user input**/try {do {temp = (char)System.in.read();input = input + temp;}while(temp != '\n');data = new Double(input);}catch(Exception err){System.out.println("Exception ...");System.exit(0);}double d_data = data.doubleValue();System.out.println("Printing Math values......");    System.out.println("Sin : " + (Math.sin(d_data)));    System.out.println("Cos : " + (Math.cos(d_data)));System.out.println("Tan : " + (Math.tan(d_data)));System.out.println("asin : " + (Math.asin(d_data)));System.out.println("acos : " + (Math.acos(d_data)));System.out.println("atan : " + (Math.atan(d_data)));System.out.println("Abs : " + (Math.abs(d_data)));System.out.println("Exp : " + (Math.exp(d_data)));System.out.println("Log : " + (Math.log(d_data)));System.out.println("Sqrt : " + (Math.sqrt(d_data)));System.out.println("Ceil : " + (Math.ceil(d_data)));    System.out.println("Floor : " + (Math.floor(d_data)));System.out.println("rint : " + (Math.rint(d_data)));System.out.println("round : " + (Math.round(d_data)));System.out.println("Random Number : " + (Math.random()));}}output:Enter any number100Printing Math values......Sin : -0.5063656411097588Cos : 0.8623188722876839Tan : -0.5872139151569291asin : NaNacos : NaNatan : 1.5607966601082315Abs : 100.0Exp : 2.6881171418161356E43Log : 4.605170185988092Sqrt : 10.0Ceil : 100.0Floor : 100.0rint : 100.0round : 100Random Number : 0.6755771329201157`