Math Class Java API Examples
The Java API (Application Programming Interface) provides classes defining programming objects. One class is called Math and defines the single objectg representing Math. Since it represents a single object the class is designed such you can use it without instantiating an object. Here are some examples of using the Math class.
public class MathAPIBasicExamples
{
public static void main(String[] args) {
double aNumber = -191.635;
double smallNumber = 1.635;
double bigNumber = 123.499;
/*-------------------------------------------------------
Math.abs
Returns the absolute value of a double,
float or int value.
-------------------------------------------------------*/
System.out.println( "The absolute value of\t"
+ aNumber + "\t\tis\t"
+ Math.abs(aNumber));
/*-------------------------------------------------------
Math.ceil
Returns the smallest (closest to negative infinity)
double value that is not less than the argument
and is equal to a mathematical integer.
-------------------------------------------------------*/
System.out.println( "The ceiling of\t\t"
+ aNumber + "\t\tis\t"
+ Math.ceil(aNumber));
/*-------------------------------------------------------
Math.floor
Returns the largest (closest to positive infinity)
double value that is not greater than the argument
and is equal to a mathematical integer.
-------------------------------------------------------*/
System.out.println( "The floor of\t\t"
+ aNumber + "\t\tis\t"
+ Math.floor(aNumber));
/*-------------------------------------------------------
Math.rint
Returns the double value that is closest in value
to the argument and is equal to a mathematical integer.
-------------------------------------------------------*/
System.out.println( "The rint of\t\t"
+ aNumber + "\t\tis\t"
+ Math.rint(aNumber));
/*-------------------------------------------------------
Math.round
Returns the closest long to a double argument or
the closest int to a float argument.
-------------------------------------------------------*/
System.out.println( "The round of\t\t"
+ aNumber + "\t\tis\t"
+ Math.round(aNumber));
System.out.println( "The round of\t\t"
+ bigNumber + "\t\t\tis\t"
+ Math.round(bigNumber));
/*-------------------------------------------------------
Math.max
Returns the greater of two int, two long, two
double or two float values.
-------------------------------------------------------*/
System.out.println( "Max of \t\t\t"
+ smallNumber + " and " + bigNumber + "\tis\t"
+ Math.max(bigNumber,smallNumber));
/*-------------------------------------------------------
Math.min
Returns the smaller of two int, two long, two
double or two float values.
-------------------------------------------------------*/
System.out.println( "Min of \t\t\t"
+ smallNumber + " and " + bigNumber + "\tis\t"
+ Math.min(bigNumber,smallNumber));
/*-------------------------------------------------------
Math.pow
Returns the value of the first argument as double
raised to the power of the second argument as double.
-------------------------------------------------------*/
System.out.println( "Pow of \t\t\t"
+ "2" + " to " + "8" + "\t\t\tis\t"
+ Math.pow(2,8));
System.out.println( "Pow of \t\t\t"
+ "2" + " to " + "16" + "\t\t\tis\t"
+ Math.pow(2,16));
System.out.println( "Pow of \t\t\t"
+ "2" + " to " + "32" + "\t\t\tis\t"
+ Math.pow(2,32));
/*-------------------------------------------------------
Math.random
Returns a double value with a positive sign,
greater than or equal to 0.0 and less than 1.0.
-------------------------------------------------------*/
System.out.println( "Random \t\t\t\t\t\tis\t"
+ Math.random());
/*-------------------------------------------------------
Math.random die roll number 1 to 6
Returns a double value with a positive sign,
greater than or equal to 0.0 and less than 1.0.
-------------------------------------------------------*/
System.out.println( "Random die values 1-6\t\t\t\tis\t"
+ ((int)(Math.random()*6) + 1));
}
}