Mathematical Functions - cont
Let’s try out an example now in order to make sure that we know how to use the contents of the Math class.
Type out the following program, which will calculate the radius of a circle in feet and inches, given that it has an area of 100 square feet.
public class MathCalculation{
public static void main(String[ ] args){
double radius = 0.0;
double circleArea = 100.0;
int feet = 0; int inches = 0;
radius = Math.sqrt(circleArea/Math.PI);
// Get the feet as a whole number
feet = (int)Math.floor(radius);
// Calculate the number of inches
inches = (int)Math.round(12.0 * (radius – feet));
System.out.println(“The radius of a circle with area “ + circleArea + “ square feet is\n” + feet + “ feet “ + inches + “ inches”);
}
}
Save the program as MathCalculation.java, compile and run it. You should get the following output:
The radius of a circle with area 100 square feet is
5 feet 8 inches
The first calculation we need, after defining the variables uses the sqrt( ) method to calculate the radius.
Since the area of a circle, with radius r, is given by the formula pir², the radius must be sqrt(area/pi), and we specify the argument to the sqrt( ) method as the expression circleArea/Math.PI. The result is in feet as double value. To get the number of the whole feet we use the floor( ) method. Note that cast to int is necessary, otherwise you will get an error from the compiler.
Lastly we get the number of inches by subtracting the value of whole feet from the original radius, multiplying the fraction of foot by 12 to get the equivalent inches, and then rounding the result to the nearest integer using the round( ) method.
Note how we output the result. We did encounter the System.out.println in our “Hello World” application, but we didn’t explain exactly how it works, so let’s do it now. System is the name of a standard class that contains variables and methods for supporting simple keyboard input and character output to the display. It is contained in the package java.lang so it is always accessible just by using the simple class name, System.
The object out represents the standard output stream – your display screen, and is data member of the class System. This member is referenced by using the class name System separated from the member name out by a period – System.out . The bit at the rightmost end, println(…. ), calls the println( ) method of the object out. This method outputs the text string that appears between the parentheses to your display.
You might be wondering what is the + operator doing here, it’s not arithmetic we are doing, is it? No, but the plus has a special effect when used with character strings: it joins them together (concatenation). Neither circleArea, feet or inches are of type String, but they are all converted to a character string to be compatible with “The radius of a circle with area “.
Exercise 3 – 6 Using math functions
Create an application that gets a random number between 0 and 1000, finds the square root of the number, the log of the number, the sin, cos, and tan of the number and prints them (with suitable text) to the screen. To generate the random number, use the Random class in the java.util package. If your not sure of how these are generated, look up this class and it’s methods in the online documentation.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment