1 import java.util.Arrays;
2 import java.util.Scanner;
3
4 /**
5 This program computes a final score for a series of quiz scores: the sum after dropping
6 the lowest score. The program uses arrays.
7 */
8 public class Scores
9 {
10 public static void main(String[] args)
11 {
12 double[] scores = readInputs();
13 if (scores.length == 0)
14 {
15 System.out.println("At least one score is required.");
16 }
17 else
18 {
19 double total = sum(scores) - minimum(scores);
20 System.out.println("Final score: " + total);
21 }
22 }
23
24 /**
25 Reads a sequence of floating-point numbers.
26 @return an array containing the numbers
27 */
28 public static double[] readInputs()
29 {
30 // Read the input values into an array
31
32 final int INITIAL_SIZE = 10;
33 double[] inputs = new double[INITIAL_SIZE];
34 System.out.println("Please enter values, Q to quit:");
35 Scanner in = new Scanner(System.in);
36 int currentSize = 0;
37 while (in.hasNextDouble())
38 {
39 // Grow the array if it has been completely filled
40
41 if (currentSize >= inputs.length)
42 {
43 inputs = Arrays.copyOf(inputs, 2 * inputs.length);
44 }
45 inputs[currentSize] = in.nextDouble();
46 currentSize++;
47 }
48
49 return Arrays.copyOf(inputs, currentSize);
50 }
51
52 /**
53 Computes the sum of the values in an array
54 @param data an array
55 @return the sum of the values in data
56 */
57 public static double sum(double[] data)
58 {
59 double total = 0;
60 for (double element : data)
61 {
62 total = total + element;
63 }
64 return total;
65 }
66
67 /**
68 Gets the minimum value from an array.
69 @param data an array of size >= 1
70 @return the smallest element of data
71 */
72 public static double minimum(double[] data)
73 {
74 double smallest = data[0];
75 for (int i = 1; i < data.length; i++)
76 {
77 if (data[i] < smallest)
78 {
79 smallest = data[i];
80 }
81 }
82 return smallest;
83 }
84 }
85