w3resource

Java: School Management System

Java OOP: Exercise-14 with Solution

Write a Java program to create a class called "School" with attributes for students, teachers, and classes, and methods to add and remove students and teachers, and to create classes.

Sample Solution:

Java Code:

// School.java
import java.util.ArrayList;

public class School {
  private ArrayList < Student > students;
  private ArrayList < Teacher > teachers;
  private ArrayList < SchoolClass > classes;

  public School() {
    this.students = new ArrayList < Student > ();
    this.teachers = new ArrayList < Teacher > ();
    this.classes = new ArrayList < SchoolClass > ();
  }

  public void addStudent(Student student) {
    students.add(student);
  }

  public void removeStudent(Student student) {
    students.remove(student);
  }

  public void addTeacher(Teacher teacher) {
    teachers.add(teacher);
  }

  public void removeTeacher(Teacher teacher) {
    teachers.remove(teacher);
  }

  public void addClass(SchoolClass schoolClass) {
    classes.add(schoolClass);
  }

  public void removeClass(SchoolClass schoolClass) {
    classes.remove(schoolClass);
  }

  public ArrayList < Student > getStudents() {
    return students;
  }

  public ArrayList < Teacher > getTeachers() {
    return teachers;
  }

  public ArrayList < SchoolClass > getClasses() {
    return classes;
  }
}

The above "School" class represents a school with students, teachers, and classes. It has three ArrayList attributes to store Student, Teacher, and SchoolClass objects. The constructor initializes these ArrayLists as empty lists. It has methods to add and remove students, teachers, and classes from their respective ArrayLists. It also has getter methods to access ArrayLists of students, teachers, and classes.

//Student.java
public class Student {
  private String name;
  private int age;

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
}

The "Student" class represents a student with a name and an age. It has a constructor that takes two arguments, name and age, and initializes the corresponding attributes. It also has getter and setter methods to access and modify name and age attributes.

//Teacher.java
public class Teacher {
private String name;
private String subject;
public Teacher(String name, String subject) {
    this.name = name;
    this.subject = subject;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
}

The Teacher class represents a teacher in a school. It has two private attributes: name (teacher's name) and subject (Subject the teacher teaches). The class has a constructor that takes two arguments, the teacher's name and subject, and initializes the corresponding attributes. It also has getter and setter methods to access and modify the name and subject attributes.

// SchoolClass.java
import java.util.ArrayList;

public class SchoolClass {
private String className;
private Teacher teacher;
private ArrayList<Student> students;

public SchoolClass(String className, Teacher teacher) {
    this.className = className;
    this.teacher = teacher;
    this.students = new ArrayList<Student>();
}

public String getClassName() {
    return className;
}

public void setClassName(String className) {
    this.className = className;
}

public Teacher getTeacher() {
    return teacher;
}

public void setTeacher(Teacher teacher) {
    this.teacher = teacher;
}

public ArrayList<Student> getStudents() {
    return students;
}

public void addStudent(Student student) {
    students.add(student);
}

public void removeStudent(Student student) {
    students.remove(student);
}
}

The SchoolClass class represents a class in a school. It has a private attribute className to store the name of the class and another private attribute teacher of type Teacher to store the teacher of the class. It also has a private attribute students of type ArrayList to store the students enrolled in the class. The constructor takes two arguments: className and teacher, and initializes these attributes.

The class has getter and setter methods for className and teacher. It also has methods addStudent and removeStudent to add and remove a student from the students list. The getStudents method returns the list of students in the class.

//Main.java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
School school = new School();
    Student student1 = new Student("Mats Yatzil", 15);
    Student student2 = new Student("Karolina Ralf", 16);
	Student student3 = new Student("Felicie Anuschka", 16);
	Student student4 = new Student("Norbert Micha", 15);
	
    school.addStudent(student1);
    school.addStudent(student2);
	school.addStudent(student3);
	school.addStudent(student4);
	
    Teacher teacher1 = new Teacher("Jens Amalia", "Math");
    Teacher teacher2 = new Teacher("Elise Giiwedin", "English");
	Teacher teacher3 = new Teacher("Angelika Lotta", "Science");
	
    school.addTeacher(teacher1);
    school.addTeacher(teacher2);
	school.addTeacher(teacher3);

    SchoolClass mathClass = new SchoolClass("Mathematics", teacher1);
    mathClass.addStudent(student1);
    mathClass.addStudent(student2);
	mathClass.addStudent(student3);
	mathClass.addStudent(student4);

    SchoolClass englishClass = new SchoolClass("English", teacher2);
    englishClass.addStudent(student1);
	englishClass.addStudent(student2);
	englishClass.addStudent(student3);

    SchoolClass scienceClass = new SchoolClass("Science", teacher3);
    scienceClass.addStudent(student1);
	scienceClass.addStudent(student2);
	scienceClass.addStudent(student3);
	scienceClass.addStudent(student4);

    school.addClass(mathClass);
    school.addClass(englishClass);
	school.addClass(scienceClass);

    System.out.println("School information:");
    System.out.println("Number of students: " + school.getStudents().size());
    System.out.println("Number of teachers: " + school.getTeachers().size());
    System.out.println("Number of classes: " + school.getClasses().size());
    System.out.println();

    System.out.println("Math class information:");
    System.out.println("Class name: " + mathClass.getClassName());
    System.out.println("Teacher: " + mathClass.getTeacher().getName());
    System.out.println("Number of students: " + mathClass.getStudents().size());
    System.out.println();

    System.out.println("English class information:");
    System.out.println("Class name: " + englishClass.getClassName());
    System.out.println("Teacher: " + englishClass.getTeacher().getName());
    System.out.println("Number of students: " + englishClass.getStudents().size());
    System.out.println();
	
	System.out.println("Science class information:");
    System.out.println("Class name: " + scienceClass.getClassName());
    System.out.println("Teacher: " + scienceClass.getTeacher().getName());
    System.out.println("Number of students: " + scienceClass.getStudents().size());
    System.out.println();

    school.removeStudent(student1);
    school.removeTeacher(teacher2);
    school.removeClass(mathClass);

    System.out.println("School information after removing one student, teacher and Class:");
    System.out.println("Number of students: " + school.getStudents().size());
    System.out.println("Number of teachers: " + school.getTeachers().size());
    System.out.println("Number of classes: " + school.getClasses().size());
 }
}

The "Main" class is the program entry point. It creates objects of the "School", "Student", "Teacher", and "SchoolClass" classes to demonstrate the school system's functionality. It adds students, teachers, and classes to the school object using its respective methods. It then retrieves and prints information about the number of students, teachers, and classes in the school. It also prints the details of each class, including the name of the class, the teacher's name, and the number of students in each class. Finally, it removes one student, one teacher, and one class from the school and prints updated information about the school.

Sample Output:

School information:
Number of students: 4
Number of teachers: 3
Number of classes: 3

Math class information:
Class name: Mathematics
Teacher: Jens Amalia
Number of students: 4

English class information:
Class name: English
Teacher: Elise Giiwedin
Number of students: 3

Science class information:
Class name: Science
Teacher: Angelika Lotta
Number of students: 4

School information after removing one student, teacher and Class:
Number of students: 3
Number of teachers: 2
Number of classes: 2

Flowchart:

Flowchart: Java  OOP Exercises: School Management System.
Flowchart: Java  OOP Exercises: School Management System.
Flowchart: Java  OOP Exercises: School Management System.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Inventory Management.
Java OOP Next: Manage a music library of songs.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.