/*
 * Exams.java      Authors: Koffman & Wolz
 * Class that can calculate average score on an exam.
 * Uses class JOptionPane.
 */
import javax.swing.JOptionPane;

public class Exams {
   // Data fields
   private int sum;        // sum of scores
   private int count;      // count of scores
 
   // methods
   public static int readInt(String prompt) {
     String numStr = JOptionPane.showInputDialog(prompt);
     return Integer.parseInt(numStr);
   }

   // preconditions: count and sum are zero
   // postconditions: data field sum stores the sum of scores
   //   data field count stores the count of scores
   public void findSumAndCount() {
     int score;
     int SENTINEL = -1;   // sentinel value
     score = readInt("Enter next score or " + SENTINEL +
                     " to stop");       // initialize lcv
     while (score != SENTINEL) {        // test lcv
       sum += score;                    // add current score to sum
       count++;                         // increase count by 1
       System.out.println("score: " + score);
       score = readInt("Enter next score or " + SENTINEL +
                       " to stop");     // update lcv
     }
   }

   // precondition: sum and count are defined
   // postcondition: Returns the average score
   public  double findAve() {
     double average;      // average score
 
     if (count > 0)
        average = (double) sum / (double) count;
     else
        average = 0;
     return average;
   }
}



