New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Java› Project: A Console Grade Book

Project: A Console Grade Book

💻 Java

Build a small console application that stores students, records scores and prints a report.

Lesson 9 of 9 Project
0 of 9 completed

This project combines classes, collections, loops, exceptions and formatted output into one practical program.

By the end of this lesson you can:
  • Model students with a class.
  • Store them in a list.
  • Print a sorted report with averages.

Design

Create a Student class with a name and a list of scores. A GradeBook class holds students and provides addStudent and printReport methods.

Build the Classes

Keep input and output in main. The model classes should not print anything themselves, which makes them easier to reuse and test.

import java.util.ArrayList;
import java.util.List;

class Student {
    private final String name;
    private final List<Integer> scores = new ArrayList<>();

    Student(String name) {
        this.name = name;
    }

    void addScore(int score) {
        if (score < 0 || score > 100) {
            throw new IllegalArgumentException("Score must be 0 to 100");
        }
        scores.add(score);
    }

    double average() {
        if (scores.isEmpty()) return 0;
        int total = 0;
        for (int score : scores) total += score;
        return (double) total / scores.size();
    }

    String name() { return name; }
}

Report

Sort students by average before printing. Use String.format to align the columns so the output is readable.

students.sort((a, b) -> Double.compare(b.average(), a.average()));

for (Student student : students) {
    System.out.printf("%-20s %6.1f%n", student.name(), student.average());
}
Tip Test the model classes with a few hard-coded students before adding keyboard input.

Practice

  1. Add a method that returns the highest score for a student.
  2. Add a subject field to Student.
  3. Print a class average at the end of the report.

Hands-on exercise

Build the GradeBook project.
1. Add at least four students with two or three scores each.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Include a class average at the bottom.

Recap

A small application is a set of focused classes plus a thin main method that connects input, logic and output.

Example

import java.util.ArrayList;
import java.util.List;

class Student {
    private final String name;
    private final List<Integer> scores = new ArrayList<>();

    Student(String name) {
        this.name = name;
    }

    void addScore(int score) {
        if (score < 0 || score > 100) {
            throw new IllegalArgumentException("Score must be 0 to 100");
        }
        scores.add(score);
    }

    double average() {
        if (scores.isEmpty()) return 0;
        int total = 0;
        for (int score : scores) total += score;
        return (double) total / scores.size();
    }

    String name() { return name; }
}

Exercises

Exercise 1. Grade book report Hard
Build the GradeBook project.
1. Add at least four students with two or three scores each.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Include a class average at the bottom.
Java exercise-1.java
Expected Output
A sorted report with aligned averages and a class average.

Lesson Nav

Lesson sections