Instantly share code, notes, and snippets.

# eogas/Calc.javaSecret forked from anonymous/Calc.java Last active Dec 13, 2015

 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Calc { public static void main(String[] args) throws IOException { System.out .println("Enter the total number of courses you are going to enter:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); calculate(i); } public static void calculate(int i) throws IOException { String[] courses = new String[i]; /* store the course name */ String[] gradesl = new String[i]; /* store the course letter grades */ int[] grades = new int[i]; /* store the course 12-point grades */ double[] gradesf = new double[i]; /* store the course 4-point grades */ double totalUnits = 0.0; /* total number of units taken by the student */ double sumT = 0.0; double sumF = 0.0; double gpaT; /* Cumulative 12-point GPA */ double gpaF; /* Cumulative 4-point GPA */ System.out .println("Enter the course name and then the grade (on a 12 point scale or letter grade). Enter it as you'd like it to appear in your grade sheet that will be generated after you have entered all the courses and their grades."); for (int j = 0; j < i; j++) { System.out.println("Course Name:"); Scanner scanner1 = new Scanner(System.in); String course = scanner1.nextLine(); System.out.println("Grade:"); Scanner scanner2 = new Scanner(System.in); String grade = scanner2.nextLine(); System.out.println("Units:"); Scanner scanner3 = new Scanner(System.in); int units = scanner3.nextInt(); int finalGrade = parseGrade(grade); double fourPoint = parseGradef(finalGrade); String letterGrade = parseGradel(finalGrade); if (finalGrade != 13) { totalUnits += units; sumT += units * finalGrade; sumF += units * fourPoint; courses[j] = course; grades[j] = finalGrade; gradesf[j] = fourPoint; gradesl[j] = letterGrade; } else { System.out .println("Incorrect grade value entered, enter again.."); j--; } } /* Calculate 4 point and 12 point GPA */ gpaF = sumF / totalUnits; gpaT = sumT / totalUnits; /* Write the grades and GPA to the text file */ File file = new File("/users/forkbattery/Documents/Grade Report.txt"); /* Create a file if it does not exist in the path above */ if (!file.exists()) { file.createNewFile(); } /* Write the results to a file */ FileWriter fileWriter = new FileWriter(file.getAbsoluteFile()); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); for (int k = 0; k < i; k++) { bufferedWriter.write(courses[k] + " " + gradesl[k] + " " + gradesf[k] + " " + grades[k] + "\n"); } bufferedWriter.write("\nGPA on 12 point scale: " + gpaT + "\n"); bufferedWriter.write("\nGPA on 4 point scale: " + gpaF + "\n"); bufferedWriter.close(); System.out.println("Done!"); } public static int parseGrade(String gr) { if (gr.equals("0") || gr.equals("F") || gr.equals("f")) return 0; else if (gr.equals("1") || gr.equals("D-") || gr.equals("d-")) return 1; else if (gr.equals("2") || gr.equals("D") || gr.equals("d")) return 2; else if (gr.equals("3") || gr.equals("D+") || gr.equals("d+")) return 3; else if (gr.equals("4") || gr.equals("C-") || gr.equals("c-")) return 4; else if (gr.equals("5") || gr.equals("C") || gr.equals("c")) return 5; else if (gr.equals("6") || gr.equals("C+") || gr.equals("c+")) return 6; else if (gr.equals("7") || gr.equals("B-") || gr.equals("b-")) return 7; else if (gr.equals("8") || gr.equals("B") || gr.equals("b")) return 8; else if (gr.equals("9") || gr.equals("B+") || gr.equals("b+")) return 9; else if (gr.equals("10") || gr.equals("A-") || gr.equals("a-")) return 10; else if (gr.equals("11") || gr.equals("A") || gr.equals("a")) return 11; else if (gr.equals("12") || gr.equals("A+") || gr.equals("a+")) return 12; else return 13; } public static double parseGradef(int grf) { switch (grf) { case 0: return 0.0; case 1: return 0.7; case 2: return 1.0; case 3: return 1.3; case 4: return 1.7; case 5: return 2.0; case 6: return 2.3; case 7: return 2.7; case 8: return 3.0; case 9: return 3.3; case 10: return 3.7; case 11: return 3.9; case 12: return 4.0; default: return 5.0; } } public static String parseGradel(int grl) { switch (grl) { case 0: return "F"; case 1: return "D-"; case 2: return "D"; case 3: return "D+"; case 4: return "C-"; case 5: return "C"; case 6: return "C+"; case 7: return "B-"; case 8: return "B"; case 9: return "B+"; case 10: return "A-"; case 11: return "A"; case 12: return "A+"; default: return "error"; } } }
to join this conversation on GitHub. Already have an account? Sign in to comment