Question

public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {...

public class Scheduler {
   private List<Course> classes;
   private List<Student> students;
   public Scheduler() {
       classes = new ArrayList<Course>();
       students = new ArrayList<Student>();
   }
  
   public void addCourse(Course course) {
       this.classes.add(course);
   }

   public List<Course> getCourses() {
       List<Course> newCList=new ArrayList<>();
       for(int i=0;i<classes.size();i++){
       newCList.add(classes.get(i));
       }
       return newCList;
   }
   public void addStudent(Student student) {
       this.students.add(student);

   }
  
   public List<Student> getStudents() {  
       List<Student> newSList= new ArrayList<>();
       for(int i=0;i<students.size();i++){
           newSList.add(students.get(i));
           }
       return newSList;
   }
  
   /**
   * 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 m;
       int count = 0;
       int rslt = 0;
       for(Student student: students){
           rslt += student.getPreferences().size();
       }
       while(count<=rslt*rslt){
           for(Student student: students){
               m = 0;
               for(Course course: student.getPreferences()){
                   if(m==0) count++;
                   if(m==0 && this.classes.contains(course)
                           && !course.getRoster().contains(student)
                           && course.getRoster().size()<course.getCapacity()
                           && student.get().size()<student.getMaxCourses()) {
                       student.get().add(course);
                       course.getRoster().add(student);
                       m = 1;
                   }
               }
           }
       }
       }

* Drops a student from a course.
   public void drop(Student student, Course course) throws IllegalArgumentException {
       if(!students.contains(student))
           throw new IllegalArgumentException("this student is not in the scheduler");
       else if(!classes.contains(course)) {
           throw new IllegalArgumentException("this course is not in the scheduler");
       }
       else //classes.getRoster().remove(course);
       course.getRoster().remove(student);
       student.get().remove(course);

   }
  
   /**
   * Drops a student from all of their courses.
   *
   * @param student
   * @throws IllegalArgumentException if the student is not known to this scheduler
   */
   public void unenroll(Student student) throws IllegalArgumentException{
       if(!students.contains(student))
           throw new IllegalArgumentException("this student is not in the scheduler");
       else
           getCourses().removeAll(student.get());
   }

  
}


public class Student {
   private String name;
   private int maxCourses;
   private List<Course> preferences;
   private ArrayList<Course> s = new ArrayList<Course>();

   public Student(String name, int maxCourses, List<Course> preferences) throws IllegalArgumentException {
   this.name = name;
   this.maxCourses = maxCourses;
   this.preferences = preferences;
  
       if(maxCourses<=0) {
       throw new IllegalArgumentException("the maxCourses are invalid");
   }
     
       if(preferences.isEmpty()) {
       throw new IllegalArgumentException("the preferences are invalid");
   }
   }
   public String getName() {
      
       return name;
   }
   public int getMaxCourses() {
      
       return maxCourses;
   }
   public List<Course> getPreferences() {
      
       return preferences;
   }
   public List<Course> getSchedule() {
       List<Course> a = new ArrayList<>();
       a.addAll(s);

       return new ArrayList<>(a);
   }
   public List<Course> get(){
       return this.s;
   }

  
}
public class Course {
   private String courseNumber;
   private int capacity;
   private ArrayList<Student> r = new ArrayList<Student>();
  
   public Course(String courseNumber, int capacity) throws IllegalArgumentException {
   this.courseNumber = courseNumber;
   this.capacity = capacity;
       if(courseNumber.isEmpty()) {
       throw new IllegalArgumentException("the courseNumber is invalid");
   }

   if(capacity <=0) {
       throw new IllegalArgumentException("the capacity is invalid");
   }
   }
  

   public int getCapacity() {
       return capacity;
   }
  
   public String getCourseNumber() {  
       return courseNumber;
   }
   public List<Student> getRoster() {
return this.r;
   }
}

here are the tests that I failed with:  How to fix them?
   @Test
   public void testThreeThenUnenrollAndReschedule() {
       Course d = new Course("DUTCH100", 2);
       Course e = new Course("ECON100", 3);
      
       Student s = new Student("s", 3, Arrays.asList(new Course[] {a, b, c, d, e}));
       Student t = new Student("t", 4, Arrays.asList(new Course[] {c, a, d, e, b}));
       Student u = new Student("u", 5, Arrays.asList(new Course[] {b, a, d, c, e}));
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addStudent(u);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.addCourse(c);
       scheduler.addCourse(d);
       scheduler.addCourse(e);
       scheduler.assignAll();
       scheduler.unenroll(s);
       scheduler.assignAll();
      
       checkList(Arrays.asList(new Student[] {s, t, u}), scheduler.getStudents());      
       checkList(Arrays.asList(new Course[] {a, b, c, d, e}), scheduler.getCourses());
       checkList(Arrays.asList(new Student[] {t, s}), a.getRoster());
       checkList(Arrays.asList(new Student[] {u, s}), b.getRoster());
       assertEquals(Arrays.asList(new Student[] {t}), c.getRoster());
       checkList(Arrays.asList(new Student[] {u, t}), d.getRoster());
       checkList(Arrays.asList(new Student[] {t, u, s}), e.getRoster());
       checkList(Arrays.asList(new Course[] {a, b, e}), s.getSchedule());
       checkList(Arrays.asList(new Course[] {c, a, e, d}), t.getSchedule());
       checkList(Arrays.asList(new Course[] {b, d, e}), u.getSchedule());
   }
}

   @Test
   public void testUnenrollOne() {      
       Student s = new Student("s", 2, listAB);
       Student t = new Student("t", 2, listBA);
       List<Student> listST = Arrays.asList(new Student[] {s, t});
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.assignAll();
       scheduler.unenroll(t);
      
       checkList(listST, scheduler.getStudents());
       checkList(listAB, scheduler.getCourses());
       checkList(Arrays.asList(new Student[] {s}), a.getRoster());
       assertEquals(Arrays.asList(new Student[] {s}), b.getRoster());
       checkList(listAB, s.getSchedule());
       assertEquals(new ArrayList<>(), t.getSchedule());
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TestMain.java

package scheduler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestMain {
  
   public static void main(String[] args) {
       //course selection
       Course a = new Course("ANTHRO100", 2);
       Course b = new Course("BIO100", 2);
       Course c = new Course("COMM100", 1);
       Course d = new Course("DUTCH100", 2);
       Course e = new Course("ECON100", 3);
      
       //created student object with name, maxCourse, and preference
       Student s = new Student("s", 3, Arrays.asList(new Course[] {a, b, c, d, e}));
       Student t = new Student("t", 4, Arrays.asList(new Course[] {c, a, d, e, b}));
       Student u = new Student("u", 5, Arrays.asList(new Course[] {b, a, d, c, e}));
      
       Scheduler scheduler = new Scheduler();
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addStudent(u);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.addCourse(c);
       scheduler.addCourse(d);
       scheduler.addCourse(e);
       scheduler.assignAll();
      
       //System.out.println("Pre-Dutch Capacity: " + d.getCurrentCapacity());
      
       System.out.println("Before Unenrolling S");
       System.out.println();
       scheduler.displayStudent(s);
       System.out.println();
      
       scheduler.displayStudent(t);
       System.out.println();
      
       scheduler.displayStudent(u);
       System.out.println();
       scheduler.unenroll(s);
      
       System.out.println();
       System.out.println();
       System.out.println("Unenrolling S, but not reassigning");
       System.out.println();
       scheduler.displayStudent(s);
       System.out.println();
      
       scheduler.displayStudent(t);
       System.out.println();
      
       scheduler.displayStudent(u);
       System.out.println();
      
       scheduler.assignAll();
       //System.out.println("Post-Dutch Capacity: " + d.getCurrentCapacity());
       //scheduler.displayStudent(s);
      
       System.out.println();
       System.out.println();
       System.out.println("Re-Assigning");
      
       scheduler.displayStudent(s);
       System.out.println();
      
       scheduler.displayStudent(t);
       System.out.println();
      
       scheduler.displayStudent(u);
       System.out.println();
   }

}


Course.java


import java.util.ArrayList;
import java.util.List;

public class Course {
  
   private String courseNumber;
   private int maxCapacity;
   private int currCapacity;
   List<Student> roster;

   public Course(String courseNumber, int capacity) throws IllegalArgumentException {
      
       //check is courseNumber of capacity are invalid
       if (capacity <= 0 || courseNumber == "") {
          
           throw new IllegalArgumentException();
       }
      
       this.courseNumber = courseNumber;
       this.maxCapacity = capacity;
       this.currCapacity = 0;
       this.roster = new ArrayList<Student>();
   }
  
   public int getCapacity() {
      
       return this.maxCapacity;
   }
  
   public int getCurrentCapacity() {
      
       return this.currCapacity;
   }
  
   public String getCourseNumber() {
      
       return this.courseNumber;
   }
  
   public void addToRoster(Student student) {
      
       //pushback the student object to the list of student objects
       this.roster.add(student);
      
       //update the capacity of the class
       this.currCapacity++;
   }
   public void removeFromRoster(Student student) {
      
       this.roster.remove(student);
       this.currCapacity--;
   }

   public List<Student> getRoster() {
      
       //copy over the old roster
       List<Student> newRoster = new ArrayList<Student>(roster);
      
       return newRoster;
   }
}


CourseTest.java

import org.junit.Test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;


public class CourseTest {
  

   private Course course;
   private String courseNumber;
   private int capacity;
  
   @Before
   public void setup() {
       courseNumber = "COMPSCI190D";
       capacity = 120;      
       course = new Course(courseNumber, capacity);
   }
  
   @Test
   public void testGetCourseNumber() {
       assertEquals(courseNumber, course.getCourseNumber());
   }

   @Test
   public void testGetCapacity() {
       assertEquals(capacity, course.getCapacity());
   }
  
   @Test
   public void testGetRosterEmpty() {
       assertTrue(course.getRoster().isEmpty());
   }
  
   @Test(expected = IllegalArgumentException.class)
   public void testInvalidCourseNumber() {
       Course c = new Course("", 20);
   }


   @Test(expected = IllegalArgumentException.class)
   public void testInvalidCapacity() {
       Course c = new Course("COMPSCI190D", 0);
   }
}

Scheduler.java


import java.util.ArrayList;
import java.util.List;

public class Scheduler {
  
   List<Course> courses;
  
   //When we add student objects to the scheduler, it keeps them in a list
   List<Student> students;
  
   public Scheduler() {
      
       //both lists are currently empty
       this.courses = new ArrayList<>();
       this.students = new ArrayList<>();
   }
  
   public void addCourse(Course course) {
      
       this.courses.add(course);
   }
  
   public List<Course> getCourses() {
      
       List<Course> copy = new ArrayList<>(this.courses);
      
       return copy;
   }
  
   public void addStudent(Student student) {
      
       this.students.add(student);
   }
  
   public List<Student> getStudents() {
      
       List<Student> copy = new ArrayList<>(this.students);
      
       return copy;
   }
   public void assignAll() {
      
       boolean assign = false;
       int taskDone = 1;
       int i;
       while(taskDone > 0) {
          
           //reassigns i = 0;
           i = 0;
          
           //reassigns taskDone = 0;
           taskDone = 0;
           for (i = 0; i < this.students.size(); i++) {
              
               //reset the flag
               assign = false;
              
               //check if the student is at their maxCourse, if so do not assign this poor student more than he/she wants
               if (this.students.get(i).getMaxCourses() != this.students.get(i).getCurrentNumCourses()) {
                   for (int j = 0; j < this.students.get(i).getPreferences().size() && !assign; j++) {
                      
                       //if it exists within our list
                       if (this.courses.contains(this.students.get(i).getPreferences().get(j)) &&
                          
                           //if they don't already have this class in their schedule
                           !(this.students.get(i).getSchedule().contains(this.students.get(i).getPreferences().get(j))) &&
                          
                           //if the class still has space to add one more student
                           this.students.get(i).getPreferences().get(j).getCurrentCapacity() != this.students.get(i).getPreferences().get(j).getCapacity()) {
              
                           //assign that them the class, also need to update the roster of the class
                           this.students.get(i).addToSchedule(this.students.get(i).getPreferences().get(j));
                           this.students.get(i).getPreferences().get(j).addToRoster(this.students.get(i));
                          
                           //provides a condition to exit the loop
                           assign = true;
                          
                           //we have done an assignment
                           taskDone++;
                       }
                   }
               }
           }
       }
   }

   public void drop(Student student, Course course) throws IllegalArgumentException {
      
       //if the student or course objects are not known to this class
       if (!(this.students.contains(student)) || !(this.courses.contains(course))) {
          
           throw new IllegalArgumentException();
       }
      
       //call the method in Student that will drop the course
       student.dropFromSchedule(course);
       course.removeFromRoster(student);
   }
   public void unenroll(Student student) throws IllegalArgumentException {
      
       //if the list of students does not contain the the Student object passed
       if (!this.students.contains(student)) {
          
           //throw exception
           throw new IllegalArgumentException();
       }
      
       //the student drops all the classes from schedule
       student.dropAllFromSchedule();
   }
   public void displayStudent(Student student) {
      
       student.display();
   }
}


SchedulerTest.java
import org.junit.Test;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;


public class SchedulerTest {

   private Scheduler scheduler;
  
   private Scheduler singleScheduler;
   private Course courseOne;
   private Student studentOne;

   private Course a;
   private Course b;
   private Course c;
   private List<Course> listA;
   private List<Course> listB;
   private List<Course> listAB;
   private List<Course> listBA;  
   private List<Course> listAC;
   private List<Course> listCA;  

   @Before
   public void setup() {
       scheduler = new Scheduler();
       courseOne = new Course("ONE1", 1);
       List<Course> l = new ArrayList<>();
       l.add(courseOne);
       studentOne = new Student("one", 1, l);
       singleScheduler = new Scheduler();
       singleScheduler.addStudent(studentOne);
       singleScheduler.addCourse(courseOne);

       a = new Course("ANTHRO100", 2);
       b = new Course("BIO100", 2);
       c = new Course("COMM100", 1);
      
       listA = Arrays.asList(new Course[] {a});
       listB = Arrays.asList(new Course[] {b});
       listAB = Arrays.asList(new Course[] {a, b});
       listBA = Arrays.asList(new Course[] {b, a});
       listAC = Arrays.asList(new Course[] {a, c});
       listCA = Arrays.asList(new Course[] {c, a});
   }

   private static <E> void checkList(List<E> expected, List<E> actual) {
       assertEquals(expected.size(), actual.size());
       for (E e: expected) {
           assertTrue(actual.contains(e));
       }
   }
  
   @Test
   public void testGetCoursesEmpty() {
       assertTrue(scheduler.getCourses().isEmpty());
   }

   @Test
   public void testGetStudentsEmpty() {
       assertTrue(scheduler.getStudents().isEmpty());
   }

   @Test
   public void testAddOneCourse() {
       assertEquals(1, singleScheduler.getCourses().size());
       assertTrue(singleScheduler.getCourses().contains(courseOne));
   }

   @Test
   public void testAddOneStudent() {
       assertEquals(1, singleScheduler.getStudents().size());
       assertTrue(singleScheduler.getStudents().contains(studentOne));
   }

   @Test
   public void testGetCoursesNotShared() {
       List<Course> l = singleScheduler.getCourses();
       l.clear();
       assertTrue(singleScheduler.getCourses().contains(courseOne));
   }

   @Test
   public void testGetStudentsNotShared() {
       List<Student> l = singleScheduler.getStudents();
       l.clear();
       assertTrue(singleScheduler.getStudents().contains(studentOne));
   }
  
   @Test
   public void testSingleCourse() {      
       Student s = new Student("s", 1, listA);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(a);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       assertEquals(listA, scheduler.getCourses());
       assertEquals(listS, a.getRoster());
       assertEquals(listA, s.getSchedule());
   }
  
   @Test
   public void testDropSimple() {      
       Student s = new Student("s", 1, listA);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(a);
       scheduler.assignAll();
       scheduler.drop(s, a);
      
       assertEquals(listS, scheduler.getStudents());
       assertEquals(listA, scheduler.getCourses());
       assertTrue(s.getSchedule().isEmpty());
       assertTrue(a.getRoster().isEmpty());
   }

   @Test
   public void testSingleUnavailableCourse() {      
       Student s = new Student("s", 1, listA);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(b);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       assertEquals(Arrays.asList(new Course[] {b}), scheduler.getCourses());
       assertTrue(a.getRoster().isEmpty());
       assertTrue(b.getRoster().isEmpty());
   }
  
   @Test
   public void testPreferFirst() {      
       Student s = new Student("s", 1, listAB);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       checkList(listAB, scheduler.getCourses());
       assertEquals(listS, a.getRoster());
       assertEquals(listA, s.getSchedule());
       assertTrue(b.getRoster().isEmpty());
   }

   @Test
   public void testPreferSecond() {      
       Student s = new Student("s", 1, listBA);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(b);
       scheduler.addCourse(a);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       checkList(listBA, scheduler.getCourses());
       assertEquals(listS, b.getRoster());
       assertEquals(listB, s.getSchedule());
       assertTrue(a.getRoster().isEmpty());
   }
  
   @Test
   public void testAddTwo() {      
       Student s = new Student("s", 2, listAB);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(b);
       scheduler.addCourse(a);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       checkList(listBA, scheduler.getCourses());
       assertEquals(listS, a.getRoster());
       assertEquals(listS, b.getRoster());
      
       assertEquals(2, s.getSchedule().size());
       checkList(listAB, s.getSchedule());
   }
  
   @Test
   public void testAddOnlyOne() {      
       Student s = new Student("s", 1, listAB);
       List<Student> listS = Arrays.asList(new Student[] {s});
      
       scheduler.addStudent(s);
       scheduler.addCourse(b);
       scheduler.addCourse(a);
       scheduler.assignAll();
      
       assertEquals(listS, scheduler.getStudents());
       checkList(listBA, scheduler.getCourses());
       assertEquals(listS, a.getRoster());
       assertTrue(b.getRoster().isEmpty());
       assertEquals(listA, s.getSchedule());
   }
  
   @Test
   public void testTwoInTwo() {      
       Student s = new Student("s", 2, listAB);
       Student t = new Student("t", 2, listBA);
       List<Student> listST = Arrays.asList(new Student[] {s, t});
       List<Student> listTS = Arrays.asList(new Student[] {t, s});
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.assignAll();
      
       checkList(listST, scheduler.getStudents());
       checkList(listAB, scheduler.getCourses());
       checkList(listST, a.getRoster());
       checkList(listTS, b.getRoster());
       checkList(listAB, s.getSchedule());
       checkList(listBA, t.getSchedule());
   }
  
   @Test
   public void testDropFromOne() {      
       Student s = new Student("s", 2, listAB);
       Student t = new Student("t", 2, listBA);
       List<Student> listST = Arrays.asList(new Student[] {s, t});
       List<Student> listTS = Arrays.asList(new Student[] {t, s});
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.assignAll();
       scheduler.drop(t, a);
      
       checkList(listST, scheduler.getStudents());
       checkList(listAB, scheduler.getCourses());
       assertEquals(Arrays.asList(new Student[] {s}), a.getRoster());
       checkList(listTS, b.getRoster());
       checkList(listAB, s.getSchedule());
       assertEquals(listB, t.getSchedule());
   }
  
   @Test
   public void testUnenrollOne() {      
       Student s = new Student("s", 2, listAB);
       Student t = new Student("t", 2, listBA);
       List<Student> listST = Arrays.asList(new Student[] {s, t});
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.assignAll();
       scheduler.unenroll(t);
      
       checkList(listST, scheduler.getStudents());
       checkList(listAB, scheduler.getCourses());
       checkList(Arrays.asList(new Student[] {s}), a.getRoster());
       assertEquals(Arrays.asList(new Student[] {s}), b.getRoster());
       checkList(listAB, s.getSchedule());
       assertEquals(new ArrayList<>(), t.getSchedule());
   }
  
   @Test
   public void testOneFull() {      
       Student s = new Student("s", 2, listAC);
       Student t = new Student("t", 2, listCA);
       List<Student> listT = Arrays.asList(new Student[] {t});
       List<Student> listST = Arrays.asList(new Student[] {s, t});
       List<Student> listTS = Arrays.asList(new Student[] {t, s});
      
       scheduler.addStudent(t);
       scheduler.addStudent(s);
       scheduler.addCourse(c);
       scheduler.addCourse(a);
       scheduler.assignAll();
      
       checkList(listTS, scheduler.getStudents());
       checkList(listCA, scheduler.getCourses());
       checkList(listST, a.getRoster());
       assertEquals(listT, c.getRoster());
       assertEquals(listA, s.getSchedule());
       checkList(listCA, t.getSchedule());
   }
  
   @Test
   public void testOtherFull() {      
       Student s = new Student("s", 2, listAC);
       Student t = new Student("t", 2, listCA);
       List<Student> listT = Arrays.asList(new Student[] {t});
       List<Student> listST = Arrays.asList(new Student[] {s, t});
       List<Student> listTS = Arrays.asList(new Student[] {t, s});
      
       scheduler.addStudent(t);
       scheduler.addStudent(s);
       scheduler.addCourse(a);
       scheduler.addCourse(c);
       scheduler.assignAll();
      
       checkList(listTS, scheduler.getStudents());
       checkList(listAC, scheduler.getCourses());
       checkList(listST, a.getRoster());
       assertEquals(listT, c.getRoster());
       assertEquals(listA, s.getSchedule());
       checkList(listCA, t.getSchedule());
   }
  
   @Test
   public void testThree() {
       Course d = new Course("DUTCH100", 2);
       Course e = new Course("ECON100", 3);
      
       Student s = new Student("s", 3, Arrays.asList(new Course[] {a, b, c, d, e}));
       Student t = new Student("t", 4, Arrays.asList(new Course[] {c, a, d, e, b}));
       Student u = new Student("u", 5, Arrays.asList(new Course[] {b, a, d, c, e}));
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addStudent(u);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.addCourse(c);
       scheduler.addCourse(d);
       scheduler.addCourse(e);
       scheduler.assignAll();
      
       checkList(Arrays.asList(new Student[] {s, t, u}), scheduler.getStudents());      
       checkList(Arrays.asList(new Course[] {a, b, c, d, e}), scheduler.getCourses());
       checkList(Arrays.asList(new Student[] {s, t}), a.getRoster());
       checkList(Arrays.asList(new Student[] {u, s}), b.getRoster());
       assertEquals(Arrays.asList(new Student[] {t}), c.getRoster());
       checkList(Arrays.asList(new Student[] {u, s}), d.getRoster());
       checkList(Arrays.asList(new Student[] {t, u}), e.getRoster());
       checkList(Arrays.asList(new Course[] {a, b, d}), s.getSchedule());
       checkList(Arrays.asList(new Course[] {c, a, e}), t.getSchedule());
       checkList(Arrays.asList(new Course[] {b, d, e}), u.getSchedule());
   }
  
   @Test
   public void testThreeThenUnenrollAndReschedule() {
       Course d = new Course("DUTCH100", 2);
       Course e = new Course("ECON100", 3);
      
       Student s = new Student("s", 3, Arrays.asList(new Course[] {a, b, c, d, e}));
       Student t = new Student("t", 4, Arrays.asList(new Course[] {c, a, d, e, b}));
       Student u = new Student("u", 5, Arrays.asList(new Course[] {b, a, d, c, e}));
      
       scheduler.addStudent(s);
       scheduler.addStudent(t);
       scheduler.addStudent(u);
       scheduler.addCourse(a);
       scheduler.addCourse(b);
       scheduler.addCourse(c);
       scheduler.addCourse(d);
       scheduler.addCourse(e);
       scheduler.assignAll();
       scheduler.unenroll(s);
       scheduler.assignAll();
      
       checkList(Arrays.asList(new Student[] {s, t, u}), scheduler.getStudents());      
       checkList(Arrays.asList(new Course[] {a, b, c, d, e}), scheduler.getCourses());
       checkList(Arrays.asList(new Student[] {t, s}), a.getRoster());
       checkList(Arrays.asList(new Student[] {u, s}), b.getRoster());
       assertEquals(Arrays.asList(new Student[] {t}), c.getRoster());
       checkList(Arrays.asList(new Student[] {u, t}), d.getRoster());
       checkList(Arrays.asList(new Student[] {t, u, s}), e.getRoster());
       checkList(Arrays.asList(new Course[] {a, b, e}), s.getSchedule());
       checkList(Arrays.asList(new Course[] {c, a, e, d}), t.getSchedule());
       checkList(Arrays.asList(new Course[] {b, d, e}), u.getSchedule());
   }
}


Student.java

import java.util.ArrayList;
import java.util.List;

public class Student {
  
   private String name;
   private int maxCourses;
   private int currCourses;
   private List<Course> preferences;
   private List<Course> schedule;
   public Student(String name, int maxCourses, List<Course> preferences) throws IllegalArgumentException {
      
       //check for valid maxCourses and preference value
       if (maxCourses < 1 || preferences.size() < 1) {
          
           throw new IllegalArgumentException();
       }
      
       //store the values passed in the constructor to the class
       this.name = name;
       this.maxCourses = maxCourses;
       this.currCourses = 0;
       this.preferences = preferences;
       this.schedule = new ArrayList<Course>();
   }
  
   public String getName() {
      
       return this.name;
   }
   public int getMaxCourses() {
      
       return this.maxCourses;
   }
  
   public int getCurrentNumCourses() {
      
       return this.currCourses;
   }
   public List<Course> getPreferences() {
      
       List<Course> copy = new ArrayList<Course>(preferences);
      
       return copy;
   }
   public void addToSchedule(Course course) {
      
       this.schedule.add(course);
       this.currCourses++;
   }
   public List<Course> getSchedule() {
      
       List<Course> copy = new ArrayList<Course>(this.schedule);
      
       return copy;
   }
  
   public void dropFromSchedule(Course courseToDrop) {
      
       //search for that course in the schedule list
       for (int i = 0; i < this.schedule.size(); i++) {
          
           //if we have found to course-to-drop
           if (this.schedule.get(i).equals(courseToDrop)) {
              
               //drop it
               this.schedule.remove(i);
           }
       }
      
       this.currCourses--;
   }
   public void dropAllFromSchedule() {
      
       //removes the student from each course roster
       for (int i = 0; i < this.schedule.size(); i++) {
          
           schedule.get(i).removeFromRoster(this);
           this.currCourses--;
       }
      
       //removes all the Course objects from the Schedule list
       this.schedule.clear();
   }
  
   public void display() {
      
       System.out.println("Displaying the Student's name, maxCourses, preference, and schedule.");
      
       System.out.println("Name: " + this.getName());
       System.out.println("Max Courses: " + this.getMaxCourses());
      
       for (int i = 0; i < this.getPreferences().size(); i++) {
          
           System.out.println("Course Number: " + this.getPreferences().get(i).getCourseNumber() + "\t\tCourse Capacity: " + this.getPreferences().get(i).getCapacity());
       }
      
       System.out.println();
      
       if (this.schedule.isEmpty()) {
          
           System.out.println("The student schedule is empty right now");
       }
      
       else {
          
           System.out.println("Schedule: ");
          
           for (int i = 0; i < this.schedule.size(); i++) {
              
               System.out.println(this.schedule.get(i).getCourseNumber());
           }
       }
   }
}

StudentTest.java


import org.junit.Test;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;


public class StudentTest {

   private Student joe;
   private List<Course> courseList;
  
   @Before
   public void setup() {
       courseList = new ArrayList<>();
       courseList.add(new Course("COMPSCI190D", 120));
       courseList.add(new Course("MATH132", 50));
       courseList.add(new Course("ENGLWRIT112", 20));
      
       joe = new Student("Joe", 4, courseList);
   }
  
   @Test
   public void testGetName() {
       assertEquals("Joe", joe.getName());
   }

   @Test
   public void testGetMaxCourses() {
       assertEquals(4, joe.getMaxCourses());
   }


   @Test
   public void testGetPreferences() {
       assertEquals(courseList, joe.getPreferences());
   }

   @Test
   public void testGetPreferencesNotShared() {
       List<Course> prefs = joe.getPreferences();
       prefs.clear();
       assertEquals(courseList, joe.getPreferences());
   }
  
   @Test
   public void testGetScheduleEmpty() {
       assertTrue(joe.getSchedule().isEmpty());
   }

   @Test
   public void testGetScheduleEmptyNotShared() {
       List<Course> schedule = joe.getSchedule();
       schedule.add(new Course("COMPSCI190D", 120));
       assertTrue(joe.getSchedule().isEmpty());
   }

   @Test(expected = IllegalArgumentException.class)
   public void testInvalidMaxCourses() {
       Student s = new Student("NoCourses", 0, courseList);
   }

   @Test(expected = IllegalArgumentException.class)
   public void testEmptyPreferences() {
       Student s = new Student("DontCare", 3, new ArrayList<Course>());
   }
}

Add a comment
Know the answer?
Add Answer to:
public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need some help fixing this error. I completed the code, just modify it so the...

    I need some help fixing this error. I completed the code, just modify it so the error is gone. I had to remove the imports and parts of the test to fit everything public class Scheduler {    private List listOfCourses;    private List listOfStudents;    public Scheduler() {    listOfCourses = new ArrayList();    listOfStudents = new ArrayList();    }    public void addCourse(Course course) {    listOfCourses.add(course);    }    public List getCourses() {    List courseList =...

  • DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT...

    DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT ANSWER MY QUESTIONS package scheduler; import java.util.List; public class Scheduler { /** * Instantiates a new, empty scheduler. */ public Scheduler() { } /** * Adds a course to the scheduler. * * @param course the course to be added */ public void addCourse(Course course) { } /** * Returns the list of courses that this scheduler knows about. * * This returned object...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • 10. Observe the following simple program: public class Student : Person { private string name; private...

    10. Observe the following simple program: public class Student : Person { private string name; private List<string> courses; public void AddCourse(string course) { courses.Add(course); public void PrintCourses() { foreach(string course in courses) { Console.WriteLine(course); a) What is the access modifier of the member field name? b) What is the access modifier of the member method AddCourse? c) What is the immediate base class of Student?

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT