Time class Java

//Time class

/**
* Benis Munezero
* COSC 237 Sec 001
* Lab 4 Task 3, Time class
*/

public class Time {
private int hrs;
private int mins;
private int secs;

public Time() {
hrs = 0;
mins = 0;
mins = 0;
}

public Time (int h, int m, int s) {
setTime(h, m, s);
}

public void setTime (int h, int m, int s) {
hrs = (h < 0)? 0 : h; mins = (m < 0)? 0 : m; secs = (s < 0)? 0 : s; } public int getHrs() { return hrs; } public int getMins() { return mins; } public int getSecs() { return secs; } public void set(int h, int m, int s) { if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59) { hrs = h; mins = m; secs = s; } else { hrs = 0; mins = 0; secs = 0; } } public String toString() { String ampm = (hrs < 12)? "AM" : "PM"; int hr = (hrs == 0 || hrs == 12)? 12 : hrs % 12; return String.format("%d:%d:%d %s", hr, mins, secs, ampm); } public boolean equals (Time t) { return hrs == t.hrs && mins == t.mins && secs == t.secs; } public boolean equals (Object obj) { if (obj instanceof Time) { Time t = (Time)obj; return hrs == t.hrs && mins == t.mins && secs == t.secs; } else { return false; } } public boolean notEquals (Time t) { return !equals(t); } public boolean lessThan (Time t) { return (hrs < t.hrs) || (hrs == t.hrs && mins 59) {
secs = 0;
mins++;
if (mins> 59) {
mins = 0;
hrs++;
if (hrs > 23) {
hrs = 0;
}
}
}
}

public void advanceMins() {
mins++;
if (mins > 59) {
mins = 0;
hrs++;
if (hrs > 23) {
hrs = 0;
}
}
}

public void advanceHours() {
hrs++;
if (hrs > 23) {
hrs = 0;
}
}

public void printMilitary () {
System.out.printf("%02d:%02d:%02d", hrs, mins, secs);
}

public void printStandard () {
String ampm = (hrs < 12)? "AM" : "PM"; int hr = (hrs == 0 || hrs == 12)? 12 : hrs % 12; System.out.printf("%2d:%2d:%2d %s", hr, mins, secs, ampm); } public String toMilitary () { return String.format ("%02d:%02d:%02d", hrs, mins, secs); } public String toStandard () { String ampm = (hrs < 12)? "AM" : "PM"; int hr = (hrs == 0 || hrs == 12)? 12 : hrs % 12; return String.format ("%2d:%2d:%2d", hr, mins, secs, ampm); } } //Time client /** * Benis Munezero * COSC 237 Sec 001 * Lab 4 Task 2, Client for class Time */ public class Main { public static void main(String[] args) { Time t1 = new Time (8,15,30); System.out.println("Initial time t1 military format: " + t1.toMilitary() + "\nInitial time t1 standard format:" + t1); Time t2 = new Time (12,00,00); System.out.println("Initial time t2 military format: " + t2.toMilitary() + "\nInitial time t2 standard format:" + t2); t2.set(9, 45, 35); System.out.println("Time t2 after setTime call military format: " + t2.toMilitary() + "\nTime t2 after setTime call standard format: " + t2); if (t1.equals(t2)) System.out.println("times are equal!"); else System.out.println("times are NOT equal!"); if (t1.lessThan(t2)) System.out.println("t1 is less than t2!"); else System.out.println("t2 is less than t1!"); int hour = t1.getHrs(); int min = t1.getMins(); int sec = t1.getSecs(); System.out.println("Time 1 = " + hour + ":" + min + ":" + sec); t1.set(10,11,12); System.out.println("New Time t1 after setTime call = " + t1); t1.advanceSecs(); System.out.println("New Time t1 after advanceSecs call - standard format " + t1); t2.copy(t1); System.out.println("New t2 after call to copy - standard format: " + t2.toString()); t2 = t1.getCopy(); System.out.println ("t2 after getCopy call - standard format: " + t2); if (t2.notEquals(t1)) { System.out.println("t2 is NOT equal to t1!"); } else { System.out.println("t2 is equal to t1!"); } t2.advanceMins(); System.out.println ("t2 after adavancing mins is - military format: " + t2.toMilitary()); t1.advanceHours(); System.out.println ("t1 after adavancing hours is - standard format: " + t1.toStandard()); Time t3 = t2.getCopy(); System.out.println("t2 - standard format = " + t2.toStandard() + "\nt3 after getCopy call from t2 - standard format = " + t3.toStandard()); if(t3.greaterOrEquals(t2)) { System.out.println("t3 is greater or equal to t2!"); } else { System.out.println("t3 is less than t2!"); } System.out.println("t1 = " + t1.toStandard() + "\nt2 = " + t2.toStandard()); if (t1.lessOrEquals(t2)) { System.out.println("t1 is less or equal to t2!"); } else { System.out.println("t1 is greater than t2!"); } } }