Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

2. How will the team select a leader?

Answered: 1 week ago