Question
Please help me modify the following java code. The instructions are below the code I provided. Thank you! package ObjectsandClasses; public class Time { private
Please help me modify the following java code. The instructions are below the code I provided. Thank you!
package ObjectsandClasses;
public class Time { private int hour; private int minute; private int second; public Time(int hour, int minute, int second){ //Constructor this.hour = hour; this.minute = minute; this.second = second; } public int getHour(){ return this.hour; }
public int getMinute(){ return this.minute; }
public int getSecond(){ return this.second; }
public void setHour(int hour){ this.hour = hour; }
public void setMinute(int minute){ this.minute = minute; }
public void setSecond(int second){ this.second = second; } public void setTime(int hour, int minute, int second){ //to set time this.hour = hour; this.minute = minute; this.second = second; } public String toString(){ //to return the time in hh:mm:ss format String hh = this.hour < 10 ? "0" + this.hour : this.hour+""; //adding 0 to the hour if digit less than 9 String mm = this.minute < 10 ? "0" + this.minute : this.minute+""; //adding 0 to the minute if digit less than 9 String ss = this.second < 10 ? "0" + this.second : this.second+""; //adding 0 to the second if digit less than 9 return hh + ":" + mm + ":" + ss; } public Time nextSecond(){ if(this.second < 59){ this.second = this.second + 1; return this; } this.second = 0; this.minute++; if(this.minute < 59) { return this; } this.minute = 0; this.hour++; if(this.hour < 23){ return this; } this.hour = 0; return this; }
public Time previousSecond(){
if(this.second > 0){ this.second = this.second - 1; return this; } this.second = 59; this.minute--; if(this.minute > 0){ return this; } this.minute = 59; this.hour--; if(this.hour > 0) { return this; } this.hour = 23; return this; } }
package ObjectsandClasses;
public class TestTime {
public static void main(String[] args) { // TODO Auto-generated method stub Time t = new Time(5,25,59); System.out.println("time = "+ t); System.out.println("nextSecond = "+ t.nextSecond()); System.out.println("previousSecond = "+ t.previousSecond()); }
}
Guidelines
Modify the time class
Add the following functionality to your time class.
1. public boolean compareTime(Time t)
This method will take another Time object as a parameter (similar to how we implemented in the MyPoint example shown in class) and return true if the current time object is earlier than Time t. For example see the following lines of code on how this method will be invoked.
Time t1 = new Time(10,34,31) ; // 10:34:31
Time t2 = new Time(20,11,53) ; // 20:11:53
boolean res= t1.compareTime(t2) // should return true, since t1 is earlier than t2.
res = t2.compareTime(t1) // should return false, since t2 is later than t1
2. public int NumberOfSeconds(Time t)
This method will take another Time object as a parameter (similar to how we implemented in the MyPoint example shown in class) and return the difference in time in seconds from the current time object. For example see the following lines of code on how this method will be invoked.
Time t1 = new Time(10,34,31) ; // 10:34:31
Time t2 = new Time(20,11,53) ; // 20:11:53
int secs = t1.NumberOfSeconds(t2); // should return 34642 seconds
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started