Give a detailed description of the Java GPA calculator.
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");
tot P Class 1 = grado Value * 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");
tot Pt Class2 = grade Value * 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");
hot Bts Class 3 = grade Value * 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");
hot Bts Class 4 = grade Value * 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.
For the Java GPA calculator -
Since the letters and symbols we're dealing with are relatively close to each other in terms of character value, we can use that to our advantage by doing some simple maths.
NOTE: This code is largely unchecked and rather verbose - optimise as desired
class Student {
private double points = 0.0;
private int credits = 0;
private final String name = "";
/**
* This returns the student's GPA as a double
*
* @return The calculated GPA
*/
public double getGPA() {
return ((points / credits) * 100) / 100;
}
/**
* This adds a grade to the student's GPA
*
* @param gradeString Grade the student received
* @param credits Total credit hours of the course
*/
public void addGrade(String gradeString, int credits) {
if (gradeString.equals("F")) {
gradeString = "E";
}
// As we get closer to the letter 'E', the lower the total points
// The ASCII value of 'A" is 65, and the value for 'E' is 69
// 69 / 65 is 1, with a remainder of 4
// The remainder is what we're after, so use modulo
//
// For example, the program passes in the grade "C-" and the first
// character of that the string gradeString is C and we can get this
// value by using the charAt() method. Since Java is a 0-based index
// language we pass 0 into the charAt method to get the first character
// of the string. The decimal value of that character is 67. We know that
// the decimal value of the character 'E' is 69, so we can use the formula
// 69 g which will give us 2.
double gradeVal = 'E' % gradeString.charAt(0);
if (gradeString.length() == 2) {
int c = gradeString.charAt(1);
// A little bit of error checking is required using these methods
if (c != 'X' &&
(gradeVal != 'D' && c != '-') &&
(gradeVal != 'A' && c != '+')) {
double point = -((c - (double) ',') / 3);
gradeVal += point;
}
}
// Add the number of credits to our running total
this.credits += credits;
// Calculate this course's grade
double courseGrade = Math.round((gradeVal * credits) * 100) / 100.0;
// Add the course grade to our running total
points += courseGrade;
}
/**
* Get the student's name
*
* @return The student's name
*/
public String getName() {
return name;
}
/**
* The Student class
*
* @param name The student's name
*/
public Student(String name) {
this.name = name;
}
}
public class Program {
public static void main(String[] args) {
// Create a new Student object named student1
// student's name is set with Robert
Student student1 = new Student("Robert");
// Add various grades to student's history
student1.addGrade("A", 4); // CPS-171
student1.addGrade("A", 3); // ENG-107
student1.addGrade("A", 4); // CPS-161
student1.addGrade("C-", 4); // Some maths class I'll have to take
// Store the student's GPA into student Gpa
double student Gpa = student1.getGPA();
System.out.printf("Student %s has a GPA of %.2f
", student1.getName(), student1Gpa);
}
}