Write a program that computes a student’s grade in a course. The course grade has three components: homework assignments, a midterm exam, and a final exam. The program should prompt the user for all information necessary to compute the grade, such as the number of homework assignments, the points earned and points possible for each assignment, the midterm and final exam scores, and whether each exam was curved (and, if so, by how much). Consider writing a variation of this program that reports what final exam score the student needs to get a certain course grade.
Program:
import java.util.Scanner;
public class Ch04PP05
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int numberOfHomeworkAssignments;
int pointsEarned;
int pointsPossible;
double totalPointsEarned = 0;
double totalPointsPossible = 0;
printStudentGradeScale();
System.out.print("\nEnter the number of homework assignments: ");
numberOfHomeworkAssignments = console.nextInt();
for(int n = 1; n <= numberOfHomeworkAssignments; n++)
{
System.out.print("Enter the points possible in the homework assignment #" + n + ": ");
pointsPossible = console.nextInt();
totalPointsPossible += pointsPossible;
System.out.print("Enter the points earned in the homework assignment #" + n + ": ");
pointsEarned = console.nextInt();
totalPointsEarned += pointsEarned;
}
System.out.print("\nEnter the points possible in the midterm exam: ");
pointsPossible = console.nextInt();
totalPointsPossible += pointsPossible;
System.out.print("Enter the points earned in the midterm exam: ");
pointsEarned = console.nextInt();
totalPointsEarned += pointsEarned;
System.out.print("\nEnter the points possible in the final exam: ");
pointsPossible = console.nextInt();
totalPointsPossible += pointsPossible;
if(totalPointsEarned < totalPointsPossible * 0.90 && totalPointsPossible * 0.90 - totalPointsEarned <= pointsPossible)
{
System.out.println("The student needs to get at least "
+ (totalPointsPossible * 0.90 - totalPointsEarned) + " score in the final exam to get the A grade.");
}
if(totalPointsEarned < totalPointsPossible * 0.80 && totalPointsPossible * 0.80 - totalPointsEarned <= pointsPossible)
{
System.out.println("The student needs to get at least "
+ (totalPointsPossible * 0.80 - totalPointsEarned) + " score in the final exam to get the B grade.");
}
if(totalPointsEarned < totalPointsPossible * 0.70 && totalPointsPossible * 0.70 - totalPointsEarned <= pointsPossible)
{
System.out.println("The student needs to get at least "
+ (totalPointsPossible * 0.70 - totalPointsEarned) + " score in the final exam to get the C grade.");
}
if(totalPointsEarned < totalPointsPossible * 0.60 && totalPointsPossible * 0.60 - totalPointsEarned <= pointsPossible)
{
System.out.println("The student needs to get at least "
+ (totalPointsPossible * 0.60 - totalPointsEarned) + " score in the final exam to get the D grade.");
}
System.out.print("Enter the points earned in the final exam: ");
pointsEarned = console.nextInt();
totalPointsEarned += pointsEarned;
System.out.println("\nTotal points possible: " + totalPointsPossible);
System.out.println("Total points earned: " + totalPointsEarned);
System.out.println("Student's grade in the course: " + getCourseGrade(totalPointsPossible, totalPointsEarned));
}
public static void printStudentGradeScale()
{
System.out.println("Student's grade scale:");
System.out.println("------------------------");
System.out.printf("%-5s%18s\n", "GRADE", "SCALE");
System.out.println("------------------------");
System.out.printf("%3s%20s\n", "A", "for >= 90%");
System.out.printf("%3s%20s\n", "B", "for >= 80%");
System.out.printf("%3s%20s\n", "C", "for >= 70%");
System.out.printf("%3s%20s\n", "D", "for >= 60%");
System.out.printf("%3s%20s\n", "F", "for < 60%");
System.out.println("------------------------");
}
public static String getCourseGrade(double totalPointsPossible, double totalPointsEarned)
{
if(totalPointsEarned / totalPointsPossible * 100 >= 90)
{
return "A";
}
else if(totalPointsEarned / totalPointsPossible * 100 >= 80)
{
return "B";
}
else if(totalPointsEarned / totalPointsPossible * 100 >= 70)
{
return "C";
}
else if(totalPointsEarned / totalPointsPossible * 100 >= 60)
{
return "D";
}
else
{
return "F";
}
}
}
Output:
Student's grade scale:
------------------------
GRADE SCALE
------------------------
A for >= 90%
B for >= 80%
C for >= 70%
D for >= 60%
F for < 60%
------------------------
Enter the number of homework assignments: 3
Enter the points possible in the homework assignment #1: 20
Enter the points earned in the homework assignment #1: 15
Enter the points possible in the homework assignment #2: 20
Enter the points earned in the homework assignment #2: 16
Enter the points possible in the homework assignment #3: 20
Enter the points earned in the homework assignment #3: 17
Enter the points possible in the midterm exam: 40
Enter the points earned in the midterm exam: 30
Enter the points possible in the final exam: 100
The student needs to get at least 82.0 score in the final exam to get the B grade.
The student needs to get at least 62.0 score in the final exam to get the C grade.
The student needs to get at least 42.0 score in the final exam to get the D grade.
Enter the points earned in the final exam: 80
Total points possible: 200.0
Total points earned: 158.0
Student's grade in the course: C