JAVA HELP APPRECIATED

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

I am having trouble fixing my add and drop methods in the course class, can somebody help me? let me know if you need addition information! import java.util.Arrays; public class Course { //instance field private String courseName; private int maximumStudents; private Student[] classRoster; //constructor with no students on the roster public Course(String courseName, int maximumStudents) { this.courseName = courseName; if(maximumStudents > 0) { this.maximumStudents = maximumStudents; } this.classRoster = new Student[0]; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public int getMaximumStudents() { return maximumStudents; } public void setMaximumStudents(int maximumStudents) { if(maximumStudents > 0) { this.maximumStudents = maximumStudents; } } public Student[] getClassRoster() { return classRoster; } public void setClassRoster(Student[] classRoster) { this.classRoster = classRoster; } //adds the student if there is room and they paid their tuition public boolean addStudent(Student student) { if(classRoster.length = 0 ; i--) { if(studentOnRoster) { for(int j = 0; j < classRoster.length; j++){ classRoster[i] = classRoster[i + 1]; student = classRoster[i]; } } else { if(classRoster[i].getStudentId().equals(student.getStudentId())) studentOnRoster = true; } } return studentOnRoster; } //returns the course information public String toString() { String studentInfo = courseName + " (" + maximumStudents + " student capacity)" + "\nEnrollment: " + classRoster.length + "\n"; for (Student student : classRoster) { System.out.println(student); } return studentInfo; } }

codiii · Feb 24, 2017 11:52 AM · 499 views

4 Replies

//adds the student if there is room and they paid their tuition&nbsp; public boolean addStudent(Student student) {&nbsp; if(classRoster.length &lt; maximumStudents &amp;&amp; student.getTuitionPaid()) {&nbsp; classRoster[length] = student;&nbsp; return true;&nbsp; }&nbsp; return false;&nbsp; } //drops the student if they are in the roster&nbsp; public boolean dropStudent(Student student) {&nbsp; boolean studentOnRoster = false; for (int i = 0; i &lt; classRoster.length; i++) { if(classRoster[i].getStudentId().equals(student.getStudentId())) { studentOnRoster = true;&nbsp; for(int j = i+1; j &lt; classRoster.length; j++) { classRoster[j-1] = classRoster[j]; } break; } } return studentOnRoster;&nbsp; }&nbsp; Assumptions:&nbsp; 1. addStudent(..): New student to be added is not already in the classRoster.&nbsp; 2. dropStudent(..): ArrayUtils.removeElement(array, element) may not be allowed in your course class. Workaround: Use nested for-loop to delete a student object. Last edited: 24-Feb-17 01:15 PM

c864916 · Feb 24, 2017 1:15 PM

Hello, Thank you for the suggestion! but that solution will give me an out of bound error. I will include the other files as well as the tester program:) //Student class public class Student { //instance field private String firstName; private String lastName; private String studentId; private boolean tuitionPaid; //constructor public Student(String firstName, String lastName, String studentId, boolean tuitionPaid) { this.firstName = firstName; this.lastName = lastName; this.studentId = studentId; this.tuitionPaid = tuitionPaid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } public boolean getTuitionPaid() { return tuitionPaid; } public void setTuitionPaid(boolean tuitionPaid) { this.tuitionPaid = tuitionPaid; } //returns information about the student public String t0skript() { return firstName + " " + lastName + " (" + studentId + ")"; } } //Course class import java.util.Arrays; public class Course { //instance field private String courseName; private int maximumStudents; private Student[] classRoster; //constructor with no students on the roster public Course(String courseName, int maximumStudents) { this.courseName = courseName; if(maximumStudents &gt; 0) { this.maximumStudents = maximumStudents; } this.classRoster = new Student[0]; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public int getMaximumStudents() { return maximumStudents; } public void setMaximumStudents(int maximumStudents) { if(maximumStudents &gt; 0) { this.maximumStudents = maximumStudents; } } public Student[] getClassRoster() { return classRoster; } public void setClassRoster(Student[] classRoster) { this.classRoster = classRoster; } //adds the student if there is room and they paid their tuition&nbsp; &nbsp; &nbsp; public boolean addStudent(Student student) {&nbsp; &nbsp; &nbsp; if(classRoster.length &lt; maximumStudents &amp;&amp; student.getTuitionPaid()) {&nbsp; &nbsp; &nbsp; for(int i = classRoster.length; i &lt; classRoster.length; i++) { &nbsp; &nbsp; classRoster[i + 1] = classRoster[i];&nbsp; &nbsp; &nbsp; classRoster[i] = student; &nbsp; &nbsp; } &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; //drops the student if they are in the roster&nbsp; &nbsp; &nbsp; public boolean dropStudent(Student student) {&nbsp; &nbsp; &nbsp; boolean studentOnRoster = false; &nbsp; &nbsp; for (int i = 0; i &lt; classRoster.length; i++) { &nbsp; &nbsp; if(classRoster[i].getStudentId().equals(student.getStudentId())) { &nbsp; &nbsp; studentOnRoster = true;&nbsp; &nbsp; &nbsp; for(int j = i + 1; j &lt; classRoster.length; j++) { &nbsp; &nbsp; classRoster[j - 1] = classRoster[j];&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; break; &nbsp; &nbsp; } &nbsp; &nbsp; } &nbsp; &nbsp; return studentOnRoster;&nbsp; &nbsp; &nbsp; } //returns the course information public String t0skript() { String studentInfo = courseName + " (" + maximumStudents + " student capacity)" + "\nEnrollment: " + classRoster.length + "\n"; for (Student student : classRoster) { System.out.println(student); } return studentInfo; } } //Tester public class CourseDriver { public static void main(String[] args) { Student[] studentsInSchool = new Student[7]; studentsInSchool[0] = new Student("Adam", "Ant", "S925", true); studentsInSchool[1] = new Student("Bob", "Barker", "S713", false); studentsInSchool[2] = new Student("Chevy", "Chase", "S512", true); studentsInSchool[3] = new Student("Doris", "Day", "S513", true); studentsInSchool[4] = new Student("Emilio", "Estevez", "S516", false); studentsInSchool[5] = new Student("Farrah", "Fawcet", "S956", true); studentsInSchool[6] = new Student("Greta", "Garbo", "S419", true); Course course = new Course("Media Studies", 5); System.out.println("******Testing t0skript: should print name, max enrollment, " + "and a message that the roster is empty.*****"); System.out.println(course); System.out.println("******Testing add method: should add Adam, Chevy, Doris, Farrah, and Greta. " + "Should not add Bob or Emilio.*****"); for(int i = 0; i &lt; studentsInSchool.length; i++) { Student currentStudent = studentsInSchool[i]; if(course.addStudent(currentStudent)) { System.out.println(currentStudent + " added."); } else { System.out.println(currentStudent + " was not added."); } } System.out.println(); System.out.println("******Testing&nbsp;t0skript&nbsp;and add methods: should print name, max enrollment, " + "number enrolled, and the roster.*****"); System.out.println(course); System.out.println("******Testing drop and&nbsp;t0skript&nbsp;methods: no changes should be made when " + " trying to drop a student not in the course"); course.dropStudent(studentsInSchool[4]); System.out.println(course); System.out.println("******Testing drop and&nbsp;t0skript&nbsp;methods: Chevy should be dropped"); course.dropStudent(studentsInSchool[2]); System.out.println(course); System.out.println("******Testing add, drop, and&nbsp;t0skript&nbsp;methods: Bob should now be added " + " because he paid tuition and now there is room"); studentsInSchool[1].setTuitionPaid(true); course.addStudent(studentsInSchool[1]); System.out.println(course); } } Last edited: 24-Feb-17 01:43 PM Last edited: 24-Feb-17 03:22 PM

codiii · Feb 24, 2017 1:42 PM

Should I add a for loop in the add method?

codiii · Feb 24, 2017 2:24 PM

I edited the add and drop methods in the last post and it compiles but it doesn't show the roster with the students added :/

codiii · Feb 24, 2017 3:24 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion