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

1. Task: Create a Time class. The class should contain 3 fields (integers called

ID: 3672202 • Letter: 1

Question

1. Task: Create a Time class. The class should contain 3 fields (integers called hrs, mins, secs, for hours, minutes and seconds) and the following methods:

Default and alternate constructors.

Three getters (accessors) to return hours, minutes and seconds

A method named setTime; invalid values set to 0

Two methods named printMilitary and printStandard to print each Time object in military and standard format

A method toString()

A method named equals to compare 2 Time objects for equality

A method named makeCopy to make a copy of a Time object into this Time object

A method named getCopy to make a copy of a Time object into another Time object

A method named advanceSecs to advance time by 1 second

A method named lessThan to decide if a time comes before another time

2. Task: Create a client for the Time class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below:

Initial time t1 (alternate constructor invoked) - military format: 08:15:30
Initial time t1 (alternate constructor invoked) - standard format: 8:15:30 AM
Initial time t2 (default constructor invoked) - military format: 00:00:00
Initial time t2 (default constructor invoked) - standard format: 12:00:00 AM
t2 after call to setTime() - military format: 09:45:35
t2 after call to setTime() - standard format: 9:45:35 AM
After call to equals(): times are NOT equal.
After call to lessThan(): t1 is less than t2.
Enter hours, minutes, and seconds: 10 11 12
New time t1 after call to setTime() - standard format: 10:11:12 AM
New time t1 after call to advanceSecs() - standard format: 10:11:13 AM
New t2 after call to makeCopy() - standard format: 10:11:13 AM
Test toString() for t2: 10:11:13

3. Task: Modify the Time class and add more methods.

3.1. The class Time (discussed in class) has 2 member methods equals and lessThan. It would be easier for the user to have access to new member methods notEquals, lessOrEquals,greaterThan, and greaterOrEquals. Hint: Don’t start from scratch; make use of the already existing methods.

public boolean notEquals(Time otherTime) {... }
public boolean lessOrEquals(Time otherTime) {... }
public boolean greaterThan(Time otherTime) {... }
public boolean greaterOrEquals(Time otherTime) {... }

3.2. Add 2 more methods named toMilitary and toStandard; they should return a String with the military and standard format for a Time object.

Modify the client for complete testing, including the new methods.

Explanation / Answer

// Time.java

public class Time {
   int hrs, mins, secs;
   public Time() {
       hrs = 0;
       mins = 0;
       secs = 0;
   }
   Time(int h, int m, int s){
       hrs = h;
       mins = m;
       secs = s;
   }
   public int getHrs() {
       return hrs;
   }
   public int getMins() {
       return mins;
   }
   public int getSecs() {
       return secs;
   }
   void setTime(int h, int m, int s){
       hrs = h;
       if(hrs > 23 || hrs < 0) hrs = 0;
       mins = m;
       if(mins > 59 || mins < 0) mins = 0;
       secs = s;
       if(secs > 59 || secs < 0) secs = 0;
   }
   void printMilitary(){
       System.out.print("military format: ");
       if(hrs < 10) System.out.print(0);
       System.out.print(hrs + ":");
       if(mins < 10) System.out.print(0);
       System.out.print(mins + ":");
       if(secs < 10) System.out.print(0);
       System.out.println(secs);
   }
   void printStandard(){
       System.out.print("standard format: ");
       if(hrs > 12) System.out.print(hrs - 12);
       else System.out.print(hrs);
       System.out.print(":" + mins + ":" + secs);
       if(hrs >= 12) System.out.println(" PM");
       else System.out.println(" AM");      
   }
   public String toString(){
       return hrs + ":" + mins + ":" + secs;
   }
   static boolean equals(Time t1, Time t2){
       if(t1.hrs == t2.hrs && t1.mins == t2.mins && t1.secs == t2.secs){
           return true;
       }
       return false;
   }
   void makeCopy(Time t){
       this.hrs = t.hrs;
       this.mins = t.mins;
       this.secs = t.secs;
   }
   void advanceSecs(){
       secs++;
       if(secs == 60){
           secs = 0;
           mins++;
           if(mins == 60){
               mins = 0;
               hrs++;
               if(hrs == 24) hrs = 0;
           }
       }
   }
   static boolean lessThan(Time t1, Time t2){
       if(t1.hrs < t2.hrs) return true;
       else if(t1.hrs > t2.hrs) return false;
       else{
           if(t1.mins < t2.mins) return true;
           else if(t1.mins > t2.mins) return false;
           else {
               if(t1.secs < t2.secs) return true;
               else return false;
           }
       }
   }
}

// TimeTest.java

import java.util.Scanner;

public class TimeTest{
   public static void main(String args[]){
       int h, m, s;
       Time t1 = new Time(8, 15, 30);
       Time t2 = new Time();
       System.out.print("Initial time t1 (alternate constructor invoked) - ");
       t1.printMilitary();
       System.out.print("Initial time t1 (alternate constructor invoked) - ");
       t1.printStandard();
       System.out.print("Initial time t2 (default constructor invoked) - ");
       t2.printMilitary();
       System.out.print("Initial time t2 (default constructor invoked) - ");
       t2.printStandard();
       t2.setTime(9, 45, 35);
       System.out.print("t2 after call to setTime() - ");
       t2.printMilitary();
       System.out.print("t2 after call to setTime() - ");
       t2.printStandard();
       if(!Time.equals(t1, t2)){
           System.out.println("After call to equals(): times are NOT equal.");
       }
       else
           System.out.println("After call to equals(): times are equal.");
       if(Time.lessThan(t1, t2)){
           System.out.println("After call to lessThan(): t1 is less than t2.");
       }
       else{
           System.out.println("After call to lessThan(): t1 is NOT less than t2.");
       }
       System.out.print("Enter hours, minutes, and seconds: ");
       Scanner in = new Scanner(System.in);
       h = in.nextInt();
       m = in.nextInt();
       s = in.nextInt();
       t1.setTime(h, m, s);
       System.out.print("New time t1 after call to setTime() - ");
       t1.printStandard();
   }
}