Page 194 - Introduction to Programming with Java: A Problem Solving Approach
P. 194
160 Chapter 5 Using Pre-Built Methods
public static double acos(double ratio)
Returns the angle in radians between 0.0 and whose cosine equals the given value. public static double asin(double ratio)
Returns the angle in radians between /2 and /2 whose sine equals the given value. public static double atan(double ratio)
Returns the angle in radians between /2 and /2 whose tangent equals the given value. public static double cos(double radians)
Returns the cosine of an angle expressed in radians.
public static double sin(double radians)
Returns the sine of an angle expressed in radians.
public static double tan(double radians)
Returns the tangent of an angle expressed in radians.
public static double toDegrees(double radians)
Converts an angle measured in radians to an angle measured in degrees.
public static double toRadians(double degrees)
Converts an angle measured in degrees to an angle measured in radians.
Apago PDF Enhancer
Figure 5.4 API headings and brief descriptions of some trigonometric methods in the java.lang.Math class
Suppose you want to compute the water needed for a 10 centimeter diameter water balloon. Here’s the formula for the volume of a sphere:
6 diameter3
And here’s the code and resulting output for computing the volume of water for the water balloon:
double diameter = 10.0;
double volume = Math.PI / 6.0 * diameter * diameter * diameter;
System.out.print("Balloon volume in cubic cm = " + volume);
Output:
Balloon volume in cubic cm = 523.5987755982989
Some of Java’s Math class methods are extremely helpful when you need to evaluate a non-trivial math- ematical function, like raising a floating-point number to a fractional power. Others do simple things you could do yourself. For example, can you think of a primitive way to do the same thing that Math.round does? It’s pretty easy. Just add 0.5 to your original double number and then use a long cast operator on that double value to end up with a rounded version of the original number. (That’s what was done in days of yore.) If it’s that easy, why bother to use Math.round? Because it makes code more readable! The expression, Math.round(number), is self-documenting. It’s more informative than the odd-looking ex- pression,((long) (0.5 + number)).