JAVA BEGINNERS TUTORIAL – Dealing With Numbers 3

this how I review basics of a language to write what I reviewed in blog posts also it will be good interance for Android development. the good news now it become dialy as I need to review also other languages before release some open source case studies this blog post is the last one of numbers series lets view some methods.

min()

The method gives the smaller of the two arguments. The argument can be int, float, long, double.

double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)
  • A primitive data types
  • This method Returns the smaller of the two arguments.

Example:

public class Test{ 

   public static void main(String args[]){
      System.out.println(Math.min(12.123, 12.456));      
      System.out.println(Math.min(23.12, 23.0));  
   }
}

max()

The method gives the maximum of the two arguments. The argument can be int, float, long, double.

double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)
  • A primitive data types
  • This method Returns the maximum of the two arguments.

Example:

public class Test{ 

   public static void main(String args[]){
      System.out.println(Math.max(12.123, 12.456));      
      System.out.println(Math.max(23.12, 23.0));  
   }
}

exp()

The method gives the maximum of the two arguments. The argument can be int, float, long, double.

double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)
  • A primitive data types
  • This method Returns the maximum of the two arguments.

Example:

public class Test{ 

   public static void main(String args[]){
      System.out.println(Math.max(12.123, 12.456));      
      System.out.println(Math.max(23.12, 23.0));  
   }
}

log()

The method returns the natural logarithm of the argument.

double log(double d)
  • d — A primitive data types
  • This method Returns the natural logarithm of the argument.

Example:

public class Test{ 

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

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x));
   }
}

pow()

The method returns the value of the first argument raised to the power of the second argument.

double pow(double base, double exponent)
  • base — A primitive data type
  • exponenet — A primitive data type
  • This method Returns the value of the first argument raised to the power of the second argument.

Example:

public class Test{ 

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

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y));

   }
}

sqrt()

The method returns the square root of the argument.

double sqrt(double d)
  • d — A primitive data type
  • This method Returns the square root of the argument.

Example:

public class Test{ 

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

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
   }
}

sin()

The method returns the sine of the specified double value.

double sin(double d)
  • d — A double data types
  • This method Returns the sine 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 sine of %.1f degrees is %.4f%n",
                        degrees, Math.sin(radians));

   }
}

cos()

The method returns the cosine of the specified double value.

double cos(double d)
  • d — A double data types
  • 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));

   }
}

tan()

The method returns the tangent of the specified double value.

double tan(double d)
  • d — A double data type
  • This method Returns the tangent 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 tangent of %.1f degrees is %.4f%n",
                        degrees, Math.tan(radians));

   }
}

asin()

The method returns the arcsine of the specified double value.

double asin(double d)
  • d — A double data types
  • 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))));


   }
}

acos()

The method returns the arccosine of the specified double value.

double acos(double d)

  • d — A double data types
  • 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))));


   }
}

atan()

The method returns the arctangent of the specified double value.

double atan(double d)
  • d — A double data types
  • 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))));


   }
}

atan2()

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

double atan2(double y, double x)
  • X — X co-ordinate in double data type
  • Y — Y co-ordinate in double data type
  • 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) );
   }
}

toDegrees()

The method converts the argument value to degrees.

double toDegrees(double d)
  • d — A double data type.
  • This method returns a double value.

Example:

public class Test{ 

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

     System.out.println( Math.toDegrees(x) );
     System.out.println( Math.toDegrees(y) );
   }
}

toRadians()

The method converts the argument value to radians.

double toRadians(double d)
  • d — A double data type.
  • This method returns a double value.

Example:

public class Test{ 

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

     System.out.println( Math.toRadians(x) );
     System.out.println( Math.toRadians(y) );
   }
}

random()

The method is used to generate a random number between 0.0 and 1.0. The range is: 0.0 =< Math.random < 1.0. Different ranges can be achieved by using arithmetic.

static double random()
  • This method returns a double

Example:

public class Test{ 

   public static void main(String args[]){
     System.out.println( Math.random() );
     System.out.println( Math.random() );
   }
}

Comments on this post

No comments.

Leave a Reply

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

4 × one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL