8.10 Case Study: GradeBook Using a Rectangular Array
In Section 8.8, we presented class GradeBook (Fig. 8.15), which used a one-dimensional array to store student grades on a single exam. In most courses, students take several exams. Instructors are likely to want to analyze grades across the entire course, both for a single student and for the class as a whole.
Storing Student Grades in a Rectangular Array in Class GradeBook
Figure 8.20 shows the output that summarizes 10 students’ grades on three exams. Figure 8.21 contains a version of class GradeBook that uses a rectangular array grades to store the grades of a number of students on multiple exams. Each row of the array represents a single student’s grades for the entire course, and each column represents the grades for the whole class on one of the exams the students took during the course. An app such as GradeBookTest (Fig. 8.22) passes the array as an argument to the GradeBook constructor. In this example, we use a 10-by-3 array containing 10 students’ grades on three exams.
Five methods perform array manipulations to process the grades. Each method is similar to its counterpart in the one-dimensional-array GradeBook (Fig. 8.15). Method Get-Minimum (lines 44–60 in Fig. 8.21) determines the lowest grade of any student. Method GetMaximum (lines 63–79) determines the highest grade of any student. Method Get-Average (lines 82–96) determines a particular student’s semester average. Method OutputBarChart (lines 99–133) outputs a bar chart of the distribution of all student grades for the semester. Method OutputGrades (lines 136–164) outputs the two-dimensional array in tabular format, along with each student’s semester average.
Processing a Two-Dimensional Array with a foreach Statement
Methods GetMinimum, GetMaximum and OutputBarChart each loop through array grades using the foreach statement—for example, the foreach statement from method GetMinimum (lines 50–57). To find the lowest overall grade, this foreach statement iterates through rectangular array grades and compares each element to variable lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade.
When the foreach statement traverses the elements of array grades, it looks at each element of the first row in order by index, then each element of the second row in order by index and so on. The foreach statement (lines 50–57) traverses the elements of grades in the same order as the following equivalent code, expressed with nested for statements:
When the foreach statement completes, lowGrade contains the lowest grade in the rectangular array. Method GetMaximum works similarly to method GetMinimum. Note the simplicity of using foreach vs. the preceding nested for statement.
Software Engineering Observation 8.2
“Keep it simple” is good advice for most of the code you’ll write.
Method OutputBarChart
Method OutputBarChart (lines 99–133) displays the grade distribution as a bar chart. The syntax of the foreach statement (lines 107–110) is identical for one-dimensional and two-dimensional arrays.
Method OutputGrades
Method OutputGrades (lines 136–164) uses nested for statements to output grades’ values, in addition to each student’s semester average. The output in Fig. 8.20 shows the result, which resembles the tabular format of an instructor’s physical grade book. Lines 142– 145 (in Fig. 8.21) display the column headings for each test. We use the for statement rather than the foreach statement here so that we can identify each test with a number. Similarly, the for statement in lines 150–163 first outputs a row label using a counter variable to identify each student (line 152). Although array indices start at 0, lines 144 and 152 output test + 1 and student + 1, respectively, to produce test and student numbers starting at 1 (see Fig. 8.20). The inner for statement in lines 155–158 of Fig. 8.21 uses the outer for statement’s counter variable student to loop through a specific row of array grades and output each student’s test grade. Finally, line 162 obtains each student’s semester average by passing the row index of grades (i.e., student) to method GetAverage.
Method GetAverage
Method GetAverage (lines 82–96) takes one argument—the row index for a particular student. When line 162 calls GetAverage, the argument is int value student, which specifies the particular row of rectangular array grades. Method GetAverage calculates the sum of the array elements on this row, divides the total by the number of test results and returns the floating-point result as a double value (line 95).
Class GradeBookTest That Demonstrates Class GradeBook
Figure 8.22 creates an object of class GradeBook (Fig. 8.21) using the two-dimensional array of ints that gradesArray references (Fig. 8.22, lines 9–18). Lines 20–21 pass a course name and gradesArray to the GradeBook constructor. Lines 22–23 then invoke myGrade-Book’s DisplayMessage and ProcessGrades methods to display a welcome message and obtain a report summarizing the students’ grades for the semester, respectively.