Implementing Sentinel-Controlled Repetition in Class GradeBook
Figure C.5 shows the Java class GradeBook
containing method determineClassAverage
that implements the pseudocode algorithm of Fig. C.4. Although each grade is an integer, the averaging calculation is likely to produce a number with a decimal point—in other words, a real (i.e., floating-point) number. The type int
cannot represent such a number, so this class uses type double
to do so.
1 // Fig. C.5: GradeBook.java 2 // GradeBook class that solves the class-average problem using 3 // sentinel-controlled repetition. 4 import java.util.Scanner; // program uses class Scanner 5 6 public class GradeBook 7 { 8 private String courseName; ...
Get Android™ How to Program, Second 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.