Java Reference
In-Depth Information
array parameter. Thus, a
GradeBook
object can process a
variable
number of grades. The
grade values in the argument could have been input from a user, read from a file on disk
(as discussed in Chapter 15) or come from a variety of other sources. In class
GradeBook-
Test
, we initialize an array with grade values (Fig. 7.15, line 10). Once the grades are
stored in
instance variable
grades
of class
GradeBook
, all the class's methods can access the
elements of
grades
.
1
// Fig. 7.14: GradeBook.java
2
// GradeBook class using an array to store test grades.
3
4
public
class
GradeBook
5
{
6
private
String courseName;
// name of course this GradeBook represents
7
private
int
[] grades;
// array of student grades
8
9
// constructor
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 method getAverage to calculate the average grade
35
System.out.printf(
"%nClass average is %.2f%n"
,
getAverage()
);
36
37
// call methods getMinimum and getMaximum
38
System.out.printf(
"Lowest grade is %d%nHighest grade is %d%n%n"
,
39
getMinimum() getMaximum()
,
);
40
41
// call outputBarChart to print grade distribution chart
42
outputBarChart();
43
}
44
Fig. 7.14
|
GradeBook
class using an array to store test grades. (Part 1 of 3.)