Title: Math Class
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Math Class

The Math class in the java.lang package is pretty straight forward. So I'm mostly going to ask that you read

Sun Certified Programmer for Java 2 : Chapter 10

I will point out a few things:

All of the classes in Math are static --- this means you can just call the methods without creating an instance of the Math class first.

double Math.random( ) - this is very handy to know about because it returns numbers
>= 0 and < 1.0
This means that you can easily create random numbers between 0 and some Max. Let's say you want numbers between 0 and 100, then you would create an expression something like:
x = 101 * Math.random( );

Note that 101 will never be created because Math.random( ) never returns 1.00

abs, min, and max

There are many overloaded methods for routines like:
abs, min, and max. You might want to review how methods are chosen by going over the overrloading discussion in the oop section.

ceil, floor, and Round

double Math.ceil( double inVal) - returns the next larger whole number

double Math.floor(double inVal) - returns the next smaller whole number

long Math.Round (double inVal) - Rounds a double to the nearest integer.


  // For example:
  double outVal, inVal;
  long outLong;

  outVal =  Math.ceil(13.12);  // outVal now = 14.0
  outVal =  Math.floor(13.12); // outVal now = 13.0
  outLong = Math.Round(13.12); // outLong now = 13
  outLong = Math.Round(13.5);  // outLong now = 14


Last Updated: September 2, 2002 4:34 PM