Java Reference
In-Depth Information
puts a grade bar chart for the entire semester's student grades. Method
outputGrades
(lines
132-156) outputs the array in a tabular format, along with each student's semester average.
1
// Fig. 7.18: GradeBook.java
2
// GradeBook class using a two-dimensional array to store grades.
3
4
public
class
GradeBook
5
{
6
private
String courseName;
// name of course this grade book represents
7
private
int
[][] grades;
// two-dimensional array of student grades
8
9
// two-argument constructor initializes courseName and grades array
10
public
GradeBook(String courseName,
int
[][] grades
)
11
{
12
this
.courseName = courseName;
13
this
.grades = grades;
14
}
15
16
// method to set the course name
17
public
void
setCourseName(String courseName)
18
{
19
this
.courseName = courseName;
20
}
21
22
// method to retrieve the course name
23
public
String getCourseName()
24
{
25
return
courseName;
26
}
27
28
// perform various operations on the data
29
public
void
processGrades()
30
{
31
// output grades array
32
outputGrades();
33
34
// call methods getMinimum and getMaximum
35
System.out.printf(
"%n%s %d%n%s %d%n%n"
,
36
"Lowest grade in the grade book is"
, getMinimum(),
37
"Highest grade in the grade book is"
, getMaximum());
38
39
// output grade distribution chart of all grades on all tests
40
outputBarChart();
41
}
42
43
// find minimum grade
44
public
int
getMinimum()
45
{
46
// assume first element of grades array is smallest
47
int
lowGrade = grades[
0
][
0
];
48
Fig. 7.18
|
GradeBook
class using a two-dimensional array to store grades. (Part 1 of 4.)