Let’s use pseudocode to list the actions to execute and specify the order in which they should execute. We use counter-controlled repetition to input the grades one at a time. This technique uses a variable called a counter (or control variable) to control the number of times a set of statements will execute. In this example, repetition terminates when the counter exceeds 10. This section presents a fully developed pseudocode algorithm (Fig. C.1) and a version of class GradeBook
(Fig. C.2) that implements the algorithm in a Java method. We then present an application (Fig. C.3) that demonstrates the algorithm in action.
1 Set total to zero
2 Set grade counter to one
3
4 While grade counter is less than or equal to ten
5 Prompt the user to enter the next grade
6 Input the next grade
7 Add the grade into the total
8 Add one to the grade counter
9
10 Set the class average to the total divided by ten
11 Print the class average
1 // Fig. C.2: GradeBook.java
2 // GradeBook class that solves the class-average problem using
3 // counter-controlled repetition.
4 import java.util.Scanner; // program uses class Scanner
5
6 public class GradeBook
7 {
8 private String courseName; // name of course this GradeBook represents
9
10 // constructor initializes courseName
11 public GradeBook( String name )
12 {
13 courseName = name; // initializes courseName
14 } // end constructor
15
16 // method to set the course name
17 public void setCourseName( String name )
18 {
19 courseName = name; // store the course name
20 } // end method setCourseName
21
22 // method to retrieve the course name
23 public String getCourseName()
24 {
25 return courseName;
26 } // end method getCourseName
27
28 // display a welcome message to the GradeBook user
29 public void displayMessage()
30 {
31 // getCourseName gets the name of the course
32 System.out.printf( "Welcome to the grade book for
%s!
",
33 getCourseName() );
34 } // end method displayMessage
35
36 // determine class average based on 10 grades entered by user
37 public void determineClassAverage()
38 {
39 // create Scanner to obtain input from command window
40 Scanner input = new Scanner( System.in );
41
42 int total; // sum of grades entered by user
43 int gradeCounter; // number of the grade to be entered next
44 int grade; // grade value entered by user
45 int average; // average of grades
46
47 // initialization phase
48 total = 0; // initialize total
49 gradeCounter = 1; // initialize loop counter
50
51 // processing phase uses counter-controlled repetition
52 while ( gradeCounter <= 10 ) // loop 10 times
53 {
54 System.out.print( "Enter grade: " ); // prompt
55 grade = input.nextInt(); // input next grade
56 total = total + grade; // add grade to total
57 gradeCounter = gradeCounter + 1; // increment counter by 1
58 } // end while
59
60 // termination phase
61 average = total / 10; // integer division yields integer result
62
63 // display total and average of grades
64 System.out.printf( "
Total of all 10 grades is %d
", total );
65 System.out.printf( "Class average is %d
", average );
66 } // end method determineClassAverage
67 } // end class GradeBook
1 // Fig. C.3: GradeBookTest.java
2 // Create GradeBook object and invoke its determineClassAverage method.
3
4 public class GradeBookTest
5 {
6 public static void main( String[] args )
7 {
8 // create GradeBook object myGradeBook and
9 // pass course name to constructor
10 GradeBook myGradeBook = new GradeBook(
11 "CS101 Introduction to Java Programming" );
12
13 myGradeBook.displayMessage(); // display welcome message
14 myGradeBook.determineClassAverage(); // find average of 10 grades
15 } // end main
16 } // end class GradeBookTest
Welcome to the grade book for
CS101 Introduction to Java Programming!
Enter grade: 67
Enter grade: 78
Enter grade: 89
Enter grade: 67
Enter grade: 87
Enter grade: 98
Enter grade: 93
Enter grade: 85
Enter grade: 82
Enter grade: 100
Total of all 10 grades is 846
Class average is 84
Note the references in the algorithm of Fig. C.1 to a total and a counter. A total is a variable used to accumulate the sum of several values. A counter is a variable used to count—in this case, the grade counter indicates which of the 10 grades is about to be entered by the user. Variables used to store totals are normally initialized to zero before being used in a program.