Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Now that the classList Array has been implemented, we need to create methods to access the list items. Create the following static methods for the
Now that the classList Array has been implemented, we need to create methods to access the list items.
Create the following static methods for the Student class:
getLastStudent() - returns the name of the last student in the classList array.
getClassSize() - returns the size of the classList
addStudent(int index, Student student) - adds a new student to the classList at index index. This method is a little tricky - when the new Student is added to the class, it will create a duplicate value at the end of the classList because of our Student constructor. This method should also include a command to remove the extra Student in the classList added to the end.
getStudent(int index) - returns the name of a student at the index specified.
Coding below was given to edit and use
public class ClassListTester
{
public static void main(String[] args)
{
//You don't need to change anything here, but feel free to add more Students!
Student alan = new Student("Alan", 11);
Student kevin = new Student("Kevin", 10);
Student annie = new Student("Annie", 12);
System.out.println(Student.printClassList());
System.out.println(Student.getLastStudent());
System.out.println(Student.getStudent(1));
Student.addStudent(2, new Student("Trevor", 12));
System.out.println(Student.printClassList());
System.out.println(Student.getClassSize());
}
}
import java.util.ArrayList;
public class Student
{
private String name;
private int grade;
//Implement classList here:
private static ArrayList classList = new ArrayList();
public Student(String name, int grade)
{
this.name = name;
this.grade = grade;
classList.add(this);
}
public String getName()
{
return this.name;
}
//Add the static methods here:
public static String printClassList()
{
String names = "";
for(Student name: classList)
{
names+= name.getName() + " ";
}
return "Student Class List: " + names;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started