Reading history

Java Project: Student Result System

9 min read 0 views

Build an SSC-style student result system in Java: grade points, GPA with the optional subject bonus, merit list, pass rate, subject stats and report cards.

In this project you will build the result system a school uses after SSC or HSC style exams. It turns marks into letter grades and grade points, works out each student’s GPA with the fourth-subject bonus, and prints a merit list, pass rate, subject statistics and a report card. You will use an enum for the grading scale, a record for students, streams for the reports and exceptions for bad data, all in one realistic program.

In this project you will

  • Model the Bangladesh grading scale with an enum
  • Calculate GPA, including the optional-subject rule and the 5.00 cap
  • Rank students with a Comparator that breaks ties
  • Summarise results with groupingBy and IntSummaryStatistics
  • Print a report card that names failed subjects

This project uses enums and records, Comparator, Collectors and groupingBy and printf formatting.

The Grading Rules

Marks out of 100, letter grade and grade pointF0.00D1.033C2.040B3.050A-3.560A4.070A+5.080100pass mark

The program follows the grading scale used by the education boards in Bangladesh:

  • Each subject’s mark out of 100 gives a letter grade and a grade point, from A+ (5.0) down to F (0.0). The pass mark is 33.
  • GPA is the average grade point of the compulsory subjects.
  • If a student fails any compulsory subject, the GPA is 0.00 and the result is F.
  • For the optional (fourth) subject, only the points above 2 are added to the total before dividing.
  • GPA can never be more than 5.00.

We use six compulsory subjects to keep the output short. A real board result has more subjects, but the method is exactly the same.

Step 1: The Grading Engine

import java.util.*;

public class Main {
    enum Grade {
        A_PLUS("A+", 80, 5.0), A("A", 70, 4.0), A_MINUS("A-", 60, 3.5),
        B("B", 50, 3.0), C("C", 40, 2.0), D("D", 33, 1.0), F("F", 0, 0.0);

        final String label; final int minMark; final double point;
        Grade(String label, int minMark, double point) {
            this.label = label; this.minMark = minMark; this.point = point;
        }
        static Grade of(int mark) {
            if (mark < 0 || mark > 100) throw new IllegalArgumentException("mark out of range: " + mark);
            for (Grade g : values()) if (mark >= g.minMark) return g;   // values() are in order, A+ first
            throw new AssertionError();
        }
    }

    // GPA = average point of compulsory subjects, plus (optional point - 2) if above 2.
    // Any F in a compulsory subject means GPA 0.00. GPA is capped at 5.00.
    static double gpa(int[] compulsory, Integer optional) {
        double sum = 0;
        for (int m : compulsory) {
            Grade g = Grade.of(m);
            if (g == Grade.F) return 0.0;
            sum += g.point;
        }
        if (optional != null) {
            double extra = Grade.of(optional).point - 2;
            if (extra > 0) sum += extra;
        }
        return Math.min(5.0, sum / compulsory.length);
    }

    public static void main(String[] args) {
        for (int m : new int[]{100, 80, 79, 65, 50, 33, 32, 0})
            System.out.printf("mark %3d -> %-2s %.1f%n", m, Grade.of(m).label, Grade.of(m).point);

        int[] all80 = {85, 90, 81, 95, 88, 80};
        int[] mixed = {85, 72, 64, 55, 91, 47};
        int[] oneF  = {95, 92, 88, 30, 90, 85};
        System.out.printf("%nall A+               : %.2f%n", gpa(all80, null));
        System.out.printf("mixed, no optional   : %.2f%n", gpa(mixed, null));
        System.out.printf("mixed, optional 82   : %.2f%n", gpa(mixed, 82));
        System.out.printf("mixed, optional 45   : %.2f%n", gpa(mixed, 45));
        System.out.printf("one F                : %.2f%n", gpa(oneF, 90));
        System.out.printf("all A+, optional A+  : %.2f  (capped)%n", gpa(all80, 85));
        try {
            Grade.of(105);
        } catch (IllegalArgumentException e) {
            System.out.println("Rejected: " + e.getMessage());
        }
    }
}

Output

mark 100 -> A+ 5.0
mark  80 -> A+ 5.0
mark  79 -> A  4.0
mark  65 -> A- 3.5
mark  50 -> B  3.0
mark  33 -> D  1.0
mark  32 -> F  0.0
mark   0 -> F  0.0

