Primitives Hacks GPA Calculator
Week 1 GPA Calculator
import java.util.Scanner;
public class ScanPrimitives {
public static void main(String[] args) {
System.out.println("Welcome to GPA Calculator");
System.out.println("Code by Meena Annamalai");
int count = -1;
Scanner input;
boolean exitProgram = false;
input = new Scanner(System.in);
System.out.print("How many course would you like to enter? ");
int numCourses = input.nextInt();
System.out.println(numCourses);
input.close();
int[] gradePoints = new int[numCourses];
int[] creditHours = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
// get course
input = new Scanner(System.in);
System.out.print("\nEnter a course, type 0 to exit: ");
/** Wrapper class object string is used here since the user enters the course name **/
String course = input.nextLine();
System.out.println(course);
input.close();
if (course.equals("0")) {
exitProgram = true;
System.out.println("Bye!");
break;
}
//compound assignment operator used here - the result is that the count is increased by 1 each time the loop runs
count += 1;
// get credits
input = new Scanner(System.in);
System.out.print("Enter the number of credits for that course: ");
/** primitive data type: an integer is used here since the number of credits will later be used for calculations so a string shouldn't be used and since the credit number will always
be a whole number, an integer data type can be used **/
int credits = input.nextInt();
System.out.println(credits);
input.close();
// get letter grade
input = new Scanner(System.in);
System.out.print("Enter the letter grade you got: ");
/** Wrapper class object string is used here since the user enters the letter grade they got which will then
be matched with the corresponding point value with the below switch statement **/
String letterGrade = input.nextLine();
System.out.println(letterGrade);
input.close();
int pointValue = 0;
//convert letter grade to gpa
switch (letterGrade) {
case "A":
pointValue = 4;
break;
case "B":
pointValue = 3;
break;
case "C":
pointValue = 2;
break;
case "D":
pointValue = 1;
break;
case "F":
pointValue = 0;
break;
}
creditHours[count] = credits;
gradePoints[count] = pointValue * credits;
}
int totalGradePoints = 0;
for (int i = 0; i < gradePoints.length; i++) {
totalGradePoints += gradePoints[i];
}
int totalAttemptedCredits = 0;
for (int i = 0; i < creditHours.length; i++) {
totalAttemptedCredits += creditHours[i];
}
//here the Primitive data type boolean is used since I only want the final gpa to be shown if the user doesn't exit the program
if (exitProgram == false) {
//here the primitive data type double is used since the the total grade pts divided by the total attempted credits will not always be a whole number
//casting is also used for this reason as both variables were originally integers
double gpa = (double) totalGradePoints / (double) totalAttemptedCredits;
System.out.println("Your GPA is: " + String.valueOf(gpa));
System.out.println("Bye! Thank you for using GPA Calculator");
}
}
}
ScanPrimitives.main(null);