Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I need help defining a class.

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help defining a class.

    We are working on a group project and each person is given a class that they are to define. Here are the directions:

    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
    Here is my code so far:

    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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,654
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: I need help defining a class.

    The easiest way, in my opinion, would be to utilize the contains(Time) method which you have already created.
    Consider the fact that this.TimeRange would contain otherTime if any of these conditions are met:
    1. this.TimeRange contains otherTime's startTime
    2. this.TimeRange contains otherTime's endTime
    3. otherTime contains this.TimeRange's startTime
    4. otherTime contains this.TimeRange's endTime
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help defining a class.

    I don't know if my reply posted. How do I say, in java, otherTime's startTime and so on.
    like:
    if (this.TimeRange.contains(------){
    return true;
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,654
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: I need help defining a class.

    You don't need this.TimeRange. I was just using that convention to indicate the current instance of the TimeRange object invoking the overlaps() method, opposed to the TimeRange object passed in the parameters. To call the contains() method from the overlaps() method, you just need to say: if(contains(------))
    You would use your getters.
    otherTime.getStartTime() would get otherTime's start time, and otherTime.getEndTime() would get otherTime's end time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help defining a class.

    So like this?
    if (contains(otherTime.getStartTime())){
    return true;
    }

    --- Update ---

    if (contains(otherTime.getStartTime())){
                return true;
            }
            else if (contains(otherTime.getEndTime())){
                return true;
            }
            else if (otherTime.contains(startTime)){
                return true;
            }
            else if (otherTime.contains(endTime)){
                return true;
            }
            else{
                return false;
            }

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,654
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: I need help defining a class.

    Looks like it. Write a small test program and try it out.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help defining a class.

    Thank you, I appreciate it.

Similar Threads

  1. defining a constructor with a scanner
    By bombert in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 07:55 AM
  2. help with defining a changing variable within a loop
    By uswhovian in forum Loops & Control Statements
    Replies: 3
    Last Post: March 9th, 2013, 09:30 AM
  3. help with defining a method
    By mrjavajava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2013, 06:06 AM
  4. Defining a rational class. Simple but i cant figure it out to save my life
    By spetillo3 in forum Object Oriented Programming
    Replies: 3
    Last Post: October 26th, 2011, 11:02 PM
  5. [SOLVED] What do I need to do to use a bean in Netbeans 7 apart from defining it?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 31st, 2011, 12:29 AM