Question

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 add(int hours, int minutes) {
totalMinutes += 60 * hours + minutes;
}

// Returns whether o is a TimeSpan representing the same
// number of hours and minutes as this TimeSpan object.
public boolean equals(Object o) {
if (o instanceof TimeSpan) {
TimeSpan other = (TimeSpan) o;
return totalMinutes == other.totalMinutes;
} else { // not a TimeSpan object
return false;
}
}

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

------------------------------------------------------------------

The Question is:

Add the following mutator method to the TimeSpan class:

public void add(TimeSpan span)

Adds the given amount of time to this time span.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// 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 add(int hours, int minutes) {
        totalMinutes += 60 * hours + minutes;
    }

    // Returns whether o is a TimeSpan representing the same
    // number of hours and minutes as this TimeSpan object.
    public boolean equals(Object o) {
        if (o instanceof TimeSpan) {
            TimeSpan other = (TimeSpan) o;
            return totalMinutes == other.totalMinutes;
        } else { // not a TimeSpan object
            return false;
        }
    }

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

    // Adds the given amount of time to this time span.
    public void add(TimeSpan span) {
        // add totalMinutes of span to the current object
        totalMinutes += span.totalMinutes;
    }
}
Add a comment
Know the answer?
Add Answer to:
Here is the code of TimeSpan.java // Represents a time span of hours and minutes elapsed....
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
  • 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, *...

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

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • [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...

  • 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{...

  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

  • I need the following code to keep the current output but also include somthing similar to...

    I need the following code to keep the current output but also include somthing similar to this but with the correct details from the code. Truck Details: Skoda 100 Nathan Roy 150.5 3200 Details of Vehicle 1: Honda, 5 cd, owned by Peter England Details of Vehicle 2: Skoda, 100 cd, owned by Nathan Roy, 150.5 lbs load, 3200 tow --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- class Person {     private String name;        public Person()     {        name="None";     }       ...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

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

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