Ankaj Gupta
September 29, 2020

Java program for student mark list using inheritance - java code examples with output

Java OOP Tutorial

Build a Student Mark List in Java Using Inheritance

This guide walks through designing a console-based Java application that captures marks for multiple students, calculates their totals, and prints a formatted report. You will apply single inheritance, encapsulation, and clean code practices to keep the workflow easy to extend.

Objective: Build a reusable base class for shared behaviours (input/output) and extend it with a student-specific subclass that stores marks and totals.

Key Concepts

  • Single inheritance
  • Array storage
  • Formatted console output

Prerequisites

  • Java 8 or newer
  • Basic OOP knowledge
  • Terminal/command prompt access

Deliverables

A `StudentReport` program that collects marks for n students, calculates totals, and prints a tabular mark list.

Designing the Class Hierarchy

Base Class: `MarkSheet`

Handles common tasks such as reading input, storing student data, and printing formatted headers. Methods include `captureStudent()` and `printTable()`.

Derived Class: `StudentReport`

Extends `MarkSheet` to add application flow—collecting the number of students, looping through entries, calculating totals, and triggering the final print.

Complete Java Implementation

The following code uses `Scanner` for input and keeps responsibilities separated across classes. Use it as a starting point for experimenting with grading rules or exporting data.

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

class MarkSheet {
    static class Student {
        String name;
        int[] marks;
        int total;

        Student(String name, int subjectCount) {
            this.name = name;
            this.marks = new int[subjectCount];
        }
    }

    protected final Scanner scanner = new Scanner(System.in);
    protected final List<Student> students = new ArrayList<>();
    protected final int subjectCount;

    MarkSheet(int subjectCount) {
        this.subjectCount = subjectCount;
    }

    protected void captureStudent() {
        System.out.print("Student name: ");
        String name = scanner.nextLine().trim();
        Student student = new Student(name, subjectCount);

        for (int i = 0; i < subjectCount; i++) {
            System.out.printf("Subject %d mark: ", i + 1);
            student.marks[i] = Integer.parseInt(scanner.nextLine().trim());
            student.total += student.marks[i];
        }

        students.add(student);
    }

    protected void printTable() {
        System.out.println("============================================================");
        System.out.printf("%-4s %-18s", "No", "Student Name");
        for (int i = 0; i < subjectCount; i++) {
            System.out.printf(" Sub%-2d", i + 1);
        }
        System.out.printf("  Total%n");
        System.out.println("============================================================");

        int index = 1;
        for (Student student : students) {
            System.out.printf("%-4d %-18s", index++, student.name);
            for (int mark : student.marks) {
                System.out.printf(" %-6d", mark);
            }
            System.out.printf(" %-6d%n", student.total);
        }

        System.out.println("============================================================");
    }
}

public class StudentReport extends MarkSheet {

    public StudentReport(int subjectCount) {
        super(subjectCount);
    }

    public void run() {
        System.out.print("How many students? ");
        int numberOfStudents = Integer.parseInt(scanner.nextLine().trim());

        for (int i = 0; i < numberOfStudents; i++) {
            System.out.printf("%nEnter details for student %d%n", i + 1);
            captureStudent();
        }

        System.out.println();
        printTable();
    }

    public static void main(String[] args) {
        int subjectCount = 5; // Adjust as needed
        StudentReport report = new StudentReport(subjectCount);
        report.run();
    }
}
Tip: Replace `Scanner` with `java.nio` file reading APIs if you prefer to read student data from CSVs or external feeds.

Sample Console Output

How many students? 2

Enter details for student 1
Student name: Ankaj
Subject 1 mark: 8
Subject 2 mark: 9
Subject 3 mark: 6
Subject 4 mark: 7
Subject 5 mark: 9

Enter details for student 2
Student name: Anmol
Subject 1 mark: 9
Subject 2 mark: 6
Subject 3 mark: 7
Subject 4 mark: 8
Subject 5 mark: 9

============================================================
No   Student Name       Sub1   Sub2   Sub3   Sub4   Sub5   Total
============================================================
1    Ankaj              8      9      6      7      9      39
2    Anmol              9      6      7      8      9      39
============================================================

Suggested Enhancements

Feature Ideas

  • Calculate averages and grades
  • Export reports to CSV or PDF
  • Add validation for mark ranges

Common Pitfalls

  • Mixing `Scanner.nextInt()` and `nextLine()` without consuming newline characters
  • Hard-coding subject counts across multiple methods
  • Ignoring encapsulation—keep student details private

Key Takeaways

  • Inheritance lets you share input/output logic while keeping student-specific behaviour separate.
  • Use collections (`List`) to store dynamic numbers of students instead of fixed-size arrays.
  • Plan for extensibility—separate formatting, data capture, and calculation so new features are easy to add.
Java program

Related Posts