Implementing Sentinel-Controlled Repetition

In Fig. 4.10, method main (lines 7–46) implements the pseudocode algorithm of Fig. 4.9. Although each grade is an integer, the averaging calculation is likely to produce a number with a decimal point—in other words, a real (floating-point) number. The type int cannot represent such a number, so this class uses type double to do so. You’ll also see that control statements may be stacked on top of one another (in sequence). The while statement (lines 22–30) is followed in sequence by an if...else statement (lines 34–45). Much of the code in this program is identical to that in Fig. 4.8, so we concentrate on the new concepts.

 1   // Fig. 4.10: ClassAverage.java 2   // Solving ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.