Question
(Intro to Java Help?) Define a class named TimeSpan. A TimeSpan object stores a span of time in hours and minutes (for example, the time
(Intro to Java Help?)
Define a class named TimeSpan. A TimeSpan object stores a span of time in hours and minutes (for example, the time span between Monday 8AM and Wednesday 10:30AM is 50 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 minutes and seconds.
add(hours, minutes) Adds the given amount of time to the current time span (e.g. 2 hours, 15 min + 1 hour, 45 min = 4 hours). Assume that the arguments 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. For example, consider the following code:
TimeSpan t1 = new TimeSpan(3, 45);
System.out.println(t1 + " is " + t1.getTotalHours() + " hours");
t1.add(2, 30);
System.out.println(t1 + " is " + t1.getTotalHours() + " hours");
TimeSpan t2 = new TimeSpan(0, 55);
t1.add(t2);
System.out.println(t1 + " is " + t1.getTotalHours() + " hours");
This code creates a TimeSpan object and adds three different times to it. The output should be:
3h45m is 3.75 hours
6h15m is 6.25 hours
7h10m is 7.166666666666667 hours
Notice that the second time is not 5 hours, 75 minutes, although that's what you'd get by just adding values.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started