-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeCalculatorSystem.java
More file actions
74 lines (63 loc) · 2.39 KB
/
Copy pathGradeCalculatorSystem.java
File metadata and controls
74 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Scanner;
// The Grader Class
class Grader {
// Instance variable
private double score;
// Suitable constructor
public Grader(double score) {
this.score = score;
}
// Method to calculate and return the letter grade
public char letterGrade() {
if (score >= 90 && score <= 100) {
return 'O'; // Outstanding
} else if (score >= 80 && score < 90) {
return 'E'; // Excellent
} else if (score >= 70 && score < 80) {
return 'A'; // Very Good
} else if (score >= 60 && score < 70) {
return 'B'; // Good
} else if (score >= 50 && score < 60) {
return 'C'; // Fair
} else {
return 'F'; // Fail (0 to 49)
}
}
}
// Sample Testing Class (PascalCase)
public class GradeCalculatorSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- Student Grading System ---");
double validScore = -1;
boolean isValid = false;
// Loop until a valid score between 0 and 100 is provided
while (!isValid) {
System.out.print("Please enter a score (0 - 100): ");
validScore = getValidDouble(scanner);
// Confirming the value is between 0 and 100
if (validScore >= 0 && validScore <= 100) {
isValid = true;
} else {
System.out.println("Error: Score must be between 0 and 100. Please try again.");
}
}
// Generate the Grader object once confirmed
Grader studentGrader = new Grader(validScore);
// Obtain and print the Grade
char finalGrade = studentGrader.letterGrade();
System.out.println("\n==================================");
System.out.println("Score Entered: " + validScore);
System.out.println("Final Letter Grade: " + finalGrade);
System.out.println("==================================");
scanner.close();
}
// Helper method to ensure program doesn't crash on invalid (non-numeric) input
private static double getValidDouble(Scanner scanner) {
while (!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please enter a valid number: ");
scanner.next(); // Consume invalid input
}
return scanner.nextDouble();
}
}