We are working on a group project and each person is given a class that they are to define. Here are the directions:
Here is my code so far:TimeRange
This class represents a duration or range of time marked by a starting and ending time. This class does not allow for such a range of
time to extend to the following day. In other words, the start time must always come before the end time without considering the days.
So defining a range like 1900 to 0200 (7pm to 2am) would instead be interpreted as 0200 to 1900 (2am to 7pm).
Private Attributes
Time startTime
Time endTime
the starting and ending times for the range. The start time must always come before the end time.
Public Methods
TimeRange(Time startTime, Time endTime)
constructor that takes a start and end time and initializes the object. If the start time does not come before the end time, the constructor
switches them around to make the range meaningful.
Time getStartTime()
Time getEndTime()
returns the start and end times, respectively. Note that there are no equivalent setter methods here. This is because a setter method
might violate the “start comes before end” property, forcing the time range to rearrange itself to remain meaningful. Instead, any
changes must be done by instantiating a new TimeRange object.
int getDuration()
returns the duration in minutes of the range
boolean contains(Time time)
returns true if the current TimeRange object contains the time passed in. For instance, the time range (1000, 1400) contains 1030 (and
1000 and 1400 and all times in between) but does not contain 0800 or 1500 (or 1401, for that matter).
boolean overlaps(TimeRange otherTime)
returns true if the current TimeRange object overlaps the TimeRange object passed in. For instance, the time range(1000, 1400)
overlaps the ranges (1100, 1500), (1030, 1130), and (0900, 1430), but it does not overlap the range (0800, 0900).
String toString()
returns the string representation of the time range of the form “HHMM-HHMM” where the first time is the start and the second is the
end
public class TimeRange{ // Private Attributes. private Time startTime; private Time endTime; // Constructor that takes a start and end time and initializes the object. If the start time does not come before the end time, the constructor // switches them around to make the range meaningful. public TimeRange(Time startTime, Time endTime){ if (startTime > endTime){ Time temp = startTime; startTime = endTime; endTime = temp; } else{ startTime = startTime; endTime = endTime; } } // Returns the start and end times, respectively. Note that there are no equivalent setter methods here. This is because a setter method // might violate the “start comes before end” property, forcing the time range to rearrange itself to remain meaningful. Instead, any // changes must be done by instantiating a new TimeRange object. public Time getStartTime(){ return startTime; } public Time getEndTime(){ return endTime; } // Returns the duration in minutes of the range. public int getDuration(){ return (Math.abs(endTime - startTime) / 100) * 60; } // Returns true if the current TimeRange object contains the time passed in. For instance, the time range (1000, 1400) contains 1030 (and // 1000 and 1400 and all times in between) but does not contain 0800 or 1500 (or 1401, for that matter). boolean contains(Time time){ if (time <= endTime && time >= startTime){ return true; } else{ return false; } } // Returns true if the current TimeRange object overlaps the TimeRange object passed in. For instance, the time range(1000, 1400) // overlaps the ranges (1100, 1500), (1030, 1130), and (0900, 1430), but it does not overlap the range (0800, 0900). boolean overlaps(TimeRange otherTime){ } // Returns the string representation of the time range of the form “HHMM-HHMM” where the first time is the start and the second is the end. String toString(){ String timeRange = startTime + " - " + endTime; return timeRange; } }
I am having trouble figuring out how to implement the overlap method. If anyone could point me in the right direction, I would appreciate it.