Java Reference
In-Depth Information
Line 19 displays the prompt
"Enter
grade: "
. Line 20 reads the grade entered by the
user and assigns it to variable
grade
. Then line 21 adds the new
grade
entered by the user
to the
total
and assigns the result to
total
, which replaces its previous value.
Line 22 adds
1
to
gradeCounter
to indicate that the program has processed a grade and
is ready to input the next grade from the user. Incrementing
gradeCounter
eventually causes
it to exceed 10. Then the loop terminates, because its condition (line 17) becomes
false
.
Termination Phase: Calculating and Displaying the Class Average
When the loop terminates, line 26 performs the averaging calculation and assigns its result
to the variable
average
. Line 29 uses
System.out
's
printf
method to display the text
"To-
tal
of
all
10
grades
is "
followed by variable
total
's value. Line 30 then uses
printf
to display the text
"Class
average
is "
followed by variable
average
's value. When exe-
cution reaches line 31, the program terminates.
Notice that this example contains only one class, with method
main
performing all the
work. In this chapter and in Chapter 3, you've seen examples consisting of two classes—one
containing instance variables and methods that perform tasks using those variables and one
containing method
main
, which creates an object of the other class and calls its methods.
Occasionally, when it does not make sense to try to create a reusable class to demonstrate a
concept, we'll place the program's statements entirely within a single class's
main
method.
Notes on Integer Division and Truncation
The averaging calculation performed by method
main
produces an integer result. The pro-
gram's output indicates that the sum of the grade values in the sample execution is 846,
which, when divided by 10, should yield the floating-point number 84.6. However, the
result of the calculation
total / 10
(line 26 of Fig. 4.8) is the integer 84, because
total
and
10
are both integers. Dividing two integers results in
integer division
—any fractional
part of the calculation is
truncated
(i.e.,
lost
). In the next section we'll see how to obtain a
floating-point result from the averaging calculation.
Common Programming Error 4.4
Assuming that integer division rounds (rather than truncates) can lead to incorrect results.
For example, 7 ÷ 4, which yields 1.75 in conventional arithmetic, truncates to 1 in inte-
ger arithmetic, rather than rounding to 2.
A Note About Arithmetic Overflow
In Fig. 4.8, line 21
total = total + grade;
// add grade to total
added each
grade
entered by the user to the
total
. Even this simple statement has a
potential
problem—adding the integers could result in a value that's
too large
to store in an
int
vari-
able. This is known as
arithmetic overflow
and causes
undefined behavior
, which can lead to
unintended results
(
http://en.wikipedia.org/wiki/Integer_overflow#Security_
lated the sum of two
int
values entered by the user:
sum = number1 + number2;
// add numbers, then store total in sum
The maximum and minimum values that can be stored in an
int
variable are repre-
sented by the constants
MIN_VALUE
and
MAX_VALUE
, respectively, which are defined in class