Rewrite the Course class from Listing 10.6. Use an ArrayList to store students.
ID: 3655807 • Letter: R
Question
Rewrite the Course class from Listing 10.6. Use an ArrayList to store students. Do not change the signatures (contract, API) of the original class Course. Implement all methods of course from Listing 10.6. public class Course { private String courseName; private String [] students = new String [100]; private int numberOfStudents; public Course(String courseName){ this.courseName=courseName; } public void addStudent(String student){ students[numberOfStudents]=student; numberOfStudents++; } public String [] getStudents() { return students; } public int getNumberOfStudents() { return numberOfStudents; } public String getCouseName() { return courseName; } public void dropStudent(String student) { } }Explanation / Answer
import java.util.ArrayList; public class Course { private String courseName; private ArrayList students = new ArrayList(); private int numberOfStudents; public Course(String courseName){ this.courseName=courseName; } public void addStudent(String student){ students.add(student); numberOfStudents++; } public ArrayList getStudents() { return students; } public int getNumberOfStudents() { return numberOfStudents; } public String getCouseName() { return courseName; } public void dropStudent(String student) { } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.