Converting Letter Grade to GPA in Java

Converting Letter Grade to GPA in Java

ยท

0 min read

So, I've been taking a Beginning Java class, and our programming assignment was to write a program that would take a letter grade input from the user (i.e., "A-"), and would calculate the GPA based on that.

NOTE: We assume that this is a perfect user, who never enters the wrong thing

So I thought I'd write a blog post on it in the process!

We'll start by creating a basic Grader class:

class Grader {
    public static void main(String[] args) {

    }
}

Next, let's import a few things we're going to need. At the top of the file, enter:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

Now that the boilerplate code is there, let's start working on the actual problem.

The GPA of a letter grade is determined like so: A -> 4.0 B -> 3.0 C -> 2.0 D -> 1.0 F -> 0.0

Then, if there is a '+' or a '-', you add or subtract 0.3, respectively...

...with a couple of caveats. If you get an A+, then you don't add 0.3. And if the letter is an F, you get a 0.0 regardless of any '-' or '+'.

Let's start by defining a HashMap to store the letters and their base grades, for easy access:

//This HashMap is a nice way to store a GPA value for each letter
    private final static Map<Character, Double> letterToGPA = new HashMap<Character, Double>()
    {{
        put('A', 4.0);
        put('B', 3.0);
        put('C', 2.0);
        put('D', 1.0);
    }};

Next, let's create a method that takes a String such as "A" or "C+", and returns a double with the appropriate GPA value (I'm going to give you all the code at once, then step through it):

private static double gpaFromLetterGrade(String letterGrade) {
        //Accumulator
        double totalGrade = 0.0;

        //Store the letter portion of the grade (this way we only have to evaluate it once)
        char letter = letterGrade.charAt(0);

        //If the letter is an F, you get 0.0 no matter what, so we can shortcut to the end by simply returning the total grade
        if (letter == 'F') {
            return totalGrade;
        }

        //Set the totalGrade to the GPA of the given letter, found from the HashMap
        totalGrade = letterToGPA.get( letter );

        //If the letterGrade has a length of 2, we have a sign to evaluate
        if (letterGrade.length() == 2) {

            //Store the sign (again, so we only evaluate it once)
            char sign = letterGrade.charAt(1);

            //If the sign is negative, subtract 0.3
            // (we don't have to worry about the special case of F
            // because we dealt with that at the beginning of the method)
            if (sign == '-') {
                totalGrade -= 0.3;
            }

            //If the sign is positive, and the letter is not A, add 0.3
            // (again, we already dealt with F)
            else if (sign == '+' && letter != 'A') {
                totalGrade += 0.3;
            }

        }

        //Finally, return the totalGrade
        return totalGrade;
    }

So, that's a lot (actually, not that much, but I use a lot of whitespace and comments in my code), so let's break it down.

  1. Define an accumulator variable which will eventually contain the total grade. We start it at 0.0.
  2. Store the letter portion of the user's input (more efficient than using letterGrade.charAt(0) every time we need the letter).
  3. If the letter is an F, you get a 0.0, no matter what. Tough luck ๐Ÿ˜• (This step allows us to simplify the if statements later on)
  4. Get the base grade of the letter using our HashMap
  5. If there is a sign on the letter...
  6. Store it (same reason as with the letter)
  7. If it's a minus, subtract 0.3 (we already dealt with the case that it is an F)
  8. If it's a plus, and the letter is not A, add 0.3 (again, we already dealt with F)
  9. Return the totalGrade

We're basically done now, we just need to set up a system where we can prompt the user to enter a grade, and then tell them their GPA. We'll just do this in the main function:

//Get the letterGrade input from the user
System.out.print("Enter a letter grade (i.e., 'A-'): ");

//Create a Scanner instance
Scanner in = new Scanner(System.in);

//Get the letterGrade string input from the user
String letterGrade = in.next();

//Get the GPA of that letterGrade using the gpaFromLetterGrade method
double GPA = gpaFromLetterGrade(letterGrade);

//Print the GPA
System.out.print("Your GPA is: ");
System.out.println(GPA);

We ask the user for a letter grade, then create an instance of Scanner, and retrieve the user's input as a String. Then we use the function we created previously to get the GPA, and finally we print it to the user.

This was a pretty simple problem, but perhaps you learned something from it? Thanks for reading ๐Ÿ˜€