all A+               : 5.00
mixed, no optional   : 3.75
mixed, optional 82   : 4.25
mixed, optional 45   : 3.75
one F                : 0.00
all A+, optional A+  : 5.00  (capped)
Rejected: mark out of range: 105
  • Each Grade constant stores its label, lowest mark and point. Grade.of loops through values(), which are in the order they are written, A+ first. The first grade whose minimum the mark reaches is the answer.
  • The test marks check the edges: 80 is A+, 79 is A, 33 is D and 32 is F. Testing the boundaries is where grading bugs are usually found.
  • The mixed student has points 5 + 4 + 3.5 + 3 + 5 + 2 = 22.5, so the GPA is 22.5 / 6 = 3.75.
  • With an optional mark of 82 (A+, 5 points), the bonus is 5 − 2 = 3. The total becomes 25.5 and the GPA 4.25. An optional mark of 45 (C, 2 points) adds nothing.
  • A single F gives 0.00, even with a 90 in the optional subject.
  • All A+ plus an A+ optional would be 33 / 6 = 5.5, so Math.min(5.0, ...) caps it at 5.00.
  • A mark of 105 is rejected with an exception, so bad data cannot slip into a result.

Step 2: The Class Report

import java.util.*;
import java.util.stream.*;

public class Main {
    static final String[] SUBJECTS = {"Bangla", "English", "Math", "Physics", "Chemistry", "ICT"};

    static String grade(int m) {
        return m >= 80 ? "A+" : m >= 70 ? "A" : m >= 60 ? "A-" : m >= 50 ? "B"
             : m >= 40 ? "C" : m >= 33 ? "D" : "F";
    }
    static double point(int m) {
        return m >= 80 ? 5.0 : m >= 70 ? 4.0 : m >= 60 ? 3.5 : m >= 50 ? 3.0
             : m >= 40 ? 2.0 : m >= 33 ? 1.0 : 0.0;
    }

    record Student(int roll, String name, int[] marks, int optional) {
        boolean passed() { return Arrays.stream(marks).allMatch(m -> m >= 33); }
        int total() { return Arrays.stream(marks).sum() + optional; }
        double gpa() {
            if (!passed()) return 0.0;
            double sum = Arrays.stream(marks).mapToDouble(m -> point(m)).sum();
            sum += Math.max(0, point(optional) - 2);
            return Math.min(5.0, sum / marks.length);
        }
        String letter() {
            double g = gpa();
            return g == 5.0 ? "A+" : g >= 4 ? "A" : g >= 3.5 ? "A-" : g >= 3 ? "B"
                 : g >= 2 ? "C" : g >= 1 ? "D" : "F";
        }
    }

    public static void main(String[] args) {
        List<Student> cls = List.of(
            new Student(101, "Nafisa Islam",   new int[]{88, 91, 97, 85, 82, 95}, 84),
            new Student(102, "Arif Hossain",   new int[]{72, 65, 81, 58, 61, 77}, 70),
            new Student(103, "Sumaiya Khatun", new int[]{81, 79, 88, 80, 76, 90}, 92),
            new Student(104, "Rakib Uddin",    new int[]{55, 48, 29, 41, 39, 60}, 66),
            new Student(105, "Tasnim Ferdous", new int[]{90, 86, 84, 88, 81, 93}, 61),
            new Student(106, "Jahid Hasan",    new int[]{64, 52, 47, 50, 44, 68}, 35));

        // 1. Merit list: GPA high to low, then total marks to break ties
        List<Student> merit = cls.stream()
            .sorted(Comparator.comparingDouble(Student::gpa).reversed()
                    .thenComparing(Comparator.comparingInt(Student::total).reversed()))
            .toList();
        System.out.println("Pos Roll Name            Total  GPA   Grade");
        int pos = 1;
        for (Student s : merit)
            System.out.printf("%2d  %d  %-15s %4d  %.2f  %s%n",
                pos++, s.roll(), s.name(), s.total(), s.gpa(), s.letter());

        // 2. Pass rate and grade counts
        long passed = cls.stream().filter(Student::passed).count();
        System.out.printf("%nPassed: %d of %d (%.1f%%)%n", passed, cls.size(), 100.0 * passed / cls.size());
        Map<String, Long> byGrade = cls.stream()
            .collect(Collectors.groupingBy(Student::letter, TreeMap::new, Collectors.counting()));
        System.out.println("Students per final grade: " + byGrade);

        // 3. Subject statistics: average, highest and number of A+
        System.out.println("\nSubject    Avg   High  A+");
        for (int i = 0; i < SUBJECTS.length; i++) {
            final int k = i;
            IntSummaryStatistics st = cls.stream().mapToInt(s -> s.marks()[k]).summaryStatistics();
            long aPlus = cls.stream().filter(s -> s.marks()[k] >= 80).count();
            System.out.printf("%-9s %5.1f  %4d  %2d%n", SUBJECTS[i], st.getAverage(), st.getMax(), aPlus);
        }

        // 4. Report card for one student, showing the failed subject
        Student r = cls.stream().filter(s -> s.roll() == 104).findFirst().orElseThrow();
        System.out.println("\nReport card: " + r.name() + " (roll " + r.roll() + ")");
        for (int i = 0; i < SUBJECTS.length; i++)
            System.out.printf("  %-9s %3d  %-2s%n", SUBJECTS[i], r.marks()[i], grade(r.marks()[i]));
        String failed = IntStream.range(0, SUBJECTS.length)
            .filter(i -> r.marks()[i] < 33).mapToObj(i -> SUBJECTS[i]).collect(Collectors.joining(", "));
        System.out.printf("  GPA %.2f, failed in: %s%n", r.gpa(), failed);
    }
}

