Java Reference
In-Depth Information
Line 42 displays the class average. In this example, we display the class average rounded
to the nearest hundredth. The format specifier %.2f in printf 's format control string indi-
cates that variable average 's value should be displayed with two digits of precision to the
right of the decimal point—indicated by .2 in the format specifier. The three grades
entered during the sample execution (Fig. 4.10) total 257, which yields the average
85.666666…. Method printf uses the precision in the format specifier to round the value
to the specified number of digits. In this program, the average is rounded to the hun-
dredths position and is displayed as 85.67 .
Floating-Point Number Precision
Floating-point numbers are not always 100% precise, but they have numerous applica-
tions. For example, when we speak of a “normal” body temperature of 98.6, we do not
need to be precise to a large number of digits. When we read the temperature on a ther-
mometer as 98.6, it may actually be 98.5999473210643. Calling this number simply 98.6
is fine for most applications involving body temperatures.
Floating-point numbers often arise as a result of division, such as in this example's
class-average calculation. In conventional arithmetic, when we divide 10 by 3, the result is
3.3333333…, with the sequence of 3s repeating infinitely. The computer allocates only a
fixed amount of space to hold such a value, so clearly the stored floating-point value can
be only an approximation.
Owing to the imprecise nature of floating-point numbers, type double is preferred
over type float , because double variables can represent floating-point numbers more
accurately. For this reason, we primarily use type double throughout the topic. In some
applications, the precision of float and double variables will be inadequate. For precise
floating-point numbers (such as those required by monetary calculations), Java provides
class BigDecimal (package java.math ), which we'll discuss in Chapter 8.
Common Programming Error 4.7
Using floating-point numbers in a manner that assumes they're represented precisely can
lead to incorrect results.
4.11 Formulating Algorithms: Nested Control Statements
For the next example, we once again formulate an algorithm by using pseudocode and top-
down, stepwise refinement, and write a corresponding Java program. We've seen that con-
trol statements can be stacked on top of one another (in sequence). In this case study, we
examine the only other structured way control statements can be connected—namely, by
nesting one control statement within another.
Consider the following problem statement:
A college offers a course that prepares students for the state licensing exam for real-estate
brokers. Last year, ten of the students who completed this course took the exam. The col-
lege wants to know how well its students did on the exam. You've been asked to write a
program to summarize the results. You've been given a list of these 10 students. Next to
each name is written a 1 if the student passed the exam or a 2 if the student failed.
Your program should analyze the results of the exam as follows:
1. Input each test result (i.e., a 1 or a 2). Display the message “Enter result” on the screen
each time the program requests another test result.
 
 
Search WWH ::




Custom Search