Math Class

Java's Math class contains a collection of methods, and two constants, that support mathematical computation. The class is final, so you cannot extend it. The constructor is private, so you cannot create an instance. Fortunately, the methods and constants are static, so they can be accessed through the class name without having to construct a Math object.

The two constants of the Math class are:

There are a bunch of methods supporting trigonometric operations, rounding operations, absolute value, etc.


Math Methods In Action: Here's a sample program showing a couple of the math methods in action:


Circle Operations: The following sample applet demonstrates a couple of things. First of all, a class called circle is created. The applet class, called circ, contains an instance of this class. You will notice the line

c=new circle(9);

This line calls the constructor of the circle class. From then on the variables and methods in the circle class can be used quite easily. For instance, c.radius produces the radius and c.diameter() returns the diameter. Notice the difference between how variables and methods are accessed (variables don't have parenthesis).

You should realize that when you compile the following file, two class files will be created.



- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Here's another example applet which might be interesting to look at:

FINANCIAL PLANNING - COMPOUND INTEREST APPLET
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Assignment: The Pythagorean Theorem works something like this: If you know the the lengths of the two lines adjacent to the right angle of a right triangle, then you can calculate its hypotenuse using the following formula:

    hypotenuse=square_root(side1*side1+side2*side2);
For this assignment you will create a class called rtTri which represents a right triangle. You will create a second class which uses the rtTri class to find the hypotenuse of a right triangle from two numbers supplied by the user to the applet. Be sure to use relevant methods from the Math class to help you with this program.