File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Grade {
2
+
3
+ // Simple numeric -> letter grade
4
+ public String toLetter (double score ) {
5
+ if (score < 0 || score > 100 ) {
6
+ throw new IllegalArgumentException ("Score must be 0..100" );
7
+ }
8
+ if (score >= 90 ) return "A" ;
9
+ if (score >= 80 ) return "B" ;
10
+ if (score >= 70 ) return "C" ;
11
+ if (score >= 60 ) return "D" ;
12
+ return "F" ;
13
+ }
14
+
15
+ // GPA points (typical 4.0 scale)
16
+ public double toGpaPoints (double score ) {
17
+ String letter = toLetter (score );
18
+ switch (letter ) {
19
+ case "A" : return 4.0 ;
20
+ case "B" : return 3.0 ;
21
+ case "C" : return 2.0 ;
22
+ case "D" : return 1.0 ;
23
+ default : return 0.0 ;
24
+ }
25
+ }
26
+
27
+ // Average of array
28
+ public double average (double [] scores ) {
29
+ if (scores == null || scores .length == 0 ) {
30
+ throw new IllegalArgumentException ("Scores required" );
31
+ }
32
+ double sum = 0 ;
33
+ for (double s : scores ) {
34
+ if (s < 0 || s > 100 ) {
35
+ throw new IllegalArgumentException ("Score out of range: " + s );
36
+ }
37
+ sum += s ;
38
+ }
39
+ return sum / scores .length ;
40
+ }
41
+
42
+ // Average letter
43
+ public String averageLetter (double [] scores ) {
44
+ return toLetter (average (scores ));
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments