Visual Basic 2005 for Programmers (2nd Edition)
5.9. Counter-Controlled Repetition
Next, we modify the GradeBook class of Chapter 4 to solve two variations of a problem that averages student grades. Consider the following problem statement: A class of 10 students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. The class average is equal to the sum of the grades divided by the number of students (10). The program for solving this problem must input each grade, keep track of the total of all grades input, perform the averaging calculation and print the result. We use counter-controlled repetition to input and process the grades one at a time. This technique uses a counter to specify the number of times that a set of statements will execute. Counter-controlled repetition also is called definite repetition because the number of repetitions is known before the loop begins executing. In this example, repetition terminates when the counter exceeds 10. This section presents a version of class GradeBook (Fig. 5.10) that implements the algorithm in a method. The section then presents an application (Fig. 5.11) that demonstrates the algorithm in action. Figure 5.10. Counter-controlled repetition: Class-average problem.
Figure 5.11. Module GradeBookTest creates an object of class GradeBook (Fig. 5.10) and invokes its DetermineClassAverage method.
Implementing Counter-Controlled Repetition in Class GradeBook
Class GradeBook (Fig. 5.10) contains a constructor (lines 810) that assigns a value to the class's property CourseName. Lines 1433 and 3641 declare property CourseName and method DisplayMessage, respectively. Lines 4469 declare method DetermineClassAverage, which implements the class-averaging algorithm. Lines 4548 declare local variables total, gradeCounter, grade and average to be of type Integer. In this example, variable total accumulates the sum of the grades entered and gradeCounter counts the number of grades entered. Variable grade stores the most recent grade value entered (line 58). Note that the declarations (in lines 4548) appear in the body of method DetermineClassAverage. Recall that variables declared in a method body are local variables and can be used only from their declaration until the end of the method declaration. A local variable's declaration must appear before the variable is used in that method. A local variable cannot be accessed outside the method in which it is declared. In Visual Basic, numeric variables are initialized by default to 0 when they are declared, unless another value is assigned to the variable in its declaration. In the versions of class GradeBook in this chapter, we simply read and process a set of grades. The averaging calculation is performed in method DetermineClassAverage using local variableswe do not preserve any information about student grades in instance variables of the class. In versions of the class in Chapter 8, Arrays, we maintain the grades in memory using an instance variable that refers to an array. This allows a GradeBook object to perform various calculations on the same set of grades without requiring the user to enter the grades multiple times. The assignments (in lines 5152) initialize total to 0 and gradeCounter to 1.Line 55 indicates that the While statement should continue looping as long as the value of gradeCounter is less than or equal to 10. While this condition remains true, the While statement repeatedly executes the statements in its body (lines 5760). Line 57 displays the prompt "Enter grade: ". Line 58 reads the grade entered by the user and assigns it to the variable grade. Then line 59 adds the new grade entered by the user into the variable total. Line 60 adds 1 to gradeCounter to indicate that the program has processed another grade and is ready to input the next grade from the user. Incrementing gradeCounter eventually causes it to exceed 10. At that point the While loop terminates because its condition (line 55) becomes false. When the loop terminates, line 64 performs the averaging calculation and assigns its result to the variable average. Line 67 displays the text "Total of all 10 grades is " followed by variable total's value. Line 68 then displays the text "Class average is " followed by variable average's value. Method DetermineClassAverage returns control to the calling method (i.e., Main in GradeBookTest of Fig. 5.11) after reaching line 69. Module GradeBookTest
Module GradeBookTest (Fig. 5.11) creates an object of class GradeBook (Fig. 5.10) and demonstrates its capabilities. Line 7 of Fig. 5.11 creates a new GradeBook object and assigns it to variable gradeBook. The string in line 7 is passed to the GradeBook constructor (lines 810 of Fig. 5.10). Line 9 calls gradeBook's DisplayMessage method to display a welcome message to the user. Line 10 then calls gradeBook's DetermineClassAverage method to allow the user to enter 10 grades, for which the method then calculates and prints the average, performing the algorithm shown in Fig. 5.10. Notes on Integer Division
The averaging calculation performed by method DetermineClassAverage in response to the method call at line 10 in Fig. 5.11 produces an integer result. The program's output indicates that the sum of the grade values in the sample execution is 844, which, when divided by 10, should yield the floating-point number 84.4. However, the result of the calculation total \ 10 (line 64 of Fig. 5.10) is the integer 84, because the integer division operator is used. Recall that the integer division operator takes two integer operands and returns an integer result. We use the floating-point division operator in the next section to determine a floating-point average. |
Категории