1  import java.util.ArrayList;
  2  
  3  /**
  4     The Student class from How To 7.1, reimplemented with an array list.
  5     Note that most of the code is the same, with the [] replaced by calls
  6     to the get/set methods. However, the code for adding a score is
  7     simpler since we don't have to worry about running out of space.
  8     Also note the use of the enhanced for loop in the sum method.
  9  */
 10  
 11  public class Student
 12  {
 13     private ArrayList<Double> scores;
 14  
 15     public Student()
 16     {
 17        scores = new ArrayList<Double>();      
 18     }
 19  
 20     /**
 21        Adds a score for this student.
 22        @param score the score to add
 23     */
 24     public void addScore(double score)
 25     {
 26        scores.add(score);
 27     }
 28     
 29     /**
 30        Gets the position of the minimum score.
 31        @return the position of the smallest element of values, or -1
 32        if there are no scores.
 33     */
 34     public int minimumPosition()
 35     {
 36        if (scores.size() == 0) { return -1; }
 37        int smallestPosition = 0;
 38        for (int i = 1; i < scores.size(); i++)
 39        {
 40           if (scores.get(i) < scores.get(smallestPosition))
 41           {
 42              smallestPosition = i;
 43           }
 44        }
 45        return smallestPosition;
 46     }
 47  
 48     /**
 49        Computes the sum of the scores
 50        @return the total score
 51     */
 52     public double sum()
 53     {
 54        double total = 0;
 55        for (double score : scores)
 56        {
 57           total = total + score;
 58        }
 59        return total;
 60     }     
 61  
 62     /**
 63        Removes a score at a given position.
 64        @param pos the position of the score to remove
 65     */
 66     public void removeScore(int pos)
 67     {
 68        scores.remove(pos);
 69        /*
 70          Alternatively, for a more efficient implementation,
 71          follow Section 7.3.6:
 72          int lastPosition = scores.size() - 1;
 73          scores.set(pos, scores.get(lastPosition));
 74          scores.remove(lastPosition);
 75        */
 76     }
 77  }
 78