Question: Java Help 3. Task: Modify the Time class and add more methods. 3.1. The class Time (discussed in class) has 2 member methods equals and

Java Help

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: Dont 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.

3.3. Have 3 methods to advance time, not only increment. Add to class Time the methods advanceHrs, advanceMins, and advanceSecs.

3.4. Change the method getCopy() try to have a single return statement

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

Class Time (Time.java):

public class Time {

private int hrs; //store hours

private int mins; //store minutes

private int secs; //store seconds

//Default constructor

public Time() {

hrs = 0;

mins = 0;

secs = 0;

}

//Alternate constructor with parameters, to set the time

public Time(int h, int m, int s) {

hrs = h;

mins = m;

secs = s;

}

//Method to set the time

public void setTime(int h, int m, int s) {

hrs = (h >= 0 && h < 24)? h : 0;

mins = (m >= 0 && m < 60)? m : 0;

secs = (s >= 0 && s < 60)? s : 0;

}

//Method to return the hours

public int getHours() {

return hrs;

}

//Method to return the minutes

public int getMinutes(){

return mins;

}

//Method to return the seconds

public int getSeconds() {

return secs;

}

//Method to print time in military format

//Time is printed in the form HH:MM:SS

public void printTimeMilitary(){

System.out.print((hrs < 10? "0": "") + hrs + ":");

System.out.print((mins < 10? "0": "") + mins + ":");

System.out.print((secs < 10? "0": "") + secs);

}

//Method to print time in standard format

//Time is printed in the form HH:MM:SS AM/PM

public void printTimeStandard(){

System.out.print((hrs == 0 || hrs == 12? 12: hrs % 12) + ":");

System.out.print((mins < 10? "0": "") + mins + ":");

System.out.print((secs < 10? "0": "") + secs + " ");

System.out.print((hrs < 12? "AM": "PM"));

}

//Method toString

public String toString(){

return hrs + ":" + mins + ":" + secs;

}

//Time advanced by one second. When 23:59:59 wrap around to 00:00:00 public void increment(){ secs++; if(secs > 59){ secs = 0; mins++; if(mins > 59){ mins = 0; hrs++; if(hrs > 23) hrs = 0; } } }

//Method to compare two times for equality

public boolean equals(Time otherTime) {

return (hrs == otherTime.hrs && mins == otherTime.mins && secs == otherTime.secs);

}

//Method to compare two times for less than

public boolean lessThan(Time t) {

return (hrs < t.hrs || hrs == t.hrs && mins < t.mins || hrs == t.hrs && mins == t.mins && secs < t.secs);

}

//Method to copy the time

public void copy(Time otherTime) {

hrs = otherTime.hrs;

mins = otherTime.mins;

secs = otherTime.secs;

}

//Method to return a copy of the time

public Time getCopy() {

Time temp = new Time();

temp.hrs = hrs;

temp.mins = mins;

temp.secs = secs;

return temp;

}

}

//CLIENT #1: Program to test class Time (TimeClient1.java) import java.util.Scanner; public class TimeClient1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Time t1 = new Time(8, 15, 30); Time t2 = new Time(); int hours, minutes, seconds;

System.out.print("Initial time t1 (alternate constructor invoked) - military format: "); t1.printTimeMilitary(); System.out.println();

System.out.print("Initial time t1 (alternate constructor invoked) - standard format: "); t1.printTimeStandard(); System.out.println();

System.out.print("Initial time t2 (default constructor invoked) - military format: "); t2.printTimeMilitary(); System.out.println(); System.out.print("Initial time t2 (default constructor invoked) - standard format: "); t2.printTimeStandard(); System.out.println();

t2.setTime(9, 45, 35); System.out.print("t2 after call to setTime - military format: "); t2.printTimeMilitary(); System.out.println(); System.out.print("t2 after call to setTime - standard format: "); t2.printTimeStandard(); System.out.println();

if(t1.equals(t2)) System.out.println("After call to equals: times are equal."); else System.out.println("After call to equals: times are NOT equal.");

if(t1.lessThan(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: "); hours = input.nextInt(); //valid type?? minutes = input.nextInt(); //valid type?? seconds = input.nextInt(); //valid type??

t1.setTime(hours, minutes, seconds); System.out.print("New time t1 after call to setTime - standard format: "); t1.printTimeStandard(); System.out.println();

t1.increment(); System.out.print("New time t1 after call to increment - standard format: "); t1.printTimeStandard(); System.out.println();

t2.copy(t1); System.out.print("New t2 after call to copy - standard format: "); t2.printTimeStandard(); System.out.println(); System.out.println("Test toString for t2: " + t2); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!