Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment you are to design and implement a class Time. Objects of thi

ID: 3837165 • Letter: F

Question

For this assignment you are to design and implement a class Time. Objects of this class are intended to represent time values that are viewed as consisting of an hour, minute, and second values. The values associated with a Time object can range between 00:00:00 (midnight) and 23:59:59, inclusively. The public interface for this class should consist of the following sets of functions:

Constructors:
Time():
This function defines a default initial value of 00:00:00 for a Time object.

Time(int h, int m, int s):
This function initializes a Time object so that its hours is h, its minutes is m, and its seconds is s. The parameters that correspond to the minutes and seconds should be specified with a default value of 0.

Access Functions:
int getHours() const;
Returns the hours value of a Time object.
int getMinutes() const;
Returns the minutes value of a Time object.
int getSeconds() const;
Returns the seconds value of a Time object.
bool LessThan(Time) const;
For two Time objects t1 and t2, t1.LessThan(t2) returns true if t1 is less than, or comes before t2.
bool GreaterThan(Time) const;
For two Time objects t1 and t2, t1.GreaterThan(t2) returns true if t1 is greater than, or comes after t2.
bool EqualTo(Time) const;
For two Time objects t1 and t2, t1.EqualTo(t2) returns true if t1 is equal to, or is the same time as t2.

Modifier Functions:
void setHours(int h);
Set the hours of Time object to value specified by h.
void setMinutes(int m);
Set the minutes of Time object to value specified by m.
void setSeconds(int s);
Set the seconds of Time object to value specified by s.
void setTime(int h, int m, int s);
Set the hours, minutes and seconds of a Time object to the values specified by h, m and s, respectively. The parameters corresponding to the minutes and seconds value should have default values of 0.

Input/Output Functions:
void Read();
t1.Read(); accepts from the keyboard a time value for t1 that is input in the form hh:mm:ss.
void Write();
t1.Write(); outputs to the display the value of t1 in the format hh:mm:ss.

Using the Time class you are to implement a well-designed program that includes two Time objects, say t1 and t2 (you may use other identifiers), where t1 is uninitialized and t2 is initialized to the value 12:45:35. The program should output the values of t1 and t2. Next the program should prompt the user to enter two values (one prompt for each), after which the program outputs these two values in ascending order, regardless of the order that the data was entered.

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Sam
*/
public class Time {
    private int hours;
    private int minutes;
    private int seconds;

    public Time() {
        hours = 0;
        minutes = 0;
        seconds = 0;
    }

    public Time(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    public int getHours() {
        return hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getSeconds() {
        return seconds;
    }
  
    public boolean isLessThan(Time other) {
        if (hours < other.hours)
            return true;
        else if (hours > other.hours)
            return false;
        if (minutes < other.minutes) //this case will be considered only if hours are equal
            return true;
        else if (minutes > other.minutes)
           return false;
        if (seconds < other.seconds) //considered only if hiurs and minutes are euqal
            return true;
        return false; // second are equal or less sore than other
    }
  
    public boolean isGreaterThan(Time other){
        if (this.equals(other))
            return false;
        return !isLessThan(other);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Time other = (Time) obj;
        if (this.hours != other.hours) {
            return false;
        }
        if (this.minutes != other.minutes) {
            return false;
        }
        if (this.seconds != other.seconds) {
            return false;
        }
        return true;
    }
  
    public void setTime(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }
  
    public void read() throws IOException {
        String[] time = (new BufferedReader(new InputStreamReader(System.in))).readLine().split(":");
        hours = Integer.parseInt(time[0]);
        minutes = Integer.parseInt(time[1]);
        seconds = Integer.parseInt(time[2]);
    }
  
    public void write() {
        System.out.println(hours + " : " + minutes + " : " +seconds);
    }
}

I hope this code will help you. I have commented the criticalsections of the code to make things easy. Let me know if you need any more guidance on this.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote