I need help creating this class with the classes that I alre
I need help creating this class with the classes that I already have. here are the classes that I have:
Time Class:
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.sun.org.apache.xpath.internal.SourceTree;
import javafx.beans.binding.ObjectExpression;
import java.util.ArrayList;
import java.util.List;
import static java.util.Calendar.PM;
public class Time implements Cloneable, Comparable<Time> {
private int hour;
private int minute;
private boolean amPm;
public Time(int h, int m, boolean p) {
//hour 1-12
if (h > 12 || h < 1)
throw new IllegalArgumentException(\"hour should be 1-12\");
//minute 0-59
if (m > 59 || m < 0)
throw new IllegalArgumentException(\"minute should be 0-59\");
this.hour = h;
this.minute = m;
this.amPm = p;
}
//gets hour
public int getHour() {
return hour;
}
//gets minute
public int getMinute() {
return minute;
}
//get true or false for AM || PM
public boolean isPM() {
return amPm;
}
@Override
public boolean equals(Object obj) {
if (obj != null && getClass() == obj.getClass()){
Time o = (Time) obj;
return hour == o.hour && minute == o.minute && amPm== o.amPm;
}
return false;
}
@Override
public int hashCode(){
int result = 17;
result = 31 * result + (amPm ? 1:0);
result = 31 * result + minute;
result = 31 * result + hour;
return result;
}
//HH:MM AP/PM format String
@Override
public String toString() {
return String.format(\"%02d\", hour) + \":\" + String.format(\"%02d\", minute) + \" \" + (amPm ? \"PM\" : \"AM\");
}
private static void checkFormat(String str){
//check format of str
if(str.length() != 8){
throw new IllegalArgumentException(\"Length has to be 8\");
}
}
public static Time fromString(String str) {
if (str.length() != 8) {
throw new IllegalArgumentException(\"invalid\");
} else if (str.charAt(2) != \':\') {
throw new IllegalArgumentException(\"No colon\");
} else if (str.charAt(5) != \' \') {
throw new IllegalArgumentException(\"no space\");
} else if (!str.substring(6, 8).equals(\"AM\") && !str.substring(6, 8).equals(\"PM\")) {
throw new IllegalArgumentException(\"AM or PM\");
} else {
int minute;
int hour;
String amorPm = str.substring(6);
try{
hour = Integer.parseInt(str.substring(0,2));
minute = Integer.parseInt(str.substring(3,5));
}catch (NumberFormatException nfe){
throw new IllegalArgumentException(\"Invalid Type\");
}
return new Time(hour, minute, amorPm.equals(\"PM\"));
}
}
public void shift(int m){
if (m<0) {
throw new IllegalArgumentException(\"minute can not be negative\");
}else {
//1) update minutes
minute += m;
int hoursToAdd = minute/60 % 24;
minute %= 60;
if (hoursToAdd == 0) return;
//2) update am/pm
int adjustedHour = hour;
if (adjustedHour == 12) adjustedHour = 0;
adjustedHour += hoursToAdd;
int numFlip = adjustedHour/12;
if(numFlip %2 == 1) amPm = !amPm;
//3) update hours
hour += hoursToAdd; //12 AM + 2 hours
hour %= 12;
if(hour == 0) hour = 12;
}
}
@Override
public Time clone(){
try {
Time copy = (Time) super.clone();
return copy;
} catch (CloneNotSupportedException e){
return null;
}
}
private int toMinutes(){
//convert the current object into minute
//12:00 AM - 0 minute
//12:01 AM - 1 minute
int newMinute;
if(hour == 12 && isPM() == false) {
return minute;
}
else if (hour == 12 && isPM() == true){
return minute + (60*12);
}
else if (isPM() == false){
newMinute = (60 * hour) + minute;
return newMinute;
}else{
newMinute = (60 * hour + minute) + (60 * 12);
return newMinute;
}
}
@Override
public int compareTo(Time other) {
return toMinutes() - other.toMinutes();
}
}
_________________________________________
Course Class:
public class Course {
private String name;
private int credits, classDuration;
private Set<Weekday> days;
private Time classStart;
public Course(String name, int credits, Set<Weekday> days, Time classStart, int classDuration) {
if(days == null || (credits > 5 || credits < 1) || (name == \"\" || name == null) || !name.contains(\" \") || (classStart == null) || (classDuration <=0)){
throw new IllegalArgumentException( \"Format is invalid\");
}
this.name = name;
this.credits = credits;
this.days = EnumSet.copyOf(days); //this.days = EnumSet
this.classStart = classStart.clone(); //this.classStart = classStart.Timeclone()
this.classDuration = classDuration;
}
public boolean conflictsWith(Course other) {
for (Weekday d : other.days) {
if (days.contains(d)) {
if (contains(d, other.getStartTime()) || other.contains(d, getStartTime())) {
return true;
}
}
}
return false;
}
public boolean contains (Weekday d, Time start) {
if (days.contains(d)) {
if (getStartTime().equals(start)) {
return true;
} else if (getEndTime().equals(start))
return false;
else
return getStartTime().compareTo(start) < 0 && getEndTime().compareTo(start) > 0;
}
else
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Course course = (Course) o;
if (credits != course.credits) return false;
if (classDuration != course.classDuration) return false;
if (name != null ? !name.equals(course.name) : course.name != null) return false;
if (days != null ? !days.equals(course.days) : course.days != null) return false;
return classStart != null ? classStart.equals(course.classStart) : course.classStart == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + credits;
result = 31 * result + classDuration;
result = 31 * result + (days != null ? days.hashCode() : 0);
result = 31 * result + (classStart != null ? classStart.hashCode() : 0);
return result;
}
public int getCredits(){
return credits;
}
public int getDuration(){
return classDuration;
}
public Time getStartTime(){
return classStart.clone();
}
public String getCourseName(){
return name;
}
public Time getEndTime(){
Time endTime = classStart.clone();
endTime.shift(classDuration);
return endTime;
}
@Override
public String toString() {
return name + \", \" + credits + \", \" + days + \", \" + classStart + \", \" +classDuration;
}
public String getName() {
return name;
}
}
________________________________________
weekdayEnumClass:
public enum Weekday {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY;
@Override
public String toString() {
String orgStr = super.toString();
return orgStr.substring(0,1) + orgStr.substring(1).toLowerCase();
}
public String toShortName(){
if (this == THURSDAY){
return \"R\";
}
return super.toString().substring(0,1);
}
public static Weekday fromString(String str){
if(str == null || str.length() < 1) {
throw new IllegalArgumentException(\"It is not of correct format\");
}
if(str.equalsIgnoreCase(\"Monday\") || str.equalsIgnoreCase(\"M\")){
return MONDAY;
} else if (str.equalsIgnoreCase(\"Tuesday\") || str.equalsIgnoreCase(\"T\")){
return TUESDAY;
}else if (str.equalsIgnoreCase(\"Wednesday\") || str.equalsIgnoreCase(\"W\")){
return WEDNESDAY;
}else if (str.equalsIgnoreCase(\"Thursday\") || str.equalsIgnoreCase(\"R\")){
return THURSDAY;
}else if (str.equalsIgnoreCase(\"Friday\") || str.equalsIgnoreCase(\"F\")) {
return FRIDAY;
} else {
throw new IllegalArgumentException();
}
}
}
_______________________________________
I need help creating this:
Schedule() method : Constructor that creates a new empty schedule. Should run in O(1) time.
add(course) method : 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 GUI\'s \"Add\" method calls this.)
clone() method: 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.
getCourse(day, time) method : 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 null.
(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
remove(day,time) method
save(printstream, comparator) method
totalcredits() method
Solution
Firstly create a class TimeTask that run your task, it looks like:
Then in main class you instantiate the task and run it periodically started by a specified date:








