i need help crating this class in java Course class: A Course object stores info
ID: 3777565 • Letter: I
Question
i need help crating this class in java
Course class:
A Course object stores information about a particular university course. It has the following public behavior (days is a String for ex:MWF or TW where M is monday T us tuesday etc.)
Method
Description
Course(name, credits, days,
startTime, duration)
Constructor that takes a course name (a string), number of credits (an integer), a Set of weekdays on which the course is offered, the time of day at which the course begins (a time), and its duration in minutes (an integer). In addition to the general argument checks, throw an IllegalArgumentException if the set of days is empty or if the credits are not between 1-5 inclusive. You may assume that the set of days is sorted in the natural ordering of weekdays; that no day in the set is null; and that no course wraps from one day to the next.
conflictsWith(course)
Returns true if this course is in session during any day(s) and time(s) that overlap with the given course. Even a single minute of overlap in the courses is considered to be a conflict. For example, if the two courses both take place on Mondays and one starts at 12:30 PM and last for 60 minutes duration and the other begins at 1:15 PM and lasts for 40 minutes duration, they conflict. This method should run in no worse than O(duration) time.
contains(day, time)
Returns true if this course is in session during the given time on the given day. Courses follow the common convention that their start times are inclusive but their ending times are exclusive. In other words, a 60-minute course beginning at 1:30 PM does contain the time of 1:30 PM and it does contain the time of 2:29 PM, but it does not contain 2:30 PM.
equals(o)
Returns true if and only if o refers to a Course object with exactly equivalent state as this one; otherwise returns false.
getCredits(), getDuration(),
getName(), getStartTime()
Accessors for the course's various state as passed to the constructor.
getEndTime()
Returns the non-inclusive end time for this course, which differs in time by exactly duration minutes from the course's start time. For example, if the course begins at 1:30 PM and lasts for 60 minutes, the end time to return is 2:30 PM. This method should run in no worse than O(duration) time.
toString()
Returns a string representation of this course. The string contains the course's name, credits, days, start time, and duration separated by commas. The days should be shown as a combined string of "short names". For example, if the course occurs on Monday, Wednesday, and Friday, its days should be represented as MWF. An example of the kind of overall string returned would be "CSE 142,4,MWF,09:30 AM,50". Note that this format also matches the expected format of courses in the input file courses.txt.
this is what i have so far:
Method
Description
Course(name, credits, days,
startTime, duration)
Constructor that takes a course name (a string), number of credits (an integer), a Set of weekdays on which the course is offered, the time of day at which the course begins (a time), and its duration in minutes (an integer). In addition to the general argument checks, throw an IllegalArgumentException if the set of days is empty or if the credits are not between 1-5 inclusive. You may assume that the set of days is sorted in the natural ordering of weekdays; that no day in the set is null; and that no course wraps from one day to the next.
conflictsWith(course)
Returns true if this course is in session during any day(s) and time(s) that overlap with the given course. Even a single minute of overlap in the courses is considered to be a conflict. For example, if the two courses both take place on Mondays and one starts at 12:30 PM and last for 60 minutes duration and the other begins at 1:15 PM and lasts for 40 minutes duration, they conflict. This method should run in no worse than O(duration) time.
contains(day, time)
Returns true if this course is in session during the given time on the given day. Courses follow the common convention that their start times are inclusive but their ending times are exclusive. In other words, a 60-minute course beginning at 1:30 PM does contain the time of 1:30 PM and it does contain the time of 2:29 PM, but it does not contain 2:30 PM.
equals(o)
Returns true if and only if o refers to a Course object with exactly equivalent state as this one; otherwise returns false.
getCredits(), getDuration(),
getName(), getStartTime()
Accessors for the course's various state as passed to the constructor.
getEndTime()
Returns the non-inclusive end time for this course, which differs in time by exactly duration minutes from the course's start time. For example, if the course begins at 1:30 PM and lasts for 60 minutes, the end time to return is 2:30 PM. This method should run in no worse than O(duration) time.
toString()
Returns a string representation of this course. The string contains the course's name, credits, days, start time, and duration separated by commas. The days should be shown as a combined string of "short names". For example, if the course occurs on Monday, Wednesday, and Friday, its days should be represented as MWF. An example of the kind of overall string returned would be "CSE 142,4,MWF,09:30 AM,50". Note that this format also matches the expected format of courses in the input file courses.txt.
Explanation / Answer
I had written the whole class as per your specifications:
import java.time.LocalTime;
import java.util.HashSet;
import java.util.Set;
public class Course {
private String courseName;
private Set<Character> weekDays; // a set used to store days
private int noOfCredits, classDuration;
private LocalTime startTime;
public Course()
{
weekDays=new HashSet<Character>(); //{'M','T','W'}
}
public Course(String courseName, Set<Character> 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>=1 && 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<Character> getWeekDays() {
return weekDays;
}
public void setWeekDays(Set<Character> 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().contains(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;
}
}
--------------------------------------------------------------------------------------
feel free to ask if you have any doubt :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.