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        Computes the sum of the scores of this student.
 37        @return the sum of the scores
 38     */
 39     public double sum()
 40     {
 41        double total = 0;
 42        for (int i = 0; i < scoresSize; i++)
 43        {
 44           total = total + scores[i];
 45        }
 46        return total;
 47     }
 48        
 49     /**
 50        Gets the minimum score of this student.
 51        @return the minimum score, or 0 if there are no scores.
 52     */
 53     public double minimum()
 54     {
 55        if (scoresSize == 0) { return 0; }
 56        double smallest = scores[0];
 57        for (int i = 1; i < scoresSize; i++)
 58        {
 59           if (scores[i] < smallest)
 60           {
 61              smallest = scores[i];
 62           }
 63        }
 64        return smallest;
 65     }
 66  
 67     /**
 68        Gets the final score for this student.
 69        @return the sum of the scores, with the lowest score dropped if 
 70        there are at least two scores, or 0 if there are no scores.
 71     */
 72     public double finalScore() 
 73     {
 74        if (scoresSize == 0)
 75        {
 76           return 0;
 77        }
 78        else if (scoresSize == 1)
 79        {
 80           return scores[0];
 81        }
 82        else
 83        {
 84           return sum() - minimum();
 85        }
 86     }
 87  }
 88