-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeConverter.java
More file actions
32 lines (29 loc) · 1.09 KB
/
Copy pathGradeConverter.java
File metadata and controls
32 lines (29 loc) · 1.09 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
import java.util.*;
// Provides functionality to convert letter grades to GPA values
public class GradeConverter {
// Mapping of letter grades to their numeric GPA equivalents
private static final Map<String, Double> gradeMap = new HashMap<>();
// Static initializer block to populate grade conversion table
static {
gradeMap.put("A1", 4.0);
gradeMap.put("A2", 3.5);
gradeMap.put("B1", 3.0);
gradeMap.put("B2", 2.5);
gradeMap.put("C1", 2.0);
gradeMap.put("C2", 1.5);
gradeMap.put("D1", 1.0);
gradeMap.put("D2", 0.5);
gradeMap.put("F3", 0.0);
}
// Converts a letter grade to its numeric GPA value
public static double convert(String grade) {
if (!gradeMap.containsKey(grade)) {
throw new IllegalArgumentException("Invalid letter grade: " + grade);
}
return gradeMap.get(grade);
}
// Checks whether a given grade is a valid letter grade
public static boolean isValidGrade(String grade) {
return gradeMap.containsKey(grade);
}
}