Solution Review: Letter Grade to GPA
In this review, solution of the challenge 'Letter Grade to GPA' from the previous lesson is provided.
We'll cover the following...
Solution: Is your GPA correct? #
Press + to interact
class gpaHelper{public static double letterToGPA (String grade) {double answer;switch (grade) {case "A+":case "A":answer = 4;break;case "A-":answer = 3.7;break;case "B+":answer = 3.3;break;case "B":answer = 3;break;case "B-":answer = 2.8;break;case "C+":answer = 2.5;break;case "C":answer = 2.0;break;case "C-":answer = 1.8;break;case "D":answer = 1.5;break;case "F":answer = 0;break;default:answer = -1;}return answer;}public static void main( String args[] ) {System.out.println("Grade A: " + letterToGPA("A"));System.out.println("Grade D: " + letterToGPA("D"));System.out.println("Grade L: " + letterToGPA("L"));}}
Understanding your Code
Line 3
-
The method
letterToGPA
is declared static so it can be called without creating an object. -
The method takes ...