1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
olya-2409 [2.1K]
3 years ago
10

Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time spans by their length. A time span tha

t 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 >= 0public TimeSpan(int hours, int minutes) {totalMinutes = 0;add(hours, minutes);}// Adds the given interval to this time span.// pre: hours >= 0 && minutes >= 0public 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);}}
Computers and Technology
1 answer:
My name is Ann [436]3 years ago
4 0

Answer:

Check the explanation

Explanation:

Here is the modified code for

TimeSpan.java and TimeSpanClient.java.

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

You might be interested in
The market for social-networking website services is characterized by network externalities because: Choose one:
ale4655 [162]

Answer:

Option C is the correct answer to the following question.

Explanation:

Because network externality is that in which a consequence of the industrial activities or the other commercial activities which are affects another parties without involving the reflected in cost of goods and services. The other folks used the website as compares to any other folks enjoys. So, that's why it is correct.

7 0
4 years ago
What did major networks do to combat audience erosion in the 1990s?
S_A_V [24]

Answer: I think is 3. They acquired cable channels. They acquired cable operators.

Explanation:

6 0
4 years ago
How do I get my text box to stop moving continuously on a Chromebook?
pickupchik [31]

Answer:

Go to Settings. Click on Advanced Settings. Scroll down to the Accessibility section. Make sure that the box next to “Automatically click when the mouse pointer stops” is Unchecked.

Explanation:

here you go hope this helps!!

5 0
3 years ago
Read 2 more answers
Computer programming(assembly language)​
Nikitich [7]

Konichiwa~. My name is Zalgo and I am here to be of assistance with your problem. The Computer Programming Assembly Language, which is sometimes referred as assembly or ASM is a low level programming language. Programs written in assembly languages are often compiled by an assembler. Every assembler has its own assembly language (meaning your assembly language could be the complete opposite of your friends/peers), which is designed for one specific computer architecture.

Hope this helps.

"Stay Brainly and stay proud!" - Zalgo

(By the way, do you mind marking me as Brainliest? I'd greatly appreciate it. Arigato~.)

6 0
4 years ago
Secure software development: a security programmer's guide by jason grembi isbn-10: 1-4180-6547-1, isb-13: 978-1-4180-6547
a_sh-v [17]

Secure software development is known to be a kind of methodology (often linked with DevSecOps) for making software that incorporates a lot of security into every stage of the software development life cycle (SDLC).

<h3>What is secure software development practices?</h3>

Secure software development is  seen as a method that is used form the creation of a lot of software that uses both security into all of the phase of the software development making cycle.

This book is known to be  A Programmer's Guide that can be able to  leads readers in all of the tasks and activities that one can do to be successful computer programmers and it is one that helps to navigate in the area of reading and analyzing requirements as well as choosing development tools, and others.

Hence, Secure software development is known to be a kind of methodology (often linked with DevSecOps) for making software that incorporates a lot of security into every stage of the software development life cycle (SDLC).

Learn more about  Secure software development  from

brainly.com/question/26135704

#SPJ1

7 0
2 years ago
Other questions:
  • If you can tell me what the best part about black ops 2 is and get it right you can have the Brainliest
    6·2 answers
  • 2. You have classes to represent different shapes (see below). You realize you can benefit from inheritance and polymorphism by
    13·1 answer
  • After which problem-solving stage should you take action?
    12·1 answer
  • Assume that someone dictates you a sequence of numbers and you need to write it down. For brevity, he dictates it as follows: fi
    14·1 answer
  • A stepping function allows a program
    11·2 answers
  • Which of the following is the answer?
    5·1 answer
  • Which computer network component connects two different networks together and allows them to communicate?
    8·2 answers
  • Q.drtrdyudoijoemrkdf
    6·2 answers
  • Brainliest
    7·1 answer
  • Add me as a friend on Real pool 3D so we can play a round of pool!!!
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!