Java Reference
In-Depth Information
Method
getAverage
Method
getAverage
(lines 86-96) takes one argument—a one-dimensional array of test
results for a particular student. When line 153 calls
getAverage
, the argument is
grades[student]
, which specifies that a particular row of the two-dimensional array
grades
should be passed to
getAverage
. For example, based on the array created in
Fig. 7.19, the argument
grades[1]
represents the three values (a one-dimensional array of
grades) stored in row
1
of the two-dimensional array
grades
. Recall that a two-dimension-
al array is one whose elements are one-dimensional arrays. Method
getAverage
calculates
the sum of the array elements, 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 7.19 creates an object of class
GradeBook
(Fig. 7.18) using the two-dimensional ar-
ray of
int
s named
gradesArray
(declared and initialized in lines 10-19). Lines 21-22 pass
a course name and
gradesArray
to the
GradeBook
constructor. Lines 23-24 display a wel-
come message containing the course name, then line 25 invokes
myGradeBook
's
process-
Grades
method to display a report summarizing the students' grades for the semester.
1
// Fig. 7.19: GradeBookTest.java
2
// GradeBookTest creates GradeBook object using a two-dimensional array
3
// of grades, then invokes method processGrades to analyze them.
4
public
class
GradeBookTest
5
{
6
// main method begins program execution
7
public
static
void
main(String[] args)
8
{
9
// two-dimensional array of student grades
int
[][] gradesArray = {{
87
,
96
,
70
},
{
68
,
87
,
90
},
{
94
,
100
,
90
},
{
100
,
81
,
82
},
{
83
,
65
,
85
},
{
78
,
87
,
65
},
{
85
,
75
,
83
},
{
91
,
94
,
100
},
{
76
,
72
,
84
},
{
87
,
93
,
73
}};
10
11
12
13
14
15
16
17
18
19
20
21
GradeBook myGradeBook =
new
GradeBook(
22
"CS101 Introduction to Java Programming"
,
gradesArray
);
23
System.out.printf(
"Welcome to the grade book for%n%s%n%n"
,
24
myGradeBook.getCourseName());
25
myGradeBook.processGrades();
26
}
27
}
// end class GradeBookTest
Fig. 7.19
|
GradeBookTest
creates
GradeBook
object using a two-dimensional array of grades,
then invokes method
processGrades
to analyze them. (Part 1 of 2.)