Java Reference
In-Depth Information
154 System.out.printf( "%9.2f%n" , average);
155 }
156 }
157 } // end class GradeBook
Fig. 7.18 | GradeBook class using a two-dimensional array to store grades. (Part 4 of 4.)
Methods getMinimum and getMaximum
Methods getMinimum , getMaximum , outputBarChart and outputGrades each loop
through array grades by using nested for statements—for example, the nested enhanced
for statement (lines 50-59) from the declaration of method getMinimum . The outer en-
hanced for statement iterates through the two-dimensional array grades , assigning suc-
cessive rows to parameter studentGrades on successive iterations. The square brackets
following the parameter name indicate that studentGrades refers to a one-dimensional
int array—namely, a row in array grades containing one student's grades. To find the
lowest overall grade, the inner for statement compares the elements of the current one-
dimensional array studentGrades to variable lowGrade . For example, on the first iteration
of the outer for , row 0 of grades is assigned to parameter studentGrades . The inner en-
hanced for statement then loops through studentGrades and compares each grade value
with lowGrade . If a grade is less than lowGrade , lowGrade is set to that grade. On the sec-
ond iteration of the outer enhanced for statement, row 1 of grades is assigned to stu-
dentGrades , and the elements of this row are compared with variable lowGrade . This
repeats until all rows of grades have been traversed. When execution of the nested state-
ment is complete, lowGrade contains the lowest grade in the two-dimensional array.
Method getMaximum works similarly to method getMinimum .
Method outputBarChart
Method outputBarChart in Fig. 7.18 is nearly identical to the one in Fig. 7.14. However,
to output the overall grade distribution for a whole semester, the method here uses nested
enhanced for statements (lines 107-111) to create the one-dimensional array frequency
based on all the grades in the two-dimensional array. The rest of the code in each of the
two outputBarChart methods that displays the chart is identical.
Method outputGrades
Method outputGrades (lines 132-156) uses nested for statements to output values of the
array grades and each student's semester average. The output (Fig. 7.19) shows the result,
which resembles the tabular format of a professor's physical grade book. Lines 138-139
print the column headings for each test. We use a counter-controlled for statement here
so that we can identify each test with a number. Similarly, the for statement in lines 144-
155 first outputs a row label using a counter variable to identify each student (line 146).
Although array indices start at 0, lines 139 and 146 output test + 1 and student + 1 , re-
spectively, to produce test and student numbers starting at 1 (see the output in Fig. 7.19).
The inner for statement (lines 148-149) 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. An enhanced for statement can be nested in a counter-controlled for statement,
and vice versa. Finally, line 153 obtains each student's semester average by passing the cur-
rent row of grades (i.e., grades[student] ) to method getAverage .
 
Search WWH ::




Custom Search