/** * Grade * * Convert from numerical grade to letter grade, using * the standard high-school scale (0-59 F, 60-62 D-, ...) * * David Liben-Nowell * CS 127 (Winter 2006, Carleton College) */ class Grade { // notice that this method must be static in order for the // main method below to invoke it. If you remove "static", you'll get the // following compilation error message: // "non-static method toGrade(int) cannot be referenced from // a static context" public String toGrade(int grade) { String str; if (grade >= 90) { str = "A"; } else if (grade >= 80) { str = "B"; } else if (grade >= 70) { str = "C"; } else if (grade >= 60) { str = "D"; } else { str = "F"; } // A "minus" grade is x0, x1, or x2 where x is one of 6/7/8/9. if ((grade % 10 <= 2) && (grade >= 60) && (grade <= 92)) { str = str + "-"; } // added after class: // A "plus" grade is 100, or x8 or x9 where x is one of 6/7/8/9. if (((grade % 10 >= 8) && (grade >= 60)) || (grade >= 98)) { str = str + "+"; } return str; } public static void main(String[] args) { // the following was also added after class; it's a loop to // show grades for all scores between 0 and 100. int grade = 0; while (grade <= 100) { System.out.println("A " + grade + " is a " + toGrade(grade)); grade = grade + 1; } } }