Student.java.html

  1  public class Student
  2  {
  3     private double[] scores;
  4     private int scoresSize;
  5  
  6     /**
  7        Constructs a student with no scores and a maximum number of scores.
  8        @capacity the maximum number of scores for this student
  9     */
 10     public Student(int capacity)
 11     {
 12        scores = new double[capacity];
 13        scoresSize = 0;
 14     }
 15  
 16     /**
 17        Adds a score for this student.
 18        @param score the score to add
 19        @return true if the score was added, false if there was no room to add the score
 20     */
 21     public boolean addScore(double score)
 22     {
 23        if (scoresSize < scores.length)
 24        {
 25           scores[scoresSize] = score;
 26           scoresSize++;
 27           return true;
 28        }
 29        else
 30        {
 31           return false;      
 32        }
 33     }
 34     
 35     /**
 36        Gets the position of the minimum score.
 37        @return the position of the smallest element of values, or -1
 38        if there are no scores.
 39     */
 40     public int minimumPosition()
 41     {
 42        if (scoresSize == 0) { return -1; }
 43        int smallestPosition = 0;
 44        for (int i = 1; i < scoresSize; i++)
 45        {
 46           if (scores[i] < scores[smallestPosition])
 47           {
 48              smallestPosition = i;
 49           }
 50        }
 51        return smallestPosition;
 52     }
 53  
 54     /**
 55        Computes the sum of the scores
 56        @return the total score
 57     */
 58     public double sum()
 59     {
 60        double total = 0;
 61        for (int i = 0; i < scoresSize; i++)
 62        {
 63           total = total + scores[i];
 64        }
 65        return total;
 66     }     
 67  
 68     /**
 69        Removes a score at a given position.
 70        @param pos the position of the score to remove
 71     */
 72     public void removeScore(int pos)
 73     {
 74        // Remove the element at this position--see Section 7.3.6
 75        scores[pos] = scores[scoresSize - 1];
 76        scoresSize--;
 77     }
 78  }
 79