Java Reference
In-Depth Information
Display 6.6
Display 6.4 Redone Using the Class
PartiallyFilledArray
(part 1 of 2)
Sample dialog is the same as in Display 6.4.
1
import
java.util.Scanner;
2
/**
3
Demonstrates Using the class PartiallyFilledArray,
4
*/
5
public class
GolfScoresVersion2
6 {
7
public static final int
MAX_NUMBER_SCORES = 10;
8
/**
9
Shows the differences between each of a list of golf scores and
their average.
10
*/
11
public static void
main(String[] args)
12 {
13 PartiallyFilledArray score =
14
new
PartiallyFilledArray(MAX_NUMBER_SCORES);
15 System.out.println("This program reads golf scores and shows");
16 System.out.println("how much each differs from the average.");
17 System.out.println("Enter golf scores:");
18 fillArray(score);
19 showDifference(score);
20 }
21
/**
22
Reads values into the PartiallyFilledArray a.
23
*/
24
public static void
fillArray(PartiallyFilledArray a)
25 {
26 System.out.println("Enter up to " + a.getMaxCapacity()
27 + " nonnegative numbers, one per line.");
28 System.out.println("Mark the end of the list with a negative
number");
29 Scanner keyboard =
new
Scanner(System.in);
31
32
double
next = keyboard.nextDouble();
31
while
((next >= 0) && (!a.full()))
33 {
34 a.add(next);
35 next = keyboard.nextDouble();
36 }
37
if
(next >= 0)
38 System.out.println("Could only read in "
39 + a.getMaxCapacity() + " input values.");
40 }
(continued)