//-----------------------------------------------------------------------------
// Grades Example
// CS100 Spring 2000
// David I. Schwartz
//-----------------------------------------------------------------------------
// This program processes grades. The user inputs a sequence of grades. 
// The sequence terminates when the user enters a negative grade.
// The program counts the number of grades, reports min/max,
// and computes an average.
//-----------------------------------------------------------------------------
// Sample problems
// ===============
// Rewrite code for:
//   + terminating if an input grade exceeds 100
//   + using -1 as a stopping value
//   + finding most common grade, assuming a sorted input sequence
//   + computing biggest "gap" between grades, assuming a sorted sequence
//-----------------------------------------------------------------------------

import java.text.DecimalFormat;

public class grades {
    public static void main(String[] args) {

	//---------------------------------------------------------------------
	// Setup for user input
	//---------------------------------------------------------------------
	TokenReader in = new TokenReader(System.in);

	//---------------------------------------------------------------------
	// Declare and initialize variables to store information
	//---------------------------------------------------------------------
	int count;
	double max, min, sum, grade, average;

	// We need a grade to start the analysis
	System.out.print("Enter a grade: ");
	grade = in.readDouble();

	count = 0;    // count of grades so far
	max = grade;  // max grade so far -- use initial grade
	min = grade;  // min grade so far -- use initial grade
	sum = 0;      // sum of grades so far
	average = 0;  // average of grades so far

	//---------------------------------------------------------------------
	// Loop for processing input and computing desired values
	// Stop loop when user enters a negative value
	//---------------------------------------------------------------------
	while(grade >= 0) { 

	    //***************************
	    // REPETITION
	    // Echo current grade
	    //***************************
	    System.out.println("You entered the grade: " + grade);

	    //****************************
	    // ACCUMULATION
	    // Count the grades so far
	    // Sum the grades so far
	    //****************************
	    count = count + 1;
	    sum = sum + grade;

	    //****************************
	    // CONDITIONAL UPDATE
	    // Determine min/max of grades
	    //****************************
	    if(grade < min)
		min = grade;
	    if(grade > max)
		max = grade;

	    //****************************
	    // Input the next grade
	    //****************************
	    System.out.print("\nEnter a grade: ");
	    grade = in.readDouble();
	    if(grade < 0)
		System.out.println("Game over. I'm stopping....");

	}

	//---------------------------------------------------------------------
	// Output results
	// Format to two decimal places
	//---------------------------------------------------------------------
	DecimalFormat fmt = new DecimalFormat("0.##");
	if((max < 0 || min < 0) || count==0)
	    System.out.println("\nNo values entered");
	else {
	    System.out.println();
	    System.out.println("Number of grades:  " + count);
	    System.out.println("Average of grades: " + fmt.format(sum/count));
	    System.out.println("Maximum grade:     " + max);
	    System.out.println("Minimum grade:     " + min);
	}

    } // method main

} // class grades
	



