Skip to content

Commit 6d5c057

Browse files
author
Adi-Sin101
committed
Add Grade class with letter/GPA conversion and averaging
1 parent f35e827 commit 6d5c057

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Features/Grade.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)