Question

QUESTION: Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time...

QUESTION:

Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time spans by their length. A time span that represents a shorter amount of time is considered to be “less than” one that represents a longer amount of time. For example, a span of 3 hours and 15 minutes is greater than a span of 1 hour and 40 minutes.

GIVEN TIMESPAN FILE

/*
* Adapted for CS211 from Building Java Programs, 4th Edition,
* by Stuart Reges and Marty Stepp
* adapted by James Livingston, Bellevue College Adjunct Instructor
*/
// Represents a time span of elapsed hours and minutes.
// Simple implementation using only total minutes as state.
public class TimeSpan {
private int totalMinutes;

// Constructs a time span with the given interval.
// pre: hours >= 0 && minutes >= 0
public TimeSpan(int hours, int minutes) {
totalMinutes = 0;
add(hours, minutes);
}

// Adds the given interval to this time span.
// pre: hours >= 0 && minutes >= 0
public void add(int hours, int minutes) {
totalMinutes += 60 * hours + minutes;
}

// Returns a String for this time span such as "6h15m".
public String toString() {
return (totalMinutes / 60) + "h"
+ (totalMinutes % 60) + "m";
}
}

GIVEN TIMESPANMAIN FILE

/*
* TimeSpanClient: a simple test client for the TimeSpan class
* Shows creation of an instance object, displaying that object,
* adding hours and minutes to that object, and showing the result.
*/
public class TimeSpanClient {
public static void main(String[] args) {
int h1 = 13, m1 = 30;
TimeSpan t1 = new TimeSpan(h1, m1);
System.out.println("New object t1: " + t1);
  
h1 = 3; m1 = 40;
System.out.println("Adding " + h1 + " hours, " + m1 + " minutes to t1");
t1.add(h1, m1);
System.out.println("New t1 state: " + t1);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the modified code for TimeSpan.java and TimeSpanClient.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// TimeSpan.java

//implemented Comparable interface, which has the compareTo method

//and can be used to sort a list or array of TimeSpan objects without

//using a Comparator

public class TimeSpan implements Comparable<TimeSpan> {

    private int totalMinutes;

    // Constructs a time span with the given interval.

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

    public TimeSpan(int hours, int minutes) {

         totalMinutes = 0;

         add(hours, minutes);

    }

    // Adds the given interval to this time span.

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

    public void add(int hours, int minutes) {

         totalMinutes += 60 * hours + minutes;

    }

    // Returns a String for this time span such as "6h15m".

    public String toString() {

         return (totalMinutes / 60) + "h" + (totalMinutes % 60) + "m";

    }

    // method to compare this time span with other

    // returns a negative value if this time span is shorter than other

    // returns 0 if both have same duration

    // returns a positive value if this time span is longer than other

    public int compareTo(TimeSpan other) {

         if (this.totalMinutes < other.totalMinutes) {

             return -1; // this < other

         } else if (this.totalMinutes > other.totalMinutes) {

             return 1; // this > other

         } else {

             return 0; // this = other

         }

    }

}

// TimeSpanClient.java

public class TimeSpanClient {

    public static void main(String[] args) {

         int h1 = 13, m1 = 30;

         TimeSpan t1 = new TimeSpan(h1, m1);

         System.out.println("New object t1: " + t1);

         h1 = 3;

         m1 = 40;

         System.out.println("Adding " + h1 + " hours, " + m1 + " minutes to t1");

         t1.add(h1, m1);

         System.out.println("New t1 state: " + t1);

         // creating another TimeSpan object, testing compareTo method using the

         // two objects

         TimeSpan t2 = new TimeSpan(10, 20);

         System.out.println("New object t2: " + t2);

         System.out.println("t1.compareTo(t2): " + t1.compareTo(t2));

         System.out.println("t2.compareTo(t1): " + t2.compareTo(t1));

         System.out.println("t1.compareTo(t1): " + t1.compareTo(t1));

    }

}

/*OUTPUT*/

New object t1: 13h30m

Adding 3 hours, 40 minutes to t1

New t1 state: 17h10m

New object t2: 10h20m

t1.compareTo(t2): 1

t2.compareTo(t1): -1

t1.compareTo(t1): 0

Add a comment
Know the answer?
Add Answer to:
QUESTION: Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is the code of TimeSpan.java // Represents a time span of hours and minutes elapsed....

    Here is the code of TimeSpan.java // Represents a time span of hours and minutes elapsed. // Alternate implementation using only total minutes. public class TimeSpan { private int totalMinutes; // Constructs a time span with the given interval. // pre: hours >= 0 && minutes >= 0 public TimeSpan(int hours, int minutes) { totalMinutes = 0; add(hours, minutes); } // Adds the given interval to this time span. // pre: hours >= 0 && minutes >= 0 public void...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...

    Define a toString method in Post and override it in EventPost. EventPost Class: /** * This class stores information about a post in a social network news feed. * The main part of the post consists of events. * Other data, such as author and type of event, are also stored. * * @author Matthieu Bourbeau * @version (1.0) March 12, 2020 */ public class EventPost extends Post { // instance variables - replace the example below with your own...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • 1. Given the following code, how many references exist to the Time object at the end...

    1. Given the following code, how many references exist to the Time object at the end of the main method and what are their names? public class Foo { public static void main(String args[]) { Time t = new Time(); Time t1 = t; Time t2 = t; Clock clock = new Clock(t); // AT THIS POINT, HOW MANY REFERENCES TO THE TIME OBJECT ARE THERE } } public class Time { } public class Clock { private Time time;...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from...

    Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your Assignment 1 Time.java, or download the solution, Time.java. This is Assignment 1: --------------------------- public class Time { private int h; private int m; public Time() { super(); } public Time(int h, int m){ if(h>=1 && h<=23){ this.h = h; }else{ this.h = 0; } if(m>=0 && m<=59){ this.m = m; }else{...

  • package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel...

    package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel label1;     public int minutes=3;//initial time     public int time=minutes*60;//total amount of time in seconds only     public int seconds=time%60;     public String defaulttimeset="0"+Integer.toString(minutes)+":0"+Integer.toString(seconds);     public static void main(String[] args) {         Timing move=new Timing();         move.go();     }     public void go(){         JFrame frame=new JFrame();         frame.setBounds(50,50,400,400);         JPanel panel2=new JPanel();         button1=new JButton("Start Time");         panel2.add(button1);                 button1.setBounds(30,50,150,170);         frame.getContentPane().add(button1);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT