Java Reference
In-Depth Information
Table 3-9
Methods from the Math Class
General form:
Math.method(arguments)
Purpose:
The Math class contains methods for performing basic numeric
operations such as the elementary exponential, logarithm, square
root, and trigonometric functions.
Examples:
METHOD
DESCRIPTION
EXAMPLE CODE
RESULT
abs()
Absolute value
Math.abs(-75)
75
max()
Higher of two numbers
Math.max(99,41)
99
min()
Lower of two numbers
Math.min(99,41)
41
pow()
Exponentiation
Math.pow(2,5)
32
random()
Random number generator
Math.random()
0< value >1
round()
Rounding to an integer
Math.round(27.59)
28
sqrt()
Square root
Math.sqrt(144)
12
Recall that the values inside the parentheses are the arguments — the pieces
of information the method needs to perform its task. The arguments in most
Math methods can be literals (as in the table examples), variables, or calls to
other methods. The pow() method , used to express exponentiation as in meters
squared, requires two arguments: the base number and the exponent. A comma
separates the arguments.
Perform the following step to enter the formulas.
To Enter the Formulas
1. With the insertion point on line 32 in the coding window, enter the
code from Figure 3-17 on the previous page.
The formulas to convert input values to metric and to calculate the BMI are
entered (Figure 3-18). Line 36 is a blank line.
formulas
pow() method
FIGURE 3-18
 
Search WWH ::




Custom Search