Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

TimeSpan

Author: Marty Stepp

Define a class named TimeSpan. A TimeSpan object stores a span of time in hours and minutes (for example, the time span between 8:00am and 10:30am is 2 hours, 30 minutes). Each TimeSpan object should have the following public methods:

  • TimeSpan(hours, minutes)
    Constructs a TimeSpan object storing the given time span of hours and minutes.
  • getHours()
    Returns the number of hours in this time span.
  • getMinutes()
    Returns the number of minutes in this time span, between 0 and 59.
  • add(hours, minutes)
    Adds the given amount of time to the span. For example, (2 hours, 15 min) + (1 hour, 45 min) = (4 hours). Assume that the parameters are valid: the hours are non-negative, and the minutes are between 0 and 59.
  • add(timespan)
    Adds the given amount of time (stored as a time span) to the current time span.
  • getTotalHours()
    Returns the total time in this time span as the real number of hours, such as 9.75 for (9 hours, 45 min).
  • toString()
    Returns a string representation of the time span of hours and minutes, such as "28h46m".

The minutes should always be reported as being in the range of 0 to 59. That means that you may have to "carry" 60 minutes into a full hour.

Type your solution here:


This is a class problem. Submit a complete Java class as described.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.