Java Reference
In-Depth Information
45
// find minimum grade
46
public
int
getMinimum()
47
{
48
int
lowGrade = grades[
0
];
// assume grades[0] is smallest
49
50
// loop through grades array
for
(
int
grade : grades)
{
// if grade lower than lowGrade, assign it to lowGrade
if
(grade < lowGrade)
lowGrade = grade;
// new lowest grade
}
51
52
53
54
55
56
57
58
return
lowGrade;
59
}
60
61
// find maximum grade
62
public
int
getMaximum()
63
{
64
int
highGrade = grades[
0
];
// assume grades[0] is largest
65
66
// loop through grades array
67
for
(
int
grade : grades)
68
{
69
// if grade greater than highGrade, assign it to highGrade
70
if
(grade > highGrade)
71
highGrade = grade;
// new highest grade
72
}
73
74
return
highGrade;
75
}
76
77
// determine average grade for test
78
public
double
getAverage()
79
{
80
int
total =
0
;
81
82
// sum grades for one student
for
(
int
grade : grades)
total += grade;
83
84
85
86
// return average of grades
87
return
(
double
) total /
grades.length
;
88
}
89
90
// output bar chart displaying grade distribution
91
public
void
outputBarChart()
92
{
93
System.out.println(
"Grade distribution:"
);
94
95
// stores frequency of grades in each range of 10 grades
96
int
[] frequency =
new
int[
11
];
Fig. 7.14
|
GradeBook
class using an array to store test grades. (Part 2 of 3.)