Question
I need help with the following Java exercise. Please read instructions. A simple, basic solution would be appreciated. Use an ArrayList to replace an array
I need help with the following Java exercise. Please read instructions. A simple, basic solution would be appreciated.
Use an ArrayList to replace an array to store students. You should not change the original contract of the Course class(i.e. the definition of the constructors and methods should not be changed -the signature stays the same). You will change the property private String[ ] students = newString[100]; to private ArrayList students = new ArrayList();. You will need to import java.util.* to get the ArrayList class.
Listing 10.6 Course.java is provided for you. Follow the instructions. Do not change the original contract of the Course class. Instead, ONLY change the implementation of students, from an array to and ArrayList, and update any methods in class Course that access student to reflect ArrayList. The methods are overridden. That means that you do not need to change the header for methods, only the body of the method. The method signature remains public String[]getStudents() but the body of the method will remove the strings from the ArrayList put them in an Array of String and return the Array of String. A testfile, Exercise11_05.java has been provided for your use. DO NOT alter the testfile Exercise11_05.java. Contractall method names, method parameters, and method return types must stay the same.
This problem is an exercise in abstraction and encapsulation.
Course.java
public class Course { private String courseName; //change String[] to an ArrayList of String 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++; }
//Keep the return type at String[] public String[] getStudents() { return students; }
public int getNumberOfStudents() { return numberOfStudents; }
public String getCourseName() { return courseName; }
public void dropStudent(String student) { // Left as an exercise in Exercise 10.9 } }
testfile Exercise11_05
public class Exercise11_05 { public static void main(String[] args) { Course course1 = new Course("Data Structures"); Course course2 = new Course("Database Systems");
course1.addStudent("Peter Jones"); course1.addStudent("Brian Smith"); course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones"); course2.addStudent("Steve Smith");
System.out.println("Number of students in course1: " + course1.getNumberOfStudents()); String[] students = course1.getStudents(); for (int i = 0; i < course1.getNumberOfStudents(); i++) System.out.print(students[i] + ", ");
System.out.println(); System.out.print("Number of students in course2: " + course2.getNumberOfStudents()); } }
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