/* ExamStatistics.java        Authors: Koffman & Wolz
 * Class for storing array of scores and doing statistics
 * Uses KeyIn
 */
import psJava.KeyIn;

public class ExamStatistics {
  // Data Fields
  private int[] scores;       // scores is an array of ints

  // Methods
  // postcondition: scores references an array object with num 
  //   type int elements. 
  public ExamStatistics(int num) {
     // Allocate storage for the array.
     scores = new int[num];
  }


  // postcondition: reads data into array scores.
  public void readScores() {
    // Read the scores
    for (int i = 0; i < scores.length; i++)
       scores[i] = KeyIn.readInt("Score for next student:");
  }

 
  // postcondition: returns the smallest score. 
  public int findMin() {
    // Find smallest score if array is not empty.
    if (scores.length > 0) {
       int minSoFar = scores[0];   // Remember first score

       // Remember any smaller scores.
       for (int i = 1; i < scores.length; i++) {
          if (scores[i] < minSoFar)
             minSoFar = scores[i];
       }
       return minSoFar;
    }
    else
       return 0;   // array is empty
  }


  // postcondition: returns the largest score.
  public int findMax() {
    // Find largest score if array is not empty.
    if (scores.length > 0) {
       int maxSoFar = scores[0];   // Remember first score
 
       // Remember any larger scores.
       for (int i = 1; i < scores.length; i++) {
          if (scores[i] > maxSoFar)
             maxSoFar = scores[i];
       }
       return maxSoFar; 
    }
    else
       return 0;   // array is empty
  }


  // postcondition: returns the mean of the scores
  public double computeMean() {
     int sum = 0;
     for (int i = 0; i < scores.length; i++)
        sum += scores[i];

    if (scores.length > 0)
        return (double)sum / (double)scores.length;
    else
       return 0;
  }


  // precondition: the mean score is defined.
  // postcondition: returns the standard deviation.
  public double computeStandDev(double mean) {
     // Compute sum of scores-squared
     int sumSquares = 0;
     for (int i = 0; i < scores.length; i++)
        sumSquares += scores[i] * scores[i];

    if (scores.length > 0)
       return Math.sqrt((double) sumSquares / scores.length - 
                        mean * mean);
    else
       return 0;
  }


  // postcondition: returns a String containing exam scores.
  public String toString () {
    
    String examStr = "";
    for (int i = 0; i < scores.length; i++) {
       examStr = examStr + scores[i] + "   ";
    }
    return examStr;
  }
}

