answer.
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]
2 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]2 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
As a twist on the Hello World exercise, you are going to be the end user of the Hello class. This class is designed to greet the
Flura [38]

Answer:

Here is the Hello class:

public class Hello { //class name

     private String name; //to store the name

     

      public Hello (String names) //parameterized constructor

      { name = names;  }  

     

      public void English() { //method to greet user in english

           System.out.print("Hello "); //displays Hello on output screen

           System.out.print(name); //displays user name

           System.out.println("!");  }  //displays exclamation mark symbol

           

           public void Spanish(){ //method to greet user in spanish

               System.out.print("Hola "); //displays Hello on output screen

               System.out.print(name); //displays user name

               System.out.println("!"); } //displays exclamation mark symbol

           

         public void French() { //method to greet user in french

              System.out.print("Bonjour "); //displays Hello on output screen

              System.out.print(name);  //displays user name

              System.out.println("!");  } } //displays exclamation mark symbol

           

Explanation:

Here is the HelloTester class:

import java.util.Scanner; //to accept input from user

public class HelloTester { //class name

 public static void main (String[]args)  { //start of main method

String name; //to store the name of user

Scanner input = new Scanner(System.in);  //creates Scanner class object

System.out.println("Enter name?" );  //prompts user to enter name

 name = input.nextLine(); //scans and reads the name from user

 Hello hello = new Hello(name); //creates Hello class object and calls constructor by passing name

hello.English(); //calls English method using object hello to greet in english

hello.Spanish(); //calls Spanish method using object hello to greet in spanish

hello.French(); } }  //calls French method using object hello to greet in french

The output of the program is:

Enter name?                                                                                                                    user                                                                                                                           Hello user!                                                                                                                    Hola user!                                                                                                                    Bonjour user!  

The screenshot of the program along with its output is attached.

4 0
2 years ago
Design two subclasses of Employee…SalariedEmployee and HourlyEmployee. A salaried employee has an annual salary attribute. An ho
avanturin [10]

Answer:

Complete answer to above question is given below.

Explanation:

Employee.java

import java.util.Date;

public class Employee

{

private int empID;

private Address address;

private Name name;

private Date date;

public Employee()

{

}

public Employee(int empID)

{

this.empID = empID;

}

public int getEmpID()

{

return empID;

}

public void setEmpID(int empID)

{

this.empID = empID;

}

public Address getAddress()

{

return address;

}

public void setAddress(Address address)

{

this.address = address;

}

public Name getName()

{

return name;

}

public void setName(Name name)

{

this.name = name;

}

public Date getDate()

{

return date;

}

public void setDate(Date date)

{

this.date = date;

}

}

Name.java

