Student Management System (Basic Version)

Rumman Ansari   Software Engineer   2025-09-26 03:47:34   374  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Objective:

To practice Java class & object concepts (attributes, methods, constructors) along with basic operations like input/output, arrays, and simple logic.


Requirements:

  1. Create a class Student with the following:

    • Attributes:

      • studentId (int)

      • name (String)

      • age (int)

      • grade (String)

    • Constructor to initialize these values.

    • Method displayInfo() → prints student details.

    • Method updateGrade(String newGrade) → updates the grade.

  2. Create a class StudentManagement that will:

    • Store multiple students in an array or ArrayList.

    • Have methods:

      • addStudent(Student s) → add new student.

      • showAllStudents() → display all student info.

      • findStudentById(int id) → search a student.

  3. In the main method:

    • Create at least 3 student objects.

    • Add them into the management system.

    • Display all students.

    • Update a student’s grade.

    • Search for a student by ID and display result.


Solutions


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

// Class representing a Student
class Student {
    int studentId;
    String name;
    int age;
    String grade;

    // Constructor
    Student(int studentId, String name, int age, String grade) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    // Method to display student info
    void displayInfo() {
        System.out.println("ID: " + studentId + " | Name: " + name + " | Age: " + age + " | Grade: " + grade);
    }

    // Method to update grade
    void updateGrade(String newGrade) {
        this.grade = newGrade;
        System.out.println(name + "'s grade updated to " + newGrade);
    }
}

// Class for managing multiple students
class StudentManagement {
    ArrayList<Student> students = new ArrayList<>();

    // Add a new student
    void addStudent(Student s) {
        students.add(s);
        System.out.println("Student Added: " + s.studentId + ", " + s.name);
    }

    // Show all students
    void showAllStudents() {
        System.out.println("\n--- Showing All Students ---");
        for (Student s : students) {
            s.displayInfo();
        }
    }

    // Find student by ID
    Student findStudentById(int id) {
        for (Student s : students) {
            if (s.studentId == id) {
                return s;
            }
        }
        return null; // Not found
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StudentManagement sm = new StudentManagement();

        // Adding some students
        sm.addStudent(new Student(101, "Riya", 20, "A"));
        sm.addStudent(new Student(102, "Arjun", 21, "B"));
        sm.addStudent(new Student(103, "Sneha", 19, "C"));

        // Show all students
        sm.showAllStudents();

        // Update a grade
        Student student = sm.findStudentById(102);
        if (student != null) {
            student.updateGrade("A");
        }

        // Search for a student by ID
        System.out.print("\nEnter Student ID to search: ");
        int searchId = sc.nextInt();
        Student found = sm.findStudentById(searchId);

        if (found != null) {
            System.out.println("Found Student:");
            found.displayInfo();
        } else {
            System.out.println("Student with ID " + searchId + " not found!");
        }

        sc.close();
    }
}

Output:


Student Added: 101, Riya
Student Added: 102, Arjun
Student Added: 103, Sneha

--- Showing All Students ---
ID: 101 | Name: Riya | Age: 20 | Grade: A
ID: 102 | Name: Arjun | Age: 21 | Grade: B
ID: 103 | Name: Sneha | Age: 19 | Grade: C
Arjun's grade updated to A

Enter Student ID to search: 102
Found Student:
ID: 102 | Name: Arjun | Age: 21 | Grade: A


import java.util.Scanner;

// Class representing a Student
class Student {
    int studentId;
    String name;
    int age;
    String grade;

    // Constructor
    Student(int studentId, String name, int age, String grade) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    // Method to display student info
    void displayInfo() {
        System.out.println("ID: " + studentId + " | Name: " + name + " | Age: " + age + " | Grade: " + grade);
    }

    // Method to update grade
    void updateGrade(String newGrade) {
        this.grade = newGrade;
        System.out.println(name + "'s grade updated to " + newGrade);
    }
}

// Class for managing multiple students using a fixed-size array
class StudentManagement {
    Student[] students;
    int count; // keeps track of current number of students

    // Constructor with a maximum number of students
    StudentManagement(int maxStudents) {
        students = new Student[maxStudents];
        count = 0;
    }

    // Add a new student
    void addStudent(Student s) {
        if (count < students.length) {
            students[count] = s;
            count++;
            System.out.println("Student Added: " + s.studentId + ", " + s.name);
        } else {
            System.out.println("Cannot add more students. Array is full!");
        }
    }

    // Show all students
    void showAllStudents() {
        System.out.println("\n--- Showing All Students ---");
        for (int i = 0; i < count; i++) {
            students[i].displayInfo();
        }
    }

    // Find student by ID
    Student findStudentById(int id) {
        for (int i = 0; i < count; i++) {
            if (students[i].studentId == id) {
                return students[i];
            }
        }
        return null; // Not found
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StudentManagement sm = new StudentManagement(10); // max 10 students

        // Adding some students
        sm.addStudent(new Student(101, "Riya", 20, "A"));
        sm.addStudent(new Student(102, "Arjun", 21, "B"));
        sm.addStudent(new Student(103, "Sneha", 19, "C"));

        // Show all students
        sm.showAllStudents();

        // Update a grade
        Student student = sm.findStudentById(102);
        if (student != null) {
            student.updateGrade("A");
        }

        // Search for a student by ID
        System.out.print("\nEnter Student ID to search: ");
        int searchId = sc.nextInt();
        Student found = sm.findStudentById(searchId);

        if (found != null) {
            System.out.println("Found Student:");
            found.displayInfo();
        } else {
            System.out.println("Student with ID " + searchId + " not found!");
        }

        sc.close();
    }
}


import java.util.Scanner;

public class Student {
    // Attributes
    int studentId;
    String name;
    int age;
    String grade;

    // Constructor
    Student(int studentId, String name, int age, String grade) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    // Display student info
    void displayInfo() {
        System.out.println("ID: " + studentId + " | Name: " + name + " | Age: " + age + " | Grade: " + grade);
    }

    // Update grade
    void updateGrade(String newGrade) {
        this.grade = newGrade;
        System.out.println(name + "'s grade updated to " + newGrade);
    }

    // --- Static array and management methods inside the same class ---
    static Student[] students = new Student[10];
    static int count = 0;

    // Add a student
    static void addStudent(Student s) {
        if (count < students.length) {
            students[count] = s;
            count++;
            System.out.println("Student Added: " + s.studentId + ", " + s.name);
        } else {
            System.out.println("Cannot add more students. Array is full!");
        }
    }

    // Show all students
    static void showAllStudents() {
        System.out.println("\n--- Showing All Students ---");
        for (int i = 0; i < count; i++) {
            students[i].displayInfo();
        }
    }

    // Find student by ID
    static Student findStudentById(int id) {
        for (int i = 0; i < count; i++) {
            if (students[i].studentId == id) {
                return students[i];
            }
        }
        return null;
    }

    // Main method
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Adding students
        addStudent(new Student(101, "Riya", 20, "A"));
        addStudent(new Student(102, "Arjun", 21, "B"));
        addStudent(new Student(103, "Sneha", 19, "C"));

        // Show all students
        showAllStudents();

        // Update a grade
        Student student = findStudentById(102);
        if (student != null) {
            student.updateGrade("A");
        }

        // Search for a student by ID
        System.out.print("\nEnter Student ID to search: ");
        int searchId = sc.nextInt();
        Student found = findStudentById(searchId);

        if (found != null) {
            System.out.println("Found Student:");
            found.displayInfo();
        } else {
            System.out.println("Student with ID " + searchId + " not found!");
        }

        sc.close();
    }
}




Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.