Question
having problem with the assignAll method package scheduler; import java.util.ArrayList; import java.util.List; /** * A class representing a Course. * * @author liberato * */
having problem with the assignAll method
package scheduler;
import java.util.ArrayList;
import java.util.List;
/**
* A class representing a Course.
*
* @author liberato
*
*/
public class Course {
/**
* Instantiates a new Course object. The course number must be non-empty, and the
* capacity must be greater than zero.
* @param courseNumber a course number, like "COMPSCI190D"
* @param capacity the maximum number of students that can be in the class
* @throws IllegalArgumentException thrown if the courseNumber or capacity are invalid
*/
private String courseNumber;
private int capacity;
private List
public Course(String courseNumber, int capacity) throws IllegalArgumentException {
this.courseNumber=courseNumber;
this.capacity=capacity;
Roster=new ArrayList<>();
if(courseNumber.isEmpty())
throw new IllegalArgumentException("the course number must be nonempty");
else if(capacity<=0)
throw new IllegalArgumentException("the course number must be nonempty");
}
/**
*
* @return the capacity of the course
*/
public int getCapacity() {
return capacity;
}
/**
*
* @return the course number
*/
public String getCourseNumber() {
return courseNumber;
}
/**
* Returns the list of students enrolled in the course.
*
* This returned object does not share state with the internal state of the Course.
*
* @return the list of students currently in the course
*/
public List
return Roster;
}
}
======================================================================
/*
* Copyright 2017 Marc Liberatore.
* Modified 2018 David Wemhoener
*/
package scheduler;
import java.util.ArrayList;
import java.util.List;
/**
* A class representing a student.
*
* @author liberato
*
*/
public class Student {
/**
*
* Instantiates a new Student object. The student's maximum course load must be greater
* than zero, and the preferences list must contain at least one course.
*
* The preference list is copied into this Student object.
*
* @param name the student's name
* @param int the student's id
* @param maxCourses the maximum number of courses that can be on this student's schedule
* @param preferences the student's ordered list of preferred courses
* @throws IllegalArgumentException thrown if the maxCourses or preferences are invalid
*/
private String name;
private int id;
private int maxCourses;
private List
private List
public Student(String name, int id, int maxCourses, List
this.name=name;
this.id=id;
this.maxCourses=maxCourses;
this.preferences=preferences;
Schedule=new ArrayList<>();
if(maxCourses<=0)
throw new IllegalArgumentException("the student's maximum course load must be greater than zero");
else if(preferences.isEmpty())
throw new IllegalArgumentException("the preferences list must contain at least one course");
}
/**
*
* @return the student's name
*/
public String getName() {
return name;
}
/**
*
* @return the student's id
*/
public int getID() {
return id;
}
/**
*
* @return the student's max course load
*/
public int getMaxCourses() {
return maxCourses;
}
/**
* Returns the student's list of course preferences, ordered from most- to least-desired.
*
* This returned object does not share state with the internal state of the Student.
*
* @return the student's preference list
*/
public List
return preferences;
}
/**
* Returns the student's current schedule.
*
* This returned object does not share state with the internal state of the Student.
*
* @return the student's schedule
*/
public List
return Schedule;
}
}
======================================================================
/*
* Copyright 2017 Marc Liberatore.
* Modified 2018 David Wemhoener.
*/
package scheduler;
import java.util.ArrayList;
import java.util.List;
public class Scheduler {
private List
private List
/**
* Instantiates a new, empty scheduler.
*/
public Scheduler() {
classes=new ArrayList<>();
students=new ArrayList<>();
}
/**
* Adds a course to the scheduler.
*
* @param course the course to be added
*/
public void addCourse(Course course) {
classes.add(course);
}
/**
* Returns the list of courses that this scheduler knows about.
*
* This returned object does not share state with the internal state of the Scheduler.
*
* @return the list of courses
*/
public List
List
for(int i=0;i { newCourses.add(classes.get(i)); } return newCourses; } /** * Adds a student to the scheduler. * * @param student the student to add */ public void addStudent(Student student) { students.add(student); } /** * Returns a list of the students this scheduler knows about. * * This returned object does not share state with the internal state of the Scheduler. * @return */ public List List for(int i=0;i { newRoster.add(students.get(i)); } return newRoster; } /** * Assigns all students to courses in the following manner: * * For a given student, check their list of preferred courses. Add them to the course that: * - exists in the scheduler's list of courses * - the student most prefers (that is, comes first in their preference list) * - the student is not not already enrolled in * - and is not full (in other words, at capacity) * Adds courses to the *end* of the student's current list of classes. Adds students to * the *end* of the course's roster. * * Repeat this process for each student, one-by-one; each student will now have one course, * usually (but not always) their most preferred course. * * Then repeat this whole process (adding one course per student, when possible, proceeding * round-robin among students), until there is nothing left to do: Students might * all be at their maximum number of courses, or there may be no available seats in courses * that students want. */ public void assignAll() { int j=0; boolean enrolled=false; for(int i=0;i { while(enrolled=false) { if(students.get(i).getPreferences().get(j).getRoster().size() { students.get(i).getSchedule().add(students.get(i).getPreferences().get(j)); students.get(i).getPreferences().get(j).getRoster().add(students.get(i)); j++; enrolled=true; } } } } /** * Drops a student from a course. * * @param student * @param course * @throws IllegalArgumentException if either the student or the course are not known to this scheduler */ public void drop(Student student, Course course) throws IllegalArgumentException { course.getRoster().remove(student); student.getSchedule().remove(course); if(students.contains(student)==false) throw new IllegalArgumentException("this student is not in the scheduler"); else if(classes.contains(course)==false) throw new IllegalArgumentException("this course is not in the scheduler"); } /** * Calculates the number of students interested in a course * * @param course * @throws IllegalArgumentException if the course is not known to this scheduler * @return */ public int interestLevel(Course course) throws IllegalArgumentException{ if(classes.contains(course)==false) throw new IllegalArgumentException("this course is not in the scheduler"); int interest=0; for(int i=0;i { if(students.get(i).getPreferences().contains(course)) interest++; } return interest; } }
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