Post

Created by @nathanedwards
 at November 1st 2023, 3:52:34 pm.

Question

In the context of Java programming, an ArrayList is a dynamic data structure that allows for the storage and manipulation of a collection of objects. It provides flexibility in terms of adding, removing, and accessing elements. Consider the following scenario:

You are tasked with creating a program that manages a list of students in a school. Each student object will store their name, age, and grade level. You decide to use an ArrayList to store these students.

a) Write the import statement required to use the ArrayList class. b) Declare a variable named studentList which will be used to store the list of students. Assign an empty ArrayList object to it. c) Write a method named addStudent that takes three parameters: name (String), age (int), and gradeLevel (int). This method should create a new student object with the given parameters and add it to the studentList. d) Write a method named removeStudent that takes the name (String) of a student as a parameter. This method should remove the student with the matching name from the studentList (if it exists). If the student is not found, the method should print a message indicating that the student was not found. e) Write a method named printStudents that does not take any parameters. This method should print the details (name, age, and grade level) of each student in the studentList. If the list is empty, the method should print a message indicating that there are no students in the list.

Implement the above program by completing the given methods and create additional methods if needed. Test your program by adding and removing students from the studentList.

import java.util.ArrayList;

public class StudentManager {
    private static ArrayList<Student> studentList = new ArrayList<>();

    public static void addStudent(String name, int age, int gradeLevel) {
        Student newStudent = new Student(name, age, gradeLevel);
        studentList.add(newStudent);
    }

    public static void removeStudent(String name) {
        boolean found = false;
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)) {
                studentList.remove(i);
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println("Student not found in the list.");
        }
    }

    public static void printStudents() {
        if (studentList.isEmpty()) {
            System.out.println("No students in the list.");
        } else {
            for (Student student : studentList) {
                System.out.println("Name: " + student.getName() + ", Age: " + student.getAge() +
                        ", Grade Level: " + student.getGradeLevel());
            }
        }
    }

    // Additional methods can be implemented if needed.
}

class Student {
    private String name;
    private int age;
    private int gradeLevel;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getGradeLevel() {
        return gradeLevel;
    }
}

Answer Explanation

a) To use the ArrayList class in Java, we need to import the java.util.ArrayList package. This can be done using the import statement as follows:

import java.util.ArrayList;

b) To declare a variable named studentList and assign an empty ArrayList object to it, we can use the following line of code:

private static ArrayList<Student> studentList = new ArrayList<>();

c) The addStudent method takes three parameters: name (String), age (int), and gradeLevel (int). It creates a new Student object with the given parameters and adds it to the studentList using the add method:

public static void addStudent(String name, int age, int gradeLevel) {
    Student newStudent = new Student(name, age, gradeLevel);
    studentList.add(newStudent);
}

d) The removeStudent method takes the name (String) of a student as a parameter. It iterates over the studentList using a for loop and compares the name of each student with the given name. If a match is found, the student is removed using the remove method. If no match is found, a message is printed indicating that the student was not found:

public static void removeStudent(String name) {
    boolean found = false;
    for (int i = 0; i < studentList.size(); i++) {
        if (studentList.get(i).getName().equals(name)) {
            studentList.remove(i);
            found = true;
            break;
        }
    }
    if (!found) {
        System.out.println("Student not found in the list.");
    }
}

e) The printStudents method prints the details (name, age, and grade level) of each student in the studentList. If the list is empty, a message indicating that there are no students in the list is printed. This is achieved using an enhanced for loop to iterate over the studentList and accessing the appropriate getter methods of the Student class:

public static void printStudents() {
    if (studentList.isEmpty()) {
        System.out.println("No students in the list.");
    } else {
        for (Student student : studentList) {
            System.out.println("Name: " + student.getName() + ", Age: " + student.getAge() +
                    ", Grade Level: " + student.getGradeLevel());
        }
    }
}

The provided code also includes the Student class, which represents a student object and contains the necessary getter methods. Additional methods can be implemented to enhance the functionality of the StudentManager class.