June 2017
Beginner
1296 pages
69h 23m
English
... enhanced for loop to walk through the array and calculate the total of the doubles in the array. Line 14 accesses numbers.length to obtain the size of the numbers array for use in the averaging calculation. Lines 27, 29 and 31 in main call method average with two, three and four arguments, respectively. Method average has a variable-length argument list (line 6), so it can average as many double arguments as the caller passes. The output shows that each call to method average returns the correct value.
1 // Fig. 7.20: VarargsTest.java
2 // Using variable-length argument lists.
3
4 public class VarargsTest {
5 // calculate average
6 public static double average( double… numbers) {
7 double total = 0.0;
8
9 // calculate total using the enhanced ...Read now
Unlock full access