Here is what i have so far for my code. //User inputs the number of classes he/s
ID: 641400 • Letter: H
Question
Here is what i have so far for my code.
//User inputs the number of classes he/she has
Scanner input = new Scanner(System.in);
System.out.println("Please enter the amount of courses you completed last quarter");
int classes;
classes = input.nextInt();
String Grade = "";
int totalCredits = 0;
int totalCreditsEarned = 0;
int credits;
double gpa;
double number = 0;
//String of if and else statements that set the number to the appropriate GPA
if (Grade.equals("A")) {
number = 4.0;
} else if (Grade.equals("B")) {
number = 3.0;
} else if (Grade.equals("C")) {
number = 2.0;
} else if (Grade.equals("D")) {
number = 1.0;
} else if (Grade.equals("F")) {
number = 0;
} else if (Grade.equals("X")) {
number = 0;
//Loop that ends once the student has put information in on all his classes
for (int count = 0; count < classes; count++) {
//Reads the number of credits each class was
Scanner input2 = new Scanner(System.in);
System.out.println("Please enter the number of credits this class was (A number)");
credits = input2.nextInt();
//reads the letter grade using the String Grade prompt
Scanner input3 = new Scanner(System.in);
System.out.println("Please enter your grade for this class (Capital letters such as A, B+, C-)");
Grade = input3.next();
//algorithm for finding the GPA
totalCredits += credits;
totalCreditsEarned += (credits * number);
}
//for loop ends
//GPA is calculated for all the students classes
gpa = totalCreditsEarned / totalCredits;
//GPA is printed to the screen
System.out.println(gpa);
}
}
}
I need help configuring it to these specifications.
Ask the user to enter the number of courses completed. The number must be greater than 0.
Perform the following steps for each course:
Get the course number (which will consist of an alphabetic prefix and a number, with or without a space between prefix and number), the number of credit hours (which must be between .5 and 9.0), and the grade earned (which must be A, B, C, D, F, or X).
Calculate quality points. Quality points are determined by multiplying the number of credit hours for a course by the number of quality points for the letter grade. Quality point values for each grade are as follows:
4 quality points per credit hour
3.0 quality points per credit hour
2.0 quality points per credit hour
1.0 quality points per credit hour
F, X 0 quality points per credit hour
Display the user
Explanation / Answer
import java.util.Scanner;
public class GPA {
public static double getPoints(String Grade){
double number = 0;
// String of if and else statements that set the number to the
// appropriate GPA
if (Grade.equals("A")) {
number = 4.0;
} else if (Grade.equals("B")) {
number = 3.0;
} else if (Grade.equals("C")) {
number = 2.0;
} else if (Grade.equals("D")) {
number = 1.0;
} else if (Grade.equals("F")) {
number = 0;
} else if (Grade.equals("X")) {
number = 0;
}
return number;
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the amount of courses you completed last quarter");
int classes;
classes = input.nextInt();
String Grade = "";
String courseNumber = "";
double totalCredits = 0;
double totalCreditsEarned = 0;
double credits;
double gpa;
double number = 0;
// Loop that ends once the student has put information in on all his
// classes
for (int count = 0; count < classes; count++) {
Scanner input1 = new Scanner(System.in);
System.out.print("Please enter the course number: ");
courseNumber = input1.nextLine();
// Reads the number of credits each class was
Scanner input2 = new Scanner(System.in);
System.out.print("Please enter the number of credits for " + courseNumber + ": ");
credits = input2.nextDouble();
while(credits < 0.5 || credits > 9){
System.out.print("Credit hours must be at least .5 and not greater than 9. Please re-enter: ");
credits = input2.nextDouble();
}
// reads the letter grade using the String Grade prompt
Scanner input3 = new Scanner(System.in);
System.out.println("Please enter your grade for " + courseNumber + " (Capital letters such as A, B+, C-)");
Grade = input3.next();
while(!(Grade.equalsIgnoreCase("A") || Grade.equalsIgnoreCase("B") ||
Grade.equalsIgnoreCase("C") || Grade.equalsIgnoreCase("D") ||
Grade.equalsIgnoreCase("F") || Grade.equalsIgnoreCase("X"))){
System.out.print("Letter grade is not valid. Valid grades are A, B, C, D, F, X. Please re-enter: ");
Grade = input3.next();
}
// algorithm for finding the GPA
totalCredits += credits;
totalCreditsEarned += (credits * getPoints(Grade));
System.out.println("Course number: " + courseNumber);
System.out.println("Credit hours: " + credits);
System.out.println("Grade earned: " + Grade);
System.out.println("Quality points: " + (credits * getPoints(Grade)));
System.out.println();
}
// for loop ends
// GPA is calculated for all the students classes
gpa = totalCreditsEarned / (double)totalCredits;
System.out.println("Total credit hours completed this semester: " + totalCredits);
System.out.println("Total quality points earned this semester: " + totalCreditsEarned);
// GPA is printed to the screen
System.out.printf("Your GPA this semester is %.2f", gpa);
}
}