Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class named TimeSpan. A TimeSpan object stores a span of time in hours

ID: 3723840 • Letter: D

Question

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 method:s new 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(O 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 Java solution code here 4 This is a class problem. Submit a complete Java class as described. | 4 | Indent Sound F/X Highlighting Submit

Explanation / Answer

Hi.. I have written the java code for the above class.

TimeSpan.java

public class TimeSpan {

int hours;

int minutes;

public TimeSpan(int hours, int minutes) {

this.hours = hours;

if(minutes>0 && minutes<60){

this.minutes = minutes;

}else{

this.minutes = 0;

}

}

public int getHours() {

return hours;

}

public int getMinutes() {

return minutes;

}

public void add(int hours,int minutes){

if(minutes<0 && minutes>60){

minutes = 0;

}

this.minutes+=minutes;

if(this.minutes>59){

this.hours++;

this.minutes = this.minutes - 60;

this.hours+=hours;

}else{

this.hours+=hours;

}

}

public void add(TimeSpan ts){

int hr = ts.getHours();

int mn = ts.getMinutes();

if(mn<0 && mn>60){

mn = 0;

}

this.minutes+=mn;

if(this.minutes>59){

this.hours++;

this.minutes = this.minutes - 60;

this.hours+=hr;

}else{

this.hours+=hr;

}

}

public double getTotalHours(){

String realTime = String.valueOf(hours);

int tr = (minutes/15)*25;

realTime+="."+String.valueOf(tr);

return Double.parseDouble(realTime);

}

@Override

public String toString() {

return hours + "h" + minutes + "m";

}

}

Please test the code and let me know any issues. Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote