Question

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{

this.m = 0;

}

}

public int getH() {

return h;

}

public void setH(int h) {

this.h = h;

}

public int getM() {

return m;

}

public void setM(int m) {

this.m = m;

}

public String convert(){

String standardTime = "";

String flag = "";

if(h>= 0 && h < 12){

flag = " AM";

if(h == 0){

standardTime = Integer.toString(12);

}

else{

standardTime = Integer.toString(h);

}

}else{

flag = " PM";

if(h == 12){

standardTime = Integer.toString(12);

}

else{

standardTime = Integer.toString(h - 12);

}   

}

standardTime += ":";

standardTime += Integer.toString(m).toCharArray().length == 1 ? "0"+Integer.toString(m):Integer.toString(m);

standardTime += flag;

return standardTime;

}

public void increment() {

int min = this.m;

min++;

if(min == 60){

this.m = 00;

this.h++;

if(this.h == 24){

this.h = 0;

}

}else{

this.m = min;

}

}

public String toString() {

String hour = Integer.toString(h).toCharArray().length == 1 ? "0" + Integer.toString(h) : Integer.toString(h);

String minutes = Integer.toString(m).toCharArray().length == 1 ? "0" + Integer.toString(m) : Integer.toString(m);

return hour + minutes ;

}

}

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

First, you will need to update Time so that it implements the Comparable interface. This will require adding an implements statement to the class declaration as well as the compareTo method. Then, you will need to add a difference method to the class. These two methods' requirements are as follows:

compareTo(Object other)
    //Returns -1 if current time is less than other.
    //Returns 0 if current time is equal to other.
    //Returns 1 if current time is greater than other.

String difference(Time t)
    //Returns a String holding the difference between the current time and
    //the Time t passed in via parameter. All values should be positive,
    //and in the format:
    //Time difference: 08:09
    //Time difference: 10:35

To test your code, download and run the runner class: student_runner_time.java. We will use a different, but similar, test class to grade the program. You will need to change the runner to test with other values to make sure your program fits all the requirements.

Sample Run of student_runner_time.java:

1712
0945
Greater than:
1
Less than:
-1
Times equal:
0
Hours equal:
1
-1
Difference
Time difference: 00:11
Time difference: 00:11
Time difference: 00:00

NOTE: You MUST use the class name "Time" for this assignment. REMEMBER: you must SUBMIT your answer. Your assignment doesn't count as complete unless it has been submitted.

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

Thanks for the question, here are the implementation of the compareTo() and difference() methods. It would have been good if you could have shared the  student_runner_time.java file so that I could have tested the code.

Nevertheless, please run the test code and if any changes is needed please do comment.

thank you

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

public class Time implements Comparable {

    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 {
            this.m = 0;
        }
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getM() {
        return m;
    }

    public void setM(int m) {
        this.m = m;
    }

    public String convert() {
        String standardTime = "";
        String flag = "";
        if (h >= 0 && h < 12) {
            flag = " AM";
            if (h == 0) {
                standardTime = Integer.toString(12);
            } else {
                standardTime = Integer.toString(h);
            }
        } else {
            flag = " PM";
            if (h == 12) {
                standardTime = Integer.toString(12);
            } else {
                standardTime = Integer.toString(h - 12);
            }
        }
        standardTime += ":";
        standardTime += Integer.toString(m).toCharArray().length == 1 ? "0" + Integer.toString(m) : Integer.toString(m);
        standardTime += flag;
        return standardTime;
    }

    public void increment() {
        int min = this.m;
        min++;
        if (min == 60) {
            this.m = 00;
           this.h++;
            if (this.h == 24) {
                this.h = 0;
            }
        } else {
            this.m = min;
        }
    }

    public String toString() {
        String hour = Integer.toString(h).toCharArray().length == 1 ? "0" + Integer.toString(h) : Integer.toString(h);
        String minutes = Integer.toString(m).toCharArray().length == 1 ? "0" + Integer.toString(m) : Integer.toString(m);
        return hour + minutes;
    }

    @Override
    public int compareTo(Object time) {
        if (time == null || !(time instanceof Time)) return -1;
        Time aTime = (Time) time;

        int result = (h * 60 + m) - (aTime.getH() * 60 + aTime.getM());
        if(result>0) return 1;
        else if(result<0) return -1;
        else return 0;
    }

    public String difference(Time t){
           
        int currentTimeTotalMins = h*60 + m;
        int passedTimeTotalMins = t.getH()*60 + t.getM();
       
        int differenceMins = Math.abs(currentTimeTotalMins-passedTimeTotalMins);
      int hrs = differenceMins/60;
        int mins = differenceMins%60;
       
        return new Time(hrs,mins).toString();
       
    }
}

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

Add a comment
Know the answer?
Add Answer to:
Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from...
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
  • 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...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

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

  • pls help me with it. you just need to answer the question in Appointment.Java, There is...

    pls help me with it. you just need to answer the question in Appointment.Java, There is only 1 question u need to answer! Appointment.Java package option1.stage3; import option1.stage1.Doctor; import option1.stage1.Patient; import option1.stage2.TimeSlot; public class Appointment { private Doctor doctor; private Patient patient; private TimeSlot timeSlot; public Doctor getDoctor() { return doctor; } public void setDoctor(Doctor doctor) { this.doctor = doctor; } public Patient getPatient() { return patient; } public void setPatient(Patient patient) { this.patient = patient; } public TimeSlot getTimeSlot()...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

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

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

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

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
ADVERTISEMENT