// Fig 4.9 
// Class average program with sentinel-controlled repetition.

// Java core packages
import java.text.DecimalFormat;

// Java extension packages
import javax.swing.JOptionPane; // import the JOption Pane from the Swing extension

public class Average2
{
	// main method
	public static void main ( String args[] )
	{
	int gradeCounter, // number of grades entered
		gradeValue,	  // initialize the gradeValue
		total;	 	  // sum of the grades input by the user
	double average;	  // aveage of the grades
	String input; 	  // the string value entered
	
	// initialization phase
	total = 0;	// initialize total to zero
	gradeCounter = 0;	// initialize the counter to zero
	
	// Processing Phase
	// the instructions for input
	input = JOptionPane.showInputDialog( 
	"Start input of grades, to terminate enter -1" );
	
	// convert grade from a sString into an integer
	gradeValue = Integer.parseInt( input );
	
	// while loop control	
	while ( gradeValue != -1 )
		{
			// add grade value to the total
			total = total + gradeValue;
			
			// add 1 to gradeCounter
			gradeCounter = gradeCounter + 1;
			
			// prompt for next input and read grade from user
			input = JOptionPane.showInputDialog (
					 "Enter integer grade, -1 to Quit: " );
					 
			// convert grade from a String into an integer
			gradeValue = Integer.parseInt( input );
		} // end the while loop
		
	// termination phase
	DecimalFormat twoDigits = new DecimalFormat ( "0.00" );
	
	if ( gradeCounter != 0 )
		{
			average = (double) total / gradeCounter; // performs integer division
			
			// Display the average of exam grades
			JOptionPane.showMessageDialog( null,
			"Class average is: " + twoDigits.format ( average ),
			"Class average", JOptionPane.INFORMATION_MESSAGE );
			
		}
		else
			JOptionPane.showMessageDialog( null,
			"No grades were entered", "Class Average",
			JOptionPane.INFORMATION_MESSAGE );
	
	// Termination Phase
	System.exit( 0 ); // teminate the program

	} // ends main method

} // ends class Sentinel