Showing posts with label JAVA Number Class. Show all posts
Showing posts with label JAVA Number Class. Show all posts

Saturday, 24 August 2013

Java - cos() Method

Description:

The method returns the cosine of the specified double value.

Syntax:

double cos(double d)

Parameters:

Here is the detail of parameters:
  • d -- A double data types

Return Value:

  • This method Returns the cosine of the specified double value.

Example:

public class Test{ 

   public static void main(String args[]){
     double degrees = 45.0;
     double radians = Math.toRadians(degrees);

     System.out.format("The value of pi is %.4f%n", Math.PI);
     System.out.format("The cosine of %.1f degrees is %.4f%n",
                        degrees, Math.cos(radians));

   }
}
This produces following result:
The value of pi is 3.1416
The cosine of 45.0 degrees is 0.7071

Java - ceil() Method

Description:

The method ceil gives the smallest integer that is greater than or equal to the argument.

Syntax:

This method has following variants:
double ceil(double d)

double ceil(float f)

Parameters:

Here is the detail of parameters:
  • A double or float primitive data type

Return Value:

  • This method Returns the smallest integer that is greater than or equal to the argument. Returned as a double.

Example:

public class Test{ 

   public static void main(String args[]){
      double d = -100.675;
      float f = -90;    

      System.out.println(Math.ceil(d));
      System.out.println(Math.ceil(f)); 
					 
      System.out.println(Math.floor(d));
      System.out.println(Math.floor(f)); 
   }
}
This produces following result:
-100.0
-90.0
-101.0
-90.0

Java - atan2() Method

Description:

The method Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.

Syntax:

double atan2(double y, double x)

Parameters:

Here is the detail of parameters:
  • X -- X co-ordinate in double data type
  • Y -- Y co-ordinate in double data type

Return Value:

  • This method Returns theta from polar coordinate (r, theta)

Example:

public class Test{ 

   public static void main(String args[]){
     double x = 45.0;
     double y = 30.0;

     System.out.println( Math.atan2(x, y) );
   }
}
This produces following result:
0.982793723247329

Java - atan() Method

Description:

The method returns the arctangent of the specified double value.

Syntax:

double atan(double d)

Parameters:

Here is the detail of parameters:
  • d -- A double data types

Return Value :

  • This method Returns the arctangent of the specified double value.

Example:

public class Test{ 

   public static void main(String args[]){
     double degrees = 45.0;
     double radians = Math.toRadians(degrees);

     System.out.format("The value of pi is %.4f%n", Math.PI);
     System.out.format("The arctangent of %.4f is %.4f degrees %n",
                     Math.cos(radians),
                     Math.toDegrees(Math.atan(Math.sin(radians))));


   }
}
This produces following result:
The value of pi is 3.1416
The arctangent of 1.0000 is 45.0000 degrees

Java - asin() Method

Description:

The method returns the arcsine of the specified double value.

Syntax:

double asin(double d)

Parameters:

Here is the detail of parameters:
  • d -- A double data types

Return Value:

  • This method Returns the arcsine of the specified double value.

Example:

public class Test{ 

   public static void main(String args[]){
     double degrees = 45.0;
     double radians = Math.toRadians(degrees);

     System.out.format("The value of pi is %.4f%n", Math.PI);
     System.out.format("The arcsine of %.4f is %.4f degrees %n",
                     Math.sin(radians),
                     Math.toDegrees(Math.asin(Math.sin(radians))));


   }
}
This produces following result:
The value of pi is 3.1416
The arcsine of 0.7071 is 45.0000 degrees

Java - acos() Method

Description:

The method returns the arccosine of the specified double value.

Syntax:

double acos(double d)

Parameters:

Here is the detail of parameters:
  • d -- A double data types

Return Value:

  • This method Returns the arccosine of the specified double value.

Example:

public class Test{ 

   public static void main(String args[]){
     double degrees = 45.0;
     double radians = Math.toRadians(degrees);

     System.out.format("The value of pi is %.4f%n", Math.PI);
     System.out.format("The arccosine of %.4f is %.4f degrees %n",
                     Math.cos(radians),
                     Math.toDegrees(Math.acos(Math.sin(radians))));


   }
}
This produces following result:
The value of pi is 3.1416
The arccosine of 0.7071 is 45.0000 degrees

Java - abs() Method

Description:

The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

Syntax:

All the variant of this method are given below:
double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)

Parameters:

Here is the detail of parameters:
  • Any primitive data type

Return Value:

  • This method Returns the absolute value of the argument.

Example:

public class Test{ 

   public static void main(String args[]){
      Integer a = -8;
      double d = -100;
      float f = -90;    
						
      System.out.println(Math.abs(a));
      System.out.println(Math.abs(d));     
      System.out.println(Math.abs(f));    
   }
}
This produces following result:
8
100.0
90.0

Java - valueOf() Method

Description:

The valueOf method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String etc.
This method is a static method. The method can take two arguments, where one is a String and the other is a radix.

Syntax:

All the variant of this method are given below:
static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)

Parameters:

Here is the detail of parameters:
  • i -- An int for which Integer representation would be returned.
  • s -- A String for which Integer representation would be returned.
  • radix -- This would be used to decide the value of returned Integer based on passed String.

Return Value:

  • valueOf(int i): This returns an Integer object holding the value of the specified primitive.
  • valueOf(String s): This returns an Integer object holding the value of the specified string representation.
  • valueOf(String s, int radix): This returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.
public class Test{ 

   public static void main(String args[]){
      
      Integer x =Integer.valueOf(9);
      Double c = Double.valueOf(5);
      Float a = Float.valueOf("80");               

      Integer b = Integer.valueOf("444",16);

      System.out.println(x); 
      System.out.println(c);
      System.out.println(a);
      System.out.println(b);
   }
}
This produces following result:
9
5.0
80.0
1092

Java - toString() Method

Description:

The method is used to get a String object representing the value of the Number Object.
If the method takes a primitive data type as an argument then the String object representing the primitive data type value is return.
If the method takes two arguments then a String representation of the first argument in the radix specified by the second argument will be returned.

Syntax:

All the variant of this method are given below:
String toString()
static String toString(int i)

Parameters:

Here is the detail of parameters:
  • i -- An int for which string representation would be returned.

Return Value:

  • toString(): This returns a String object representing the value of this Integer.
  • toString(int i): This returns a String object representing the specified integer.

Example:

public class Test{ 

   public static void main(String args[]){
      Integer x = 5;

      System.out.println(x.toString());  
      System.out.println(Integer.toString(12)); 
   }
}
This produces following result:
5
12