Allan Didier

Computer Science Teacher

# GPA Calculator

#### Goals

Write a Java program that calculates a student’s GPA.

#### Skills

• While loops
• Switch statements

#### Program Details

Write a Java program that asks the user to enter seven different grades and then calculates the GPA from those grades.

1. Grades are entered as letters, upper or lowercase.
2. Read grades in as a string, but convert them to a character. Use the .charAt(0) command to do this.
3. Use a Switch statement to convert letter grades to numbers with the following conversion:
1. A = 4 points
2. B = 3 points
3. C = 2 points
4. D = 1 point
5. F = zero points
4. GPA is calculated by adding up the total points and dividing by the number of classes.
5. Use a while loop with a counter variable to input the grades.
6. Invalid grades should not be counted in the calculation of the GPA.
7. Output the final GPA. You do not have to output the letter grades.

Testing:

• Make sure to test with all grade input options, including upper/lower case and invalid ones.
• Verify the GPA calculation is correct.

Hints:

• You can read in either a Char or String. Strings are sometimes easier to work with, though, and won’t crash with numbers, words, or strange characters.
• You do not necessarily have to save the grades as individual variables. You can just run your calculations on them after you read them in.

#### Resources

Edhesive Term 1

Lesson 17: While loops

Lesson 19: While loops with modular

Lesson 20: While loops with flag