// Fig. 2.9: Average.java
// Class average application with
// sentinel-controlled repetition.
import java.io.*;

public class Average {
   public static void main( String args[] ) throws IOException
   {
      double average;  // number with decimal point
      int counter, grade, total;
   
      // initialization phase
      total = 0;
      counter = 0;
   
      // processing phase
      System.out.print( "Enter letter grade, Z to end: " );
      grade = System.in.read();

      while ( grade != 'Z' ) {
         if ( grade == 'A' ) 
            total = total + 4;
         else if ( grade == 'B' ) 
            total = total + 3;
         else if ( grade == 'C' ) 
            total = total + 2;
         else if ( grade == 'D' ) 
            total = total + 1;

         System.in.skip( 2 );
         counter = counter + 1;
         System.out.print( "Enter letter grade, Z to end: " );
         grade = System.in.read();
      }
   
      // termination phase
      if ( counter != 0 ) {
         average = (double) total / counter;
         System.out.println( "Class average is " + average );
      }
      else
         System.out.println( "No grades were entered" );
   }
}

