Project: A Console Grade Book
💻 JavaBuild a small console application that stores students, records scores and prints a report.
This project combines classes, collections, loops, exceptions and formatted output into one practical program.
- 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());
}Practice
- Add a method that returns the highest score for a student.
- Add a subject field to Student.
- 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
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.
Expected Output
A sorted report with aligned averages and a class average.
Lesson Nav
Java Course
0/9 lessons completed
- 1 Getting Started with Java Tutorial
- 2 Variables and Data Types Tutorial
- 3 Conditions and Loops Tutorial
- 4 Methods and Parameters Tutorial
- 5 Arrays and ArrayLists Tutorial
- 6 Classes and Objects Tutorial
- 7 Inheritance, Interfaces and Polymorphism Tutorial
- 8 Exceptions and Files Tutorial
- 9 Project: A Console Grade Book Project