iTechtalk


If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.



Reply
Old 10-06-2008, 12:53 PM   #1 (permalink)
Administrator
 
Join Date: Aug 2008
Posts: 60
Post Package-java.lang.Math

Package-java.lang.Math



Another class you will refer to often, especially when programming games, is the Math class. Rather than instantiating this class, you will desire to use its static methods to perform many different mathematical operations.

The Math class gives two handy constant values for use in your programs: the natural logarithm base e and the value of pi—the ratio of the circumference of a circle to its diameter. The following demonstrates how you can use pi to calculate the area of a circle:

double radius = 4.5;
double area = Math.PI * radius * radius;
System.out.println("The area of a circle with radius " + radius + " is " + area);


The output would be

The area of a circle with radius 4.5 is 63.61725123519331

Other utilities found within the Math class consist of basic trigonometry and degree conversion methods. This contains the sine, arcsine, cosine, arccosine, tangent, and arctangent of an angular measurement in radians. You can also rapidly convert a degree measurement to radians and vice versa using the toRadians, and toDegrees methods, respectively:

double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.println(degrees + " degrees = " + radians + " radians");
System.out.println("So, " + radians + " radians = " +Math.toDegrees(radians) + "degrees!");


This will output

45.0 degrees = 0.7853981633974483 radians
So, 0.7853981633974483 radians = 45.0 degrees!


Maybe that wasn’t the most interesting example, but it is a useful construct nonetheless. A possibly more interesting method found in the Math class is the random method. There is no such thing as a truly random number on digital computers since on computers random numbers are computed algorithmically with deterministic algorithms and their sequences can be predicted and reproduced. However, the way in which Java pseudorandomly calculates random numbers is sufficient for your requirements.

The Math random method creates a double value greater than or equal to 0.0 and less than 1.0. Its magnitude is always positive, and the values it chooses usually fall within a uniform distribution of that range. The following code snippet creates 25 Integer objects whose values range between 50 and 100:

Integer[] values = new Integer[20];
for(int i = 0; i < values.length; i++)
{
values[i] = new Integer((int)(50.0 + (Math.random()*51.0)));
}
System.out.println(Arrays.asList(values));


If you feel that the code that chooses the random value looks too congested, feel free to break it up into several steps. Output for this code may look like the following:

[87, 65, 99, 67, 62, 65, 95, 69, 96, 92, 50, 98, 53, 100, 69, 51, 95, 54, 76, 55]

There are also a number of other Math routines, summarized by the following:

1) Math.abs: Returns the absolute value of the sent number.

2) Math.ceil: Returns the ceiling of the sent double value.

3) Math.exp: Returns the value of e raised to the sent double value. This is equivalent to System.out.println(Math.pow(Math.E, value));

4) Math.floor: Returns the floor value of the sent double.

5) Math.max: Returns the greater of the two sent parameters.

6) Math.min: Returns the lesser of the two sent parameters.

7) Math.pow: Returns the first value raised to the power of the second.

8) Math.round: For float types, returns the closest int value. For doubles, returns the closest long value.

9) Math.sqrt: Returns the rounded positive square root of the sent double value.
prashant is offline   Reply With Quote
Reply

Bookmarks

Tags
java, java package, java.lang.math, package

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:04 AM. itechtalk