Java Reference
In-Depth Information
Welcome to the grade book for
CS101 Introduction to Java Programming
The grades are:
Test 1 Test 2 Test 3 Average
Student 1 87 96 70 84.33
Student 2 68 87 90 81.67
Student 3 94 100 90 94.67
Student 4 100 81 82 87.67
Student 5 83 65 85 77.67
Student 6 78 87 65 76.67
Student 7 85 75 83 81.00
Student 8 91 94 100 95.00
Student 9 76 72 84 77.33
Student 10 87 93 73 84.33
Lowest grade in the grade book is 65
Highest grade in the grade book is 100
Overall grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: ***
70-79: ******
80-89: ***********
90-99: *******
100: ***
Fig. 7.19
|
GradeBookTest
creates
GradeBook
object using a two-dimensional array of grades,
then invokes method
processGrades
to analyze them. (Part 2 of 2.)
With
variable-length argument lists
, you can create methods that receive an unspecified
number of arguments. A type followed by an
ellipsis (
...
)
in a method's parameter list
indicates that the method receives a variable number of arguments of that particular type.
This use of the ellipsis can occur only
once
in a parameter list, and the ellipsis, together
with its type and the parameter name, must be placed at the
end
of the parameter list.
While you can use method overloading and array passing to accomplish much of what is
accomplished with variable-length argument lists, using an ellipsis in a method's parame-
ter list is more concise.
Figure 7.20 demonstrates method
average
(lines 7-16), which receives a variable-
length sequence of
double
s. Java treats the variable-length argument list as an array whose
elements are all of the same type. So, the method body can manipulate the parameter
num-
bers
as an array of
double
s. Lines 12-13 use the enhanced
for
loop to walk through the
array and calculate the total of the
double
s in the array. Line 15 accesses
numbers.length
to obtain the size of the
numbers
array for use in the averaging calculation. Lines 29, 31
and 33 in
main
call method
average
with two, three and four arguments, respectively.
Method
average
has a variable-length argument list (line 7), so it can average as many