Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Schedule class *** Course.java *** import java.time.LocalTime; import java.util.HashSet; import java.util.Set; public class Course { private String courseName; private Set weekDays; // a

Create a Schedule class

image text in transcribed

*** Course.java ***

import java.time.LocalTime; import java.util.HashSet; import java.util.Set; public class Course { private String courseName; private Set weekDays; // a set used to store days private int noOfCredits, classDuration; private LocalTime startTime; public Course() { weekDays=new HashSet(); //{'M','T','W'} } public Course(String courseName, Set weekDays, int noOfCredits, int classDuration, LocalTime startTime) { if(weekDays.isEmpty()) { throw new IllegalArgumentException("WeekDays is empty"); //IllegalArgumentException is thrown if weekdays is empty } if(noOfCredits=5) { throw new IllegalArgumentException("Credit should be from 1 to 5"); //IllegalArgumentException is thrown if credits is not in range of 1-5 } this.courseName = courseName; this.weekDays = weekDays; this.noOfCredits = noOfCredits; this.classDuration = classDuration; this.startTime = startTime; } public LocalTime getStartTime() { return startTime; } public LocalTime getEndTime() { return startTime.plusMinutes(classDuration); } public void setStartTime(LocalTime startTime) { this.startTime = startTime; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Set getWeekDays() { return weekDays; } public void setWeekDays(Set weekDays) { this.weekDays = weekDays; } public int getNoOfCredits() { return noOfCredits; } public void setNoOfCredits(int noOfCredits) { this.noOfCredits = noOfCredits; } public int getClassDuration() { return classDuration; } public void setClassDuration(int classDuration) { this.classDuration = classDuration; } @Override public int hashCode() { //generated hashcode method; always better to use it along with equals method final int prime = 31; int result = 1; result = prime * result + classDuration; result = prime * result + ((courseName == null) ? 0 : courseName.hashCode()); result = prime * result + noOfCredits; result = prime * result + ((startTime == null) ? 0 : startTime.hashCode()); result = prime * result + ((weekDays == null) ? 0 : weekDays.hashCode()); return result; } @Override public boolean equals(Object obj) { // equals method to compare the state of the two objects based on all the variables value if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Course other = (Course) obj; if (classDuration != other.classDuration) return false; if (courseName == null) { if (other.courseName != null) return false; } else if (!courseName.equals(other.courseName)) return false; if (noOfCredits != other.noOfCredits) return false; if (startTime == null) { if (other.startTime != null) return false; } else if (!startTime.equals(other.startTime)) return false; if (weekDays == null) { if (other.weekDays != null) return false; } else if (!weekDays.equals(other.weekDays)) return false; return true; } public boolean conflictsWith(Course course) //method to check if a course schedule conflicts with given course's schedule or not { if(this.getWeekDays().containsAll(course.getWeekDays())) { if(this.getStartTime().equals(course.getStartTime()) || isOverlapping(course)) { return true; } } return false; } public boolean isOverlapping(Course course) //helper function for conflictsWith() function { LocalTime start1=getStartTime(); LocalTime end1=getEndTime(); LocalTime start2=course.getStartTime(); LocalTime end2=course.getEndTime(); return start1.isBefore(end2) && start2.isBefore(end1); } public boolean contains(char day, LocalTime time) //checks whether the given date & time lies in between the course schedule or not { if(getWeekDays().contains(day)) { if(getStartTime().isBefore(time) && getEndTime().isAfter(time)) { return true; } } return false; } @Override public String toString() // string representation of the Course object { String days=""; for (char c : getWeekDays()) { days=days+c; } return courseName + ", "+ noOfCredits+ ", "+ days +", "+ startTime + ", " + classDuration; } }
Schedule class: A Schedule object stores information about the collection of courses in which a student is enrolled. It should have the following public behavior. Unless otherwise specified, every method of class Schedule should run in O(n) time where n is the number of courses in the schedule. Use Java Collection's List to manage the courses. The Schedule class should implement the Cloneable interface so that client code will be able to use its clone method. Note that this document does not specify what internal collection or courses in the order indicated by the comparator passed to the save method. ng to use for in a schedule. But when saving a schedule, you save the Method Description Schedule() Constructor that creates a new empty schedule. Should run in O1) time. Adds the given course to this schedule. If the given course conflicts with any of the courses in the schedule as defined previously, a ScheduleConflictException should be thrown. (The GUT's "Add method calls this.) addicourse Returns a copy of the object, following the general contract of clone from the Java API Specification. In particular, it should be a deep and independent copy, such that any subsequent changes to the state of the clone will not affect the original and vice versa. clone ( Returns the course, if any, in this schedule that takes place on the given weekday at the given time. Since courses cannot conflict, there is at most one such course. If no course in this schedule takes place at that time, returns nul1. (The GUI calls this method for each day at 30-minute intervals to know what to show in the table on the screen. The course must occupy that exact minute in time to be shown.) getCourse (day, time) Removes the course, if any, in this schedule that takes place on the given weekday at the given time. Since courses cannot conflict, there is at most one such course. If no course in this schedule takes place at that time, there is no effect. This method should run in no worse than O(n) time where n is the schedule size. (The GUT's remove (day, time) method calls this, Outputs the courses from this schedule to the given output file (represented as a PrintStream object) in the ordering represented by the given course comparator (a class that implements the Comparator interface). The courses should be output one per line in a format consistent with the toString behavior of each course. If done properly. the output format will match that of the original courses.txt file. See the descriptions of the three course comparator classes later in this document. This method should run in no worse than Oin log n) time where n is the schedule size. (The GUIs Save" button calls this with various comparators depending on the user's choice comparator) Returns the total number of credits for which the student is enrolled. For example, if the student is enrolled in a 4-credit class, a 3-credit class, and a 5-credit class, this method would return 12. If the student is not enrolled in any courses, returns 0. This method should run in no worse than O(n) time where n is the schedule size. (The GUI calls this in order to display the total credits near the bottom of the window. totalCredits() In addition to above methods, implement below method for test-only purpose. (Below method should be used by JUnit tests only.) public List getAllCourses I/Returns all courses in the scheduler Schedule class: A Schedule object stores information about the collection of courses in which a student is enrolled. It should have the following public behavior. Unless otherwise specified, every method of class Schedule should run in O(n) time where n is the number of courses in the schedule. Use Java Collection's List to manage the courses. The Schedule class should implement the Cloneable interface so that client code will be able to use its clone method. Note that this document does not specify what internal collection or courses in the order indicated by the comparator passed to the save method. ng to use for in a schedule. But when saving a schedule, you save the Method Description Schedule() Constructor that creates a new empty schedule. Should run in O1) time. Adds the given course to this schedule. If the given course conflicts with any of the courses in the schedule as defined previously, a ScheduleConflictException should be thrown. (The GUT's "Add method calls this.) addicourse Returns a copy of the object, following the general contract of clone from the Java API Specification. In particular, it should be a deep and independent copy, such that any subsequent changes to the state of the clone will not affect the original and vice versa. clone ( Returns the course, if any, in this schedule that takes place on the given weekday at the given time. Since courses cannot conflict, there is at most one such course. If no course in this schedule takes place at that time, returns nul1. (The GUI calls this method for each day at 30-minute intervals to know what to show in the table on the screen. The course must occupy that exact minute in time to be shown.) getCourse (day, time) Removes the course, if any, in this schedule that takes place on the given weekday at the given time. Since courses cannot conflict, there is at most one such course. If no course in this schedule takes place at that time, there is no effect. This method should run in no worse than O(n) time where n is the schedule size. (The GUT's remove (day, time) method calls this, Outputs the courses from this schedule to the given output file (represented as a PrintStream object) in the ordering represented by the given course comparator (a class that implements the Comparator interface). The courses should be output one per line in a format consistent with the toString behavior of each course. If done properly. the output format will match that of the original courses.txt file. See the descriptions of the three course comparator classes later in this document. This method should run in no worse than Oin log n) time where n is the schedule size. (The GUIs Save" button calls this with various comparators depending on the user's choice comparator) Returns the total number of credits for which the student is enrolled. For example, if the student is enrolled in a 4-credit class, a 3-credit class, and a 5-credit class, this method would return 12. If the student is not enrolled in any courses, returns 0. This method should run in no worse than O(n) time where n is the schedule size. (The GUI calls this in order to display the total credits near the bottom of the window. totalCredits() In addition to above methods, implement below method for test-only purpose. (Below method should be used by JUnit tests only.) public List getAllCourses I/Returns all courses in the scheduler

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions