Java Reference
In-Depth Information
Performing the Interest Calculations with
static
Method
pow
of Class
Math
The
for
statement (lines 16-23) executes its body 10 times, varying control variable
year
from 1 to 10 in increments of 1. This loop terminates when
year
becomes 11. (Variable
year
represents
n
in the problem statement.)
Classes provide methods that perform common tasks on objects. In fact, most
methods must be called on a specific object. For example, to output text in Fig. 5.6, line
13 calls method
printf
on the
System.out
object. Some classes also provide methods that
perform common tasks and do
not
require you to first create objects of those classes. These
are called
static
methods. For example, Java does not include an exponentiation oper-
ator, so the designers of Java's
Math
class defined
static
method
pow
for raising a value to
a power. You can call a
static
method by specifying the
class name
followed by a dot (
.
)
and the method name, as in
ClassName
.
methodName
(
arguments
)
In Chapter 6, you'll learn how to implement
static
methods in your own classes.
We use
static
method
pow
of class
Math
to perform the compound-interest calcula-
tion in Fig. 5.6.
Math.pow(
x
,
y
)
calculates the value of
x
raised to the
y
th
power. The
method receives two
double
arguments and returns a
double
value. Line 19 performs the
calculation
a
=
p
(1 +
r
)
n
, where
a
is
amount
,
p
is
principal
,
r
is
rate
and
n
is
year
. Class
Math
is defined in package
java.lang
, so you do
not
need to import class
Math
to use it.
The body of the
for
statement contains the calculation
1.0
+
rate
, which appears as
an argument to the
Math.pow
method. In fact, this calculation produces the
same
result
each time through the loop, so repeating it in every iteration of the loop is wasteful.
Performance Tip 5.1
In loops, avoid calculations for which the result never changes—such calculations should
typically be placed before the loop. Many of today's sophisticated optimizing compilers will
place such calculations outside loops in the compiled code.
Formatting Floating-Point Numbers
After each calculation, line 22 outputs the year and the amount on deposit at the end of
that year. The year is output in a field width of four characters (as specified by
%4d
). The
amount is output as a floating-point number with the format specifier
%,20.2f
. The
com-
ma (
,
) formatting flag
indicates that the floating-point value should be output with a
grouping separator
. The actual separator used is specific to the user's locale (i.e., country).
For example, in the United States, the number will be output using commas to separate
every three digits and a decimal point to separate the fractional part of the number, as in
1,234.45. The number
20
in the format specification indicates that the value should be
output right justified in a
field width
of 20 characters. The
.2
specifies the formatted num-
ber's
precision
—in this case, the number is
rounded
to the nearest hundredth and output
with two digits to the right of the decimal point.
A Warning about Displaying Rounded Values
We declared variables
amount
,
principal
and
rate
to be of type
double
in this example.
We're dealing with fractional parts of dollars and thus need a type that allows decimal
points in its values. Unfortunately, floating-point numbers can cause trouble. Here's a
simple explanation of what can go wrong when using
double
(or
float
) to represent dollar