1 import java.util.ArrayList;
2
3 public class GradeBook
4 {
5 private ArrayList<Double> scores;
6
7 /**
8 Constructs a gradebook with no scores.
9 */
10 public GradeBook()
11 {
12 scores = new ArrayList<Double>();
13 }
14
15 /**
16 Adds a score to this gradebook.
17 @param score the score to add
18 */
19 public void addScore(double score)
20 {
21 scores.add(score);
22 }
23
24 /**
25 Computes the sum of the scores in this gradebook.
26 @return the sum of the scores
27 */
28 public double sum()
29 {
30 double total = 0;
31 for (double element : scores)
32 {
33 total = total + element;
34 }
35 return total;
36 }
37
38 /**
39 Gets the minimum score in this gradebook.
40 @return the minimum score, or 0 if there are no scores.
41 */
42 public double minimum()
43 {
44 if (scores.size() == 0) return 0;
45 double smallest = scores.get(0);
46 for (int i = 1; i < scores.size(); i++)
47 {
48 if (scores.get(i) < smallest)
49 {
50 smallest = scores.get(i);
51 }
52 }
53 return smallest;
54 }
55
56 /**
57 Gets the final score for this gradebook.
58 @return the sum of the scores, with the lowest score dropped if
59 there are at least two scores, or 0 if there are no scores.
60 */
61 public double finalScore()
62 {
63 if (scores.size() == 0)
64 return 0;
65 else if (scores.size() == 1)
66 return scores.get(0);
67 else
68 return sum() - minimum();
69 }
70 }
71