8.8. GradeBook Case Study: Using an Array to Store GradesThis section further evolves class GradeBook, introduced in Chapter 4 and expanded in Chapters 56. Recall that this class represents a grade book used by a professor to store and analyze a set of student grades. Previous versions of the class process a set of grades entered by the user, but do not maintain the individual grade values in instance variables of the class. Thus, repeat calculations require the user to reenter the same grades. One way to solve this problem would be to store each grade entered in an instance of the class. For example, we could create instance variables grade1, grade2,..., grade10 in class GradeBook to store 10 student grades. However, the code to total the grades and determine the class average would be cumbersome, and the class would not be able to process any more than 10 grades at a time. In this section, we solve the problem by storing the grades in an array. Storing Student Grades in an Array in Class GradeBookThe version of class GradeBook (Fig. 8.13) presented here uses an array of Integers to store the grades of several students on a single exam. This eliminates the need to repeatedly input the same set of grades. Array grades is declared as an instance variable in line 5 therefore, each GradeBook object maintains its own set of grades. The class's constructor (lines 811) has two parametersthe name of the course and an array of grades. When an application (e.g., class GradeBookTest in Fig. 8.14) creates a GradeBook object, the application passes an existing Integer array to the constructor, which assigns the array's reference to instance variable grades (line 10). The size of the array grades is determined by the class that passes the array to the constructor. Thus, a GradeBook object can process a variable number of grades. The grade values in the passed array could have been input from a user or read from a file on disk (as discussed in Chapter 18). In our test application, we simply initialize an array with a set of grade values (Fig. 8.14, lines 67). Once the grades are stored in instance variable grades of class GradeBook, all the class's methods can access the elements of grades as needed to perform various calculations. Figure 8.13. GradeBook class using an array to store test grades.
Figure 8.14. GradeBookTest creates a GradeBook object using an array of grades, then invokes method ProcessGrades to analyze them.
Method ProcessGrades (lines 3143) contains a series of method calls that result in the output of a report summarizing the grades. Line 32 calls method OutputGrades to print the contents of the array grades. Lines 124127 in method OutputGrades use a For statement to output each student's grade. Lines 125126 use counter variable student's value to output each grade next to a particular student number (see the output in Fig. 8.14). Although array indices start at 0, a professor would typically number students starting at 1. Thus, lines 125126 output student + 1 as the student number to produce grade labels "Student 1: ", "Student2: ", and so on. Method ProcessGrades next calls method GetAverage (line 35) to obtain the average of the grades in the array. Method GetAverage (lines 7686) uses a For Each statement to total the values in array grades before calculating the average. The loop control variable declaration in the For Each's header (e.g., grade As Integer) indicates that for each iteration, the Integer variable grade takes on the next successive value in the array grades. The averaging calculation in line 85 uses grades.Length to determine the number of grades being averaged. Lines 3839 in method ProcessGrades call methods GetMinimum and GetMaximum to determine the lowest and highest grades of any student on the exam, respectively. Each of these methods uses a For Each statement to loop through array grades. Lines 5055 in method GetMinimum loop through the array, and lines 5254 compare each grade to low-Grade. If a grade is less than lowGrade, lowGrade is set to that grade. When line 57 executes, lowGrade contains the lowest grade in the array. Method GetMaximum (lines 6173) works similarly to method GetMinimum. Finally, line 42 in method ProcessGrades calls method OutputBarChart to print a distribution chart of the grade data using a technique similar to that in Fig. 8.6. Lines 9698 calculate the frequency of grades in each category. Line 106 passes to the method Console.Write the format string "{0, 2:D2}-{1, 2:D2}", which indicates that arguments 0 and 1 (the first two arguments after the format string) should take the format D2 (base 10 decimal number format using two digits) for display purposesthus, 8 would be converted to 08 and 10 would remain as 10. Recall that the number 2 before the colon indicates that the result should be output right justified in a field of width 2. The dash that separates the curly braces } and { is printed to display the range of the grades (see the output of Fig. 8.14). Line 93 declares and creates array frequency of 11 Integers to store the frequency of grades in each grade category. For each grade in array grades, lines 9698 increment the appropriate element of the frequency array. To determine which element to increment, line 97 divides the current grade by 10 using integer division. For example, if grade is 85, line 97 increments frequency[ 8 ] to update the count of grades in the range 8089. Lines 101116 next print the bar chart (see the output of Fig. 8.14) based on the values in the frequency array. Like lines 1517 of Fig. 8.6, lines 111113 of Fig. 8.13 use a value in array frequency to determine the number of asterisks to display in each bar. Class GradeBookTest That Demonstrates Class GradeBookThe application in Fig. 8.14 creates an object of class GradeBook (Fig. 8.13) using the Integer array gradesArray (declared and initialized in lines 67). Lines 89 pass a course name and gradesArray to the GradeBook constructor. Line 10 displays a welcome message, and line 11 invokes the GradeBook object's ProcessGrades method. The output presents an analysis of the 10 grades in gradeBooks. Software Engineering Observation 8.1
|