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
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