//An instance of this class represents an FIU letter grade // public class FIUCourseGrade { public static final double ONE_THIRD = 1.0/3.0; //Instance Variable private String grade; //Constructor // Legal Letter Grades: A, A-, B+, B, B-, C+, C, D, F public FIUCourseGrade(String grade) { if ( grade.length() < 1 || grade.length() > 2 ) //Length check: 1 or 2 throw new RuntimeException("Invalid Length"); if ( !hasValidLetter(grade) ) //Letter Check: A, B, C, D, or F throw new RuntimeException("Invalid Letter"); if ( grade.length() == 2 && !hasValidSuffix(grade))//Suffix Check: + or - throw new RuntimeException("Invalid Suffix"); this.grade = grade; } //Accessor public String getGrade() { return this.grade; } //Query Method // @return grade-points corresponding to this grade public double gradePoints() { double points = 0.0; //Calculate the "letter" points: 4, 3, 2, 1, 0 switch ( this.grade.charAt(0) ) { case 'A' : points = 4.0; break; case 'B' : points = 3.0; break; case 'C' : points = 2.0; break; case 'D' : points = 1.0; break; } //Modify by +/- 1/3 if there is a suffix if (this.grade.length() == 2) switch (this.grade.charAt(1)) { case '+' : points += ONE_THIRD; break; case '-' : points -= ONE_THIRD; } return points; } //Helper Method // @param grade: a String to be validated as a letter grade // @return true IFF the 1st char is legitimate: A, B, C, D, F private static boolean hasValidLetter(String grade) { switch ( grade.charAt(0) ) { case 'A' : case 'B' : case 'C' : case 'D' : case 'F' : return true; default : return false; } } //Helper Method // @param grade: a String to be validated as a letter grade // @return true IFF the 2nd char, if any, is legitimate: + or - private static boolean hasValidSuffix(String grade) { if (grade.length() != 2) return false; char letter = grade.charAt(0); char suffix = grade.charAt(1); switch ( suffix ) { case '+' : return (letter == 'B' || letter == 'C'); case '-' : return (letter == 'A' || letter == 'B'); default : return false; } } }