Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is from Y. Dianiel Liang\'s Introduction to Java Programming Exercise 11.5.

ID: 3655058 • Letter: T

Question

This is from Y. Dianiel Liang's Introduction to Java Programming Exercise 11.5. The Course class. Rewrite the Course class found in Listing 10.6. Use the ArrayList to replace and array to store students. Do not change the orginal contract of the Course class (i.e., the definition of the constructors and methods should not be changed. Use the following template: public class Exercise11_5 { 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()); } } class Course { // Implement it } import java.util.*; 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"); course1.addStudent("Susan Kennedy"); course1.addStudent("John Kennedy"); course1.addStudent("Kim Johnson"); course1.addStudent("S1"); course1.addStudent("S2"); course1.addStudent("S3"); course1.addStudent("S4"); course1.addStudent("S5"); course1.addStudent("S6"); course1.addStudent("S7"); 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()); course1.dropStudent("S1"); System.out.println("Number of students in course1: " + course1.getNumberOfStudents()); students = course1.getStudents(); for (int i = 0; i < course1.getNumberOfStudents(); i++) System.out.print(students[i] + ", "); course1.clear(); System.out.println("Number of students in course1: " + course1.getNumberOfStudents()); } } 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) { // Your code here students.add(student); } public String[] getStudents() { // Write your code here String [] s1 = new String [numberOfStudents]; for (int i = 0; i < students.size(); i++) { //s1[i] = students[i]; s1.toArray(students); } return s1; } public int getNumberOfStudents() { // Write your code here for (int i = 0; i< students.size(); i++) { numberOfStudents++; } return numberOfStudents; } public String getCourseName() { return courseName; } public void dropStudent(String student) { // Write your code here students.remove(student); } public void clear() { // Write your code here students.clear(); } }

Explanation / Answer

Your idea of toArray(students) is a little off. If you look at the javadoc (google Arraylist javadoc) you will see that this will return an array of objects, which you then try to do a lot, but do not assign it to anything. So i cleaned up some code, fixed the for loop, and also got rid of numOfStudents as there is no need since students.size() will always return that anyways. http://codepad.org/a5saV893
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote