Explain the working of java gpa calculator

513    Asked by RoseKnox in Java , Asked on Oct 14, 2022

 This is a program I typed up for a project pertaining to GPA calculation. However, I want to find an alternative to if statements that are NOT conditionals, loops, or arrays. Is there an alternative solution to changing this format?

import java.util.Scanner; public class Part1 { public static void main(String args[]) { String grade = ""; String[] letters = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"}; // Creates strings above and the GPA values below double[] grades = {4.33, 4.00, 3.67, 3.33, 3.00, 2.67, 2.33, 2.00, 1.67, 1.00, 0.00}; double credit1; double credit2; double credit3; double credit4; double gradeValue = 0; // Creates 4 credits double totPtsClass1 = 0; double totPtsClass2 = 0; double totPtsClass3 = 0; double totPtsClass4 = 0; double totPts = 0; double totalCredits = 0; double gpa; // Points in classes, GPA System.out.println("Welcome to the UNG GPA Calculator!"); String message; Scanner scan = new Scanner(System.in); System.out.println("Enter your first name: "); message = scan.nextLine(); System.out.println("Enter your last name: "); message = scan.nextLine(); System.out.println("Enter your major: "); message = scan.nextLine(); System.out.println("Enter the number of your first class: "); message = scan.nextLine(); Scanner console = new Scanner(System.in); System.out.println("Please enter the number of credits of the class 1 (A number)"); credit1 = console.nextDouble(); System.out.println("Please enter your grades for the class 1(Capital letters such as A,B+, C-)"); grade = console.next(); if (grade.equals("A")) gradeValue = 4.00; else if (grade.equals("A-")) gradeValue = 3.67; else if (grade.equals("B+")) gradeValue = 3.33; else if (grade.equals("B")) gradeValue = 3.00; else if (grade.equals("B-")) gradeValue = 2.67; else if (grade.equals("C+")) gradeValue = 2.33; else if (grade.equals("C")) gradeValue = 2.00; else if (grade.equals("D+")) gradeValue = 1.33; else if (grade.equals("D")) gradeValue = 1.00; else if (grade.equals("F")) gradeValue = 0; else if (grade.equals("FX")) gradeValue = 0; else System.out.println("Invalid Grade"); totPtsClass1 = gradeValue * credit1; System.out.println("Please enter the number of credits of the class 2 (A number)"); credit2 = console.nextDouble(); System.out.println("Please enter your grades for the class 2 (Capital letters such as A,B+, C-)"); grade = console.next(); if (grade.equals("A")) gradeValue = 4.00; else if (grade.equals("A-")) gradeValue = 3.67; else if (grade.equals("B+")) gradeValue = 3.33; else if (grade.equals("B")) gradeValue = 3.00; else if (grade.equals("B-")) gradeValue = 2.67; else if (grade.equals("C+")) gradeValue = 2.33; else if (grade.equals("C")) gradeValue = 2.00; else if (grade.equals("D+")) gradeValue = 1.33; else if (grade.equals("D")) gradeValue = 1.00; else if (grade.equals("F")) gradeValue = 0; else if (grade.equals("FX")) gradeValue = 0; else System.out.println("Invalid Grade"); totPtsClass2 = gradeValue * credit2; System.out.println("Please enter the number of credits of the class 3 (A number)"); credit3 = console.nextDouble(); System.out.println("Please enter your grades for the class 3 (Capital letters such as A,B+, C-)"); grade = console.next(); if (grade.equals("A")) gradeValue = 4.00; else if (grade.equals("A-")) gradeValue = 3.67; else if (grade.equals("B+")) gradeValue = 3.33; else if (grade.equals("B")) gradeValue = 3.00; else if (grade.equals("B-")) gradeValue = 2.67; else if (grade.equals("C+")) gradeValue = 2.33; else if (grade.equals("C")) gradeValue = 2.00; else if (grade.equals("D+")) gradeValue = 1.33; else if (grade.equals("D")) gradeValue = 1.00; else if (grade.equals("F")) gradeValue = 0; else if (grade.equals("FX")) gradeValue = 0; else System.out.println("Invalid Grade"); totPtsClass3 = gradeValue * credit3; System.out.println("Please enter the number of credits of the class 4 (A number)"); credit4 = console.nextDouble(); System.out.println("Please enter your grades for the class 4 (Capital letters such as A,B+, C-)"); grade = console.next(); if (grade.equals("A")) gradeValue = 4.00; else if (grade.equals("A-")) gradeValue = 3.67; else if (grade.equals("B+")) gradeValue = 3.33; else if (grade.equals("B")) gradeValue = 3.00; else if (grade.equals("B-")) gradeValue = 2.67; else if (grade.equals("C+")) gradeValue = 2.33; else if (grade.equals("C")) gradeValue = 2.00; else if (grade.equals("D+")) gradeValue = 1.33; else if (grade.equals("D")) gradeValue = 1.00; else if (grade.equals("F")) gradeValue = 0; else if (grade.equals("FX")) gradeValue = 0; else System.out.println("Invalid Grade"); totPtsClass4 = gradeValue * credit4; totPts = totPtsClass1 + totPtsClass2 + totPtsClass3 + totPtsClass4; totalCredits = credit1 + credit2 + credit3 + credit4; gpa = totPts / totalCredits; System.out.printf("Your GPA is: %.2fn", +gpa); } }

I would like a solution other than the three above, but I'm unfamiliar with anything to substitute for all those if then statements.

Answered by Roy McDonald

Regarding the JAVA GPA calculator, put the Grade-Score mapping in a HashMap and read it from there.

Do this once:

Map gradeToScore = new HashMap<>(); gradeToScore.put("A", 4.00); gradeToScore.put("A-", 3.67); gradeToScore.put("B+", 3.33); gradeToScore.put("B", 3.00); gradeToScore.put("B-", 2.67); gradeToScore.put("C+", 2.33); gradeToScore.put("C", 2.00); gradeToScore.put("D+", 1.33); gradeToScore.put("D", 1.00); gradeToScore.put("F", 0.0); gradeToScore.put("FX", 0.0);

Then you can use it as many times as needed:

if(gradeToScore.containsKey(grade)) { gradeValue = gradeToScore.get(grade); }else{ System.out.println("Invalid Grade"); }

If you want to go several steps further in optimizing things for readability, you can get to something like this:

  public class GradeCalculator { //MAIN public static void main(String args[]) { GradeCalculator calc = new GradeCalculator(); calc.startInteraction(); } //DATA CLASS private static class Student{ public String firstName; public String lastName; public String major; public String numFirstClass; public final List credits = new ArrayList&lt;>(); public final List points = new ArrayList&lt;>(); } //PROPERTIES & CONSTRUCTOR private final Map gradeToScore = new HashMap&lt;>(); public GradeCalculator() { gradeToScore.put("A+", 4.33); gradeToScore.put("A", 4.00); gradeToScore.put("A-", 3.67); gradeToScore.put("B+", 3.33); gradeToScore.put("B", 3.00); gradeToScore.put("B-", 2.67); gradeToScore.put("C+", 2.33); gradeToScore.put("C", 2.00); gradeToScore.put("C-", 1.67); gradeToScore.put("D+", 1.33); gradeToScore.put("D", 1.00); gradeToScore.put("F", 0.0); gradeToScore.put("FX", 0.0); } //PUBLIC public void startInteraction() { System.out.println("Welcome to the UNG GPA Calculator!"); try(Scanner scan = new Scanner(System.in);){ Student student = new Student(); loadStudentInfo(student, scan); loadCreditsAndPoints(student, scan, 1); loadCreditsAndPoints(student, scan, 2); loadCreditsAndPoints(student, scan, 3); loadCreditsAndPoints(student, scan, 4); double gpa = calculateGpa(student); System.out.printf("Your GPA is: %.2f
", +gpa); } } //PRIVATE private void loadStudentInfo(Student student, Scanner scan) { System.out.println("Enter your first name: "); student.firstName = scan.nextLine(); System.out.println("Enter your last name: "); student.lastName = scan.nextLine(); System.out.println("Enter your major: "); student.major = scan.nextLine(); System.out.println("Enter the number of your first class: "); student.numFirstClass = scan.nextLine(); } private void loadCreditsAndPoints(Student student, Scanner scan, int classNumber) { System.out.println("Please enter the number of credits of the class " + classNumber + " (A number)"); double credit = scan.nextDouble(); student.credits.add( credit ); System.out.println("Please enter your grades for the class " + classNumber + " (Capital letters such as A,B+, C-)"); String grade = scan.nextLine(); double gradeValue = 0; if(gradeToScore.containsKey(grade)) { gradeValue = gradeToScore.get(grade); }else{ System.out.println("Invalid Grade"); } double classTotPts = gradeValue * credit; student.points.add(classTotPts); } private double calculateGpa(Student student) { double totalCredits = getTotal(student.credits); double totPts = getTotal(student.points); double gpa = totPts / totalCredits; return gpa; } private double getTotal(List doubles) { double total = 0; for(Double d:doubles) { total += d; } return total; } }

And, I would prefer to stay away from streams and lambdas till you completely nail down core Java.



Your Answer

Interviews

Parent Categories