1  import java.util.Scanner;
  2  
  3  /**
  4     This program reads inputs that contain four grades per line and displays the
  5     average grade for each input line.
  6  */
  7  public class Grades
  8  {
  9     public static void main(String[] args)
 10     {
 11        while (processLine())
 12        {
 13        }
 14     }
 15  
 16     /**
 17        Processes one line of input.
 18        @return true if the sentinel was not encountered
 19     */
 20     public static boolean processLine()
 21     {
 22        System.out.print("Enter four grades or Q to quit: ");
 23        Scanner in = new Scanner(System.in);
 24  
 25        // Read four grades
 26  
 27        String g1 = in.next();
 28        if (g1.equals("Q")) { return false; }
 29        String g2 = in.next();
 30        String g3 = in.next();
 31        String g4 = in.next();
 32  
 33        // Compute and print their average
 34  
 35        double x1 = gradeToNumber(g1);
 36        double x2 = gradeToNumber(g2);
 37        double x3 = gradeToNumber(g3);
 38        double x4 = gradeToNumber(g4);
 39        double xlow = min(x1, x2, x3, x4);
 40        double avg = (x1 + x2 + x3 + x4 - xlow) / 3;
 41        System.out.println(numberToGrade(avg));
 42        return true;
 43     }
 44  
 45     /**
 46        Converts a letter grade to a number.
 47        @param grade a letter grade (A+, A, A-, ..., D-, F)
 48        @return the equivalent number grade
 49     */
 50     public static double gradeToNumber(String grade)
 51     {
 52        double result = 0;
 53        String first = grade.substring(0, 1);
 54        if (first.equals("A")) { result = 4; }
 55        else if (first.equals("B")) { result = 3; }
 56        else if (first.equals("C")) { result = 2; }
 57        else if (first.equals("D")) { result = 1; }
 58        if (grade.length() > 1)
 59        {
 60           String second = grade.substring(1, 2);
 61           if (second.equals("+"))
 62           {
 63              result = result + 0.3;
 64           }
 65           else if (second.equals("-"))
 66           {
 67              result = result - 0.3;
 68           }
 69        }
 70        return result;
 71     }
 72     
 73     /**
 74        Converts a number to the nearest letter grade.
 75        @param x a number between 0 and 4.3
 76        @return the nearest letter grade
 77     */
 78     public static String numberToGrade(double x)
 79     {
 80        if (x >= 4.15) { return "A+"; }
 81        if (x >= 3.85) { return "A"; }
 82        if (x >= 3.5) { return "A-"; }
 83        if (x >= 3.15) { return "B+"; }
 84        if (x >= 2.85) { return "B"; }
 85        if (x >= 2.5) { return "B-"; }
 86        if (x >= 2.15) { return "C+"; }
 87        if (x >= 1.85) { return "C"; }
 88        if (x >= 1.5) { return "C-"; }
 89        if (x >= 1.15) { return "D+"; }
 90        if (x >= 0.85) { return "D"; }
 91        if (x >= 0.5) { return "D-"; }
 92        return "F";
 93     }
 94  
 95     /**
 96        Returns the smallest of four numbers.
 97        @param a a number
 98        @param b a number
 99        @param c a number
100        @param d a number
101        @return the smallest of a, b, c, and d
102     */
103     public static double min(double a, double b, double c, double d) 
104     { 
105        return Math.min(Math.min(a, b), Math.min(c, d));
106     }
107  }