public class Name {

private String name;

public Name(String name) {

super();

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

Address.java

public class Address {

private String addr;

public Address(String addr) {

super();

this.addr = addr;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

}

SalariedEmployee.java

public class SalariedEmployee extends Employee {

private double annualSalary;

public SalariedEmployee(double annualSalary) {

super();

this.annualSalary = annualSalary;

}

public double getAnnualSalary() {

return annualSalary;

}

public void setAnnualSalary(double annualSalary) {

this.annualSalary = annualSalary;

}

}

HourlyEmployee.java

public class HourlyEmployee extends Employee {

private double hourlyrate;

private double hours_worked;

private double earnings;

public HourlyEmployee(double hourlyrate, double hours_worked) {

super();

this.hourlyrate = hourlyrate;

this.hours_worked = hours_worked;

}

public double getEarnings()

{

if(hours_worked<=40)

{

earnings=hours_worked*hourlyrate;

}

else if(hours_worked>40)

{

earnings=40*hourlyrate+(hours_worked-40)*hourlyrate*1.5;

}

return earnings;

}

public double getHourlyrate() {

return hourlyrate;

}

public void setHourlyrate(double hourlyrate) {

this.hourlyrate = hourlyrate;

}

public double getHours_worked() {

return hours_worked;

}

public void setHours_worked(double hours_worked) {

this.hours_worked = hours_worked;

}

public void setEarnings(double earnings) {

this.earnings = earnings;

}

Test.java

import java.util.Date;

public class Test {

public static void main(String[] args) {

System.out.println("_____Salaried Employee_____");

SalariedEmployee se=new SalariedEmployee(50000);

se.setEmpID(1234);

se.setName(new Name("Williams"));

se.setAddress(new Address("4,Richmond Street"));

se.setDate(new Date("Oct-11-2014"));

System.out.println("Employee Id :"+se.getEmpID());

System.out.println("Employee Name :"+se.getName().getName());

System.out.println("Employee Address :"+se.getAddress().getAddr());

System.out.println("Joining Date :"+se.getDate());

System.out.println("The Annual Salary :"+se.getAnnualSalary());

System.out.println("\n_____Hourly Employee_____");

HourlyEmployee he1=new HourlyEmployee(8.5,39);

he1.setEmpID(4567);

he1.setName(new Name("Kane"));

he1.setAddress(new Address("5,lake View Road"));

he1.setDate(new Date("Nov-12-2015"));

System.out.println("Employee Id :"+he1.getEmpID());

System.out.println("Employee Name :"+he1.getName().getName());

System.out.println("Employee Address :"+he1.getAddress().getAddr());

System.out.println("Joining Date :"+he1.getDate());

System.out.println("The Earnings Of An HourlyEmployee :"+he1.getEarnings());

System.out.println("\n_____Hourly Employee_____");

HourlyEmployee he2=new HourlyEmployee(9.5,47);

he2.setEmpID(1111);

he2.setName(new Name("John"));

he2.setAddress(new Address("17,Villey Parley Street"));

he2.setDate(new Date("Dec-13-2011"));

System.out.println("Employee Id :"+he2.getEmpID());

System.out.println("Employee Name :"+he2.getName().getName());

System.out.println("Employee Address :"+he2.getAddress().getAddr());

System.out.println("Joining Date :"+he2.getDate());

System.out.println("The Earnings Of An HourlyEmployee :"+he2.getEarnings());

}

}

Output:

_____Salaried Employee_____

Employee Id :1234

Employee Name :Williams

Employee Address :4,Richmond Street

Joining Date :Sat Oct 11 00:00:00 IST 2014

The Annual Salary :50000.0

_____Hourly Employee_____

Employee Id :4567

Employee Name :Kane

Employee Address :5,lake View Road

Joining Date :Thu Nov 12 00:00:00 IST 2015

The Earnings Of An HourlyEmployee :331.5

_____Hourly Employee_____

Employee Id :1111

Employee Name :John

Employee Address :17,Villey Parley Street

Joining Date :Tue Dec 13 00:00:00 IST 2011

The Earnings Of An HourlyEmployee :479.75

7 0
2 years ago
Java Programming home &gt; 1.14: zylab training: Interleaved input/output Н zyBooks catalog Try submitting it for grading (click
Andru [333]

Answer:

The following are the code to this question:

code:

System.out.println(x);  //use print method to print value.

Explanation:

In the given question, it simplifies or deletes code in the 11th line. This line has a large major statement.  

In the case, the tests fail because only 1 line of output is required by the tester, but two lines are obtained instead.

It's to demonstrate that the input/output or testing requirements function throughout the model.

5 0
2 years ago
Give a recursive algorithm to compute the sum of the cubes of the first n positive integers. The input to the algorithm is a pos
DaniilM [7]

Answer:

def sum_cubes(n):

   if n == 1:

       return 1

   else:

       return n * n * n + sum_cubes(n-1)

print(sum_cubes(3))

Explanation:

Create a function called sum_cubes that takes one parameter, n

If n is equal to 1, return 1. Otherwise, calculate the cube of n, and add it to the sum_cubes function with parameter n-1

If we want to calculate the cubes of the first 3 numbers:

sum_cubes(3) = 3*3*3 + sum_cubes(2)

sum_cubes(2) = 2*2*2 + sum_cubes(1)

sum_cubes(1) = 1

If you substitute the values from the bottom, you get 27+8+1 = 36

7 0
2 years ago
Pressing and holding _______ while clicking enables you to select multiple contiguous files or folders.
liq [111]

Answer:

Hi LizBiz! The answer is Ctrl key on Windows, or Command key for Mac.

Explanation:

The Ctrl (Windows) key or equivalent Command key on Mac has a special purpose which allows special operations to be performed when combined with another action, such as clicking on multiple pictures or files for selection.

8 0
2 years ago
Other questions:
  • Suppose you define a java class as follows: public class test { } in order to compile this program, the source code should be st
    14·1 answer
  • Why is it important for a Support Agent to understand and follow their company’s standardized case lifecycle roadmap? (Select 2)
    12·1 answer
  • Identify the normalized form of the mantissa in 111.01.
    14·1 answer
  • The function below takes two arguments, a dictionary called dog_dictionary and a list of dog names (strings) adopted_dog_names.
    8·1 answer
  • Database management systems are expected to handle binary relationships but not unary and ternary relationships.'
    7·1 answer
  • Assume there is a class AirConditioner that supports the following behaviors: turning the air conditioner on and off. The follow
    7·1 answer
  • The attack in which the attacker sends a forged packet with the same source IP address and destination IP address in which the v
    10·1 answer
  • You need to design a data storage scheme for Hayseed Heaven library data system. There are several hundred thousand large data r
    8·1 answer
  • Is cheque money? give reason​
    9·1 answer
  • Write a class named Employee that has private data members for an employee's name, ID_number, salary, and email_address. It shou
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!