w3resource

Java: Manage student courses using the Student class

Java OOP: Exercise-10 with Solution

Write a Java program to create a class called "Student" with a name, grade, and courses attributes, and methods to add and remove courses.

Sample Solution:

Java Code:

//Student.java
import java.util.ArrayList;

public class Student {
  private String name;
  private int grade;
  private ArrayList < String > courses;

  public Student(String name, int grade) {
    this.name = name;
    this.grade = grade;
    this.courses = new ArrayList < String > ();
  }

  public String getName() {
    return name;
  }

  public int getGrade() {
    return grade;
  }

  public ArrayList < String > getCourses() {
    return courses;
  }

  public void addCourse(String course) {
    courses.add(course);
  }

  public void removeCourse(String course) {
    courses.remove(course);
  }
  public void printStudentDetails() {
    System.out.println("Name: " + name);
    System.out.println("Grade: " + grade);
  }

}

The above "Student" class has three private attributes: 'name', 'grade', and 'courses'. The 'name' and 'grade' attributes are initialized in the constructor with the values passed as arguments. The 'courses' attribute is initialized as an empty array list. There are getter methods for the 'name', 'grade', and 'courses' attributes. There are also methods to add and remove courses from the 'courses' attribute.

//Main.java
public class Main {
  public static void main(String[] args) {
    Student student1 = new Student("Carolus Vitali", 11);
    Student student2 = new Student("Nakeisha Uhuru", 10);
    Student student3 = new Student("Gabriella Cherice", 10);
    System.out.println("Student Details:");
    student1.printStudentDetails();
    student2.printStudentDetails();
    student3.printStudentDetails();
    System.out.println("Adding courses for " + student1.getName());
    student1.addCourse("Math");
    student1.addCourse("Science");
    student1.addCourse("English");

    System.out.println(student1.getName() + "'s courses: " + student1.getCourses());

    System.out.println("\nAdding courses for " + student2.getName());
    student2.addCourse("History");
    student2.addCourse("Geography");
    student2.addCourse("English");
    System.out.println(student2.getName() + "'s courses: " + student2.getCourses());
    System.out.println("\nRemoving 'Science' course for " + student1.getName());
    student1.removeCourse("Science");
    System.out.println(student1.getName() + "'s courses: " + student1.getCourses());
  }
}

The above Main class creates three instances of the Student class, adds courses to their courses list using the “addCourse()” method, and prints out the list of courses for each student using the “getCourses()” method. It then removes a course for student1 using the ‘removeCourse()’ method, and prints out the updated list of courses for student1.

Sample Output:

Student Details:
Name: Carolus Vitali
Grade: 11
Name: Nakeisha Uhuru
Grade: 10
Name: Gabriella Cherice
Grade: 10
Adding courses for Carolus Vitali
Carolus Vitali's courses: [Math, Science, English]

Adding courses for Nakeisha Uhuru
Nakeisha Uhuru's courses: [History, Geography, English]

Removing 'Science' course for Carolus Vitali
Carolus Vitali's courses: [Math, English]

Flowchart:

Flowchart: Java  OOP Exercises: Manage student courses using the Student class.
Flowchart: Java  OOP Exercises: Manage student courses using the Student class.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Employee class with years of service calculation.
Java OOP Next: Library class with add and remove books methods.

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.