1 /* 2 This application demonstrate various control structures and additional 3 I/O methods 4 */ 5 6 import java.io.*; 7 8 public class GradeAverage 9 { 10 public static void main (String args[]) throws IOException 11 { 12 // declaring a constant variable 13 final int NUM = 5; 14 15 char lettergrade; 16 int total = 0; 17 int counter = 1; 18 double average; 19 20 // while loop and if tests 21 while (counter <= NUM) 22 { System.out.print("Enter letter grade: " ); 23 // read a byte from standard input and convert to a char 24 lettergrade = (char)System.in.read(); 25 if (lettergrade == 'A') 26 total += 4; 27 else if (lettergrade == 'B') 28 total += 3; 29 else if (lettergrade == 'C') 30 total += 2; 31 else if (lettergrade == 'D') 32 total += 1; 33 34 // skips one byte (the return) of standard input 35 System.in.skip (1); 36 counter ++; 37 } 38 39 // cast to get floating point arithmetic 40 average = (double) total/NUM; 41 System.out.println ("Class average is " + average ); 42 } 43 }