Question: Java Exercise Help! Any feedback is greatly appreciated 1. Create a toString() method for the following code. // Represents a time span of hours and

Java Exercise Help!

Any feedback is greatly appreciated

1. Create a toString() method for the following code.

// Represents a time span of hours and minutes elapsed.

// Class invariant: minutes < 60

public class TimeSpan {

private int hours;

private int minutes;

// Constructs a time span with the given interval.

// pre: hours >= 0 && minutes >= 0

public TimeSpan(int hours, int minutes) {

this.hours = 0;

this.minutes = 0;

add(hours, minutes);

}

// Adds the given interval to this time span.

// pre: hours >= 0 && minutes >= 0

public void add(int hours, int minutes) {

this.hours += hours;

this.minutes += minutes;

// convert each 60 minutes into one hour

this.hours += this.minutes / 60;

this.minutes = this.minutes % 60;

}

}

2. Write a Java client code that implements the following:

  • Create two TimeSpan objects with any hour and minute value
  • Print out current time
  • Add some hours and minutes to two TimeSpan objects
  • Print out current time again

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!