1 import java.util.ArrayList;
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.
7 */
8 public class Scores
9 {
10 public static void main(String[] args)
11 {
12 ArrayList<Double> scores = readInputs();
13 if (scores.size() == 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 ArrayList containing the numbers
27 */
28 public static ArrayList<Double> readInputs()
29 {
30 ArrayList<Double> inputs = new ArrayList<Double>();
31 System.out.println("Please enter values, Q to quit:");
32 Scanner in = new Scanner(System.in);
33 while (in.hasNextDouble())
34 {
35 inputs.add(in.nextDouble());
36 }
37 return inputs;
38 }
39
40 /**
41 Computes the sum of the values in an array list
42 @param data an array list
43 @return the sum of the values in data
44 */
45 public static double sum(ArrayList<Double> data)
46 {
47 double total = 0;
48 for (double element : data)
49 {
50 total = total + element;
51 }
52 return total;
53 }
54
55 /**
56 Gets the minimum value from an ArrayList.
57 @param data an ArrayList of size >= 1
58 @return the smallest element of data
59 */
60 public static double minimum(ArrayList<Double> data)
61 {
62 double smallest = data.get(0);
63 for (int i = 1; i < data.size(); i++)
64 {
65 if (data.get(i) < smallest)
66 {
67 smallest = data.get(i);
68 }
69 }
70 return smallest;
71 }
72 }
73