patternjavaMinor
Simplifying GPA calculator
Viewed 0 times
gpacalculatorsimplifying
Problem
I'm only two weeks into learning Java and just finished creating my first program. I have a feeling I've added unnecessary things, but not sure. I'd like someone to take a look at it and let me know if there are ways to simplify or approach the code. Basically, I'm just trying to create a simple GPA calculator.
```
import java.util.Scanner;
class GpaCalculator {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
char c1;
double gpa;
double g1, g2, g3, g4, g5, g6;
g1 = 0;
g2 = 0;
g3 = 0;
g4 = 0;
g5 = 0;
g6 = 0;
System.out.println("What is your grade for class 1?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g1 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g1 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g1 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g1 = 1;
}
if (c1 == 'f' || c1 == 'F') {
g1 = 0;
}
System.out.println("What is your grade for class 2?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g2 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g2 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g2 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g2 = 1;
}
if (c1 == 'f' || c1 == 'F') {
g2 = 0;
}
System.out.println("What is your grade for class 3?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g3 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g3 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g3 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g3 = 1;
}
if (c1 == 'f' || c1 == 'F') {
```
import java.util.Scanner;
class GpaCalculator {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
char c1;
double gpa;
double g1, g2, g3, g4, g5, g6;
g1 = 0;
g2 = 0;
g3 = 0;
g4 = 0;
g5 = 0;
g6 = 0;
System.out.println("What is your grade for class 1?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g1 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g1 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g1 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g1 = 1;
}
if (c1 == 'f' || c1 == 'F') {
g1 = 0;
}
System.out.println("What is your grade for class 2?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g2 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g2 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g2 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g2 = 1;
}
if (c1 == 'f' || c1 == 'F') {
g2 = 0;
}
System.out.println("What is your grade for class 3?");
c1 = myScanner.findWithinHorizon(".", 0).charAt(0);
if (c1 == 'a' || c1 == 'A') {
g3 = 4;
}
if (c1 == 'b' || c1 == 'B') {
g3 = 3;
}
if (c1 == 'c' || c1 == 'C') {
g3 = 2;
}
if (c1 == 'd' || c1 == 'D') {
g3 = 1;
}
if (c1 == 'f' || c1 == 'F') {
Solution
First off, both
This will allow a much simpler calculation loop :
Note that I've hidden away the scanner behind a UserInterface interface, in order to separate calculation from UI code.
It's implemented by the ConsoleUI class :
The Main class simply ties everything together :
Grade and Class (GradedClass to avoid confusion with java.lang.Class) make sense as enums.enum Grade {
A(4),B(3),C(2),D(1),F(0);
private final int value;
Grade(int value) {
this.value = value;
}
int getValue() {
return value;
}
}
enum GradedClass {
ONE, TWO, THREE, FOUR, FIVE, SIX;
@Override
public String toString() {
return "class " + (ordinal() + 1);
}
}This will allow a much simpler calculation loop :
public double calculateGpa() {
return calculateTotal() / GradedClass.values().length;
}
private double calculateTotal() {
double total = 0d;
for (GradedClass gradedClass : GradedClass.values()) {
Grade grade = userInterface.askGradeFor(gradedClass);
total += grade.getValue();
}
return total;
}Note that I've hidden away the scanner behind a UserInterface interface, in order to separate calculation from UI code.
It's implemented by the ConsoleUI class :
class ConsoleUI implements UserInterface {
Scanner myScanner = new Scanner(System.in);
@Override
public Grade askGradeFor(GradedClass gradedClass) {
System.out.println(MessageFormat.format("What is your grade for {0}?", gradedClass));
String input = myScanner.findWithinHorizon(".", 0);
return Grade.valueOf(input.toUpperCase());
}
@Override
public void presentGpa(double gpa) {
if (gpa > 3d) {
System.out.println("Great job, you're on your way to success.");
} else if (gpa > 2d) {
System.out.println("You did OK, but better than average.");
} else {
System.out.println("You didn't do too well. You should look into getting a tutor.");
}
}
}The Main class simply ties everything together :
public class Main {
public static void main(String[] args) {
UserInterface ui = new ConsoleUI();
GpaCalculator gpaCalculator = new GpaCalculator(ui);
double gpa = gpaCalculator.calculateGpa();
ui.presentGpa(gpa);
}
}Code Snippets
enum Grade {
A(4),B(3),C(2),D(1),F(0);
private final int value;
Grade(int value) {
this.value = value;
}
int getValue() {
return value;
}
}
enum GradedClass {
ONE, TWO, THREE, FOUR, FIVE, SIX;
@Override
public String toString() {
return "class " + (ordinal() + 1);
}
}public double calculateGpa() {
return calculateTotal() / GradedClass.values().length;
}
private double calculateTotal() {
double total = 0d;
for (GradedClass gradedClass : GradedClass.values()) {
Grade grade = userInterface.askGradeFor(gradedClass);
total += grade.getValue();
}
return total;
}class ConsoleUI implements UserInterface {
Scanner myScanner = new Scanner(System.in);
@Override
public Grade askGradeFor(GradedClass gradedClass) {
System.out.println(MessageFormat.format("What is your grade for {0}?", gradedClass));
String input = myScanner.findWithinHorizon(".", 0);
return Grade.valueOf(input.toUpperCase());
}
@Override
public void presentGpa(double gpa) {
if (gpa > 3d) {
System.out.println("Great job, you're on your way to success.");
} else if (gpa > 2d) {
System.out.println("You did OK, but better than average.");
} else {
System.out.println("You didn't do too well. You should look into getting a tutor.");
}
}
}public class Main {
public static void main(String[] args) {
UserInterface ui = new ConsoleUI();
GpaCalculator gpaCalculator = new GpaCalculator(ui);
double gpa = gpaCalculator.calculateGpa();
ui.presentGpa(gpa);
}
}Context
StackExchange Code Review Q#28406, answer score: 7
Revisions (0)
No revisions yet.