Output

Pos Roll Name            Total  GPA   Grade
 1  101  Nafisa Islam     622  5.00  A+
 2  103  Sumaiya Khatun   586  5.00  A+
 3  105  Tasnim Ferdous   583  5.00  A+
 4  102  Arif Hossain     484  4.17  A
 5  106  Jahid Hasan      360  2.83  C
 6  104  Rakib Uddin      338  0.00  F

Passed: 5 of 6 (83.3%)
Students per final grade: {A=1, A+=3, C=1, F=1}

Subject    Avg   High  A+
Bangla     75.0    90   3
English    70.2    91   2
Math       71.0    97   4
Physics    67.0    88   3
Chemistry  63.8    82   2
ICT        80.5    95   3

Report card: Rakib Uddin (roll 104)
  Bangla     55  B 
  English    48  C 
  Math       29  F 
  Physics    41  C 
  Chemistry  39  D 
  ICT        60  A-
  GPA 0.00, failed in: Math
  • Merit list. Three students have GPA 5.00, so GPA alone cannot rank them. thenComparing breaks the tie by total marks, highest first. .reversed() turns each ascending order into descending.
  • total() includes the optional subject. Nafisa’s 622 puts her first among the three GPA 5.00 students.
  • Rakib passed five subjects, but his 29 in Math is below 33. His GPA is 0.00 and he is placed last.
  • Pass rate. filter(Student::passed).count() counts the passes: 5 of 6, or 83.3%. Note the %% in printf, which prints one percent sign.
  • Grade counts. groupingBy with a TreeMap gives the counts in a fixed, sorted order.
  • Subject statistics. For each subject index, mapToInt picks that mark from every student, and summaryStatistics() gives the average and highest in one pass. ICT has the best average, 80.5.
  • Report card. IntStream.range walks the subject indexes, keeps those below 33 and joins their names, so the card states exactly why the student failed.

Ideas to Extend the Project

  • Read students from a CSV file with file handling, and skip bad lines with a clear error.
  • Throw a custom InvalidMarkException instead of IllegalArgumentException.
  • Store marks in a Map<String, Integer> of subject to mark, so each student can take different subjects.
  • Add a --roll 104 command-line option that prints just one report card.
  • Write the merit list to a text file that a teacher can print.

Common Mistakes

  1. Checking grades from the bottom up with >=. Every mark of 33 or more would match D first. Check from A+ downwards, or use exact ranges.
  2. Adding the whole optional point. Only the part above 2 counts.
  3. Forgetting the 5.00 cap. A strong optional subject can push the raw value above 5.
  4. Averaging marks instead of grade points. GPA is based on points, not raw marks.
  5. Comparing doubles with == for grade bands. It works for 5.0 here because the value is capped exactly, but use >= ranges for everything else.

Practice Exercises

Exercise 1. A student’s points are 4, 4, 3.5, 3, 5, 3.5 and the optional mark is 74. What is the GPA?

Show answer

The total is 23. The optional mark 74 is an A, 4 points, so the bonus is 4 − 2 = 2. (23 + 2) / 6 = 4.17, a grade of A.

Exercise 2. Print the names of all students who got A+ in Math (subject index 2).

Show answer
cls.stream()
   .filter(s -> s.marks()[2] >= 80)
   .map(Student::name)
   .forEach(System.out::println);
// Nafisa Islam, Arif Hossain, Sumaiya Khatun, Tasnim Ferdous

Frequently Asked Questions

How is GPA calculated for SSC in Bangladesh?

Each subject mark is turned into a grade point, from 5.0 for 80 or above down to 0 for below 33. The grade points of the compulsory subjects are added, the points of the optional subject above 2 are added as a bonus, and the total is divided by the number of compulsory subjects. The result is capped at 5.00, and a fail in any compulsory subject gives GPA 0.

Why use an enum for grades in Java?

An enum keeps each grade’s label, minimum mark and point together in one place, and the compiler stops you from using a grade that does not exist. If the scale changes, you edit one line instead of hunting through if statements.

Summary

  • An enum holds the grading scale; a record holds each student.
  • GPA = (compulsory points + optional points above 2) ÷ compulsory subjects, capped at 5.00, and 0 for any fail.
  • Comparator.comparingDouble(...).reversed().thenComparing(...) ranks with tie-breaks.
  • groupingBy, count and summaryStatistics build class reports quickly.
  • Test boundary marks such as 79, 80, 32 and 33.

Back to the start of the series: Java Introduction.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *