Hello all.
I have an assignment due soon and I'm struggling with this code.
a little background of what I needed to do:
create a class that will receive input from user representing time (H for hour M for minute and S for second)
this program however only has one variable which is: _secFromMid.
The program then stores whatever the user inputted in the form of seconds only.
the class is supposed to then be able to retrieve all of the original parameters from _secFromMid.
Here is the code for the class I have written:
*Please ignore the mistakes in the API as I know the changes needed to be done there..
public class Time2 { private long _secFromMid; //constructors: /** * creates a new Time object * @param _hour the hour in the time(0-23) * @param _minute the minute in the time(0-59) * @param _second the second inthe time (0-59) */ public Time2(int h, int m, int s) { if(h<0 || h>23) h=0; if(m<0 || m>59) m=0; if(s<0 || s>59) s=0; _secFromMid=h*3600+m*60+s; } public Time2 (Time2 other){ _secFromMid=other._secFromMid; } /** gets the year */ public int getHour(){ return (int)((_secFromMid-_secFromMid%3600)/3600); } /** gets the month */ public int getMinute(){ return (int)((_secFromMid%3600)/60); } /** gets the Day */ public int getSecond(){ return (int)(_secFromMid-((_secFromMid-_secFromMid%3600)+(_secFromMid%3600))); } /** sets the year * @param yearToSet the value to be set */ public void setHour(int num){ if(num<0 || num>23) _secFromMid=_secFromMid; else _secFromMid=num*3600+(_secFromMid-(_secFromMid-_secFromMid%3600)); } /** set the month * @param monthToSet the value to be set */ public void setMinute(int num){ if(num<0 || num>59) _secFromMid=_secFromMid; else _secFromMid=num*60+(_secFromMid-_secFromMid%3600); } /** sets the day * @param dayToSet the value to be set */ public void setSecond (int num){ if(num<0 || num>59) _secFromMid=_secFromMid; else _secFromMid=num+(_secFromMid-((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600)); } public String toString(){ String res=""; if(((_secFromMid-_secFromMid%3600)/3600)<10) res=res+"0"; res=res+((_secFromMid-_secFromMid%3600)/3600)+":"; if(((_secFromMid%3600)/60)<10) res=res+"0"; res=res+((_secFromMid%3600)/60)+":"; if((_secFromMid-(((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600)/60))<10) res=res+"0"; res=res+(_secFromMid-(((_secFromMid-_secFromMid%3600)/3600)+(_secFromMid%3600)/60)); return res; } public boolean equals (Time2 other){ if(_secFromMid==other._secFromMid) return true; return false; } public int difference (Time2 other){ return (int)_secFromMid-(int)other._secFromMid; } /** * checks if this date comes before a given date * @param date2 the given date * @retun true iff this date comes before date2 */ public boolean before(Time2 other) { if (_secFromMid < other._secFromMid) return true; return false; } public boolean after(Time2 other) { return other.before(this); } }
Here is the tester given to me I need to run this code with:
*This tester is not to be tempered with
public class Time2Driver { public static void main(String[] args) { Time2 firstTime = new Time2(18, 33, 59); Time2 secondTime = new Time2(firstTime); System.out.println("The hour of first Time2 object is: " + firstTime.getHour()); System.out.println("The minute of first Time2 object is: " + firstTime.getMinute()); System.out.println("The second of first Time2 object is: " + firstTime.getSecond()); secondTime.setHour(16); secondTime.setMinute(55); secondTime.setSecond(21); System.out.println("The hour of second Time2 object is: " + secondTime.getHour()); System.out.println("The minute of second Time2 object is: " + secondTime.getMinute()); System.out.println("The second of second Time2 object is: " + secondTime.getSecond()); System.out.println("Is the first time equal to the second time? " + firstTime.equals(secondTime)); System.out.println("Is the first time before the second time? " + firstTime.before(secondTime)); System.out.println("Is the first time after the second time? " + firstTime.after(secondTime)); System.out.println("The difference in seconds between the first and second time is: " + firstTime.difference(secondTime)); System.out.println("The string representation of the first time is: " + firstTime.toString()); } }
The output I need to write my class by is as follows:
Hour of first time is: 18
Minute of first time is: 33
Second of first time is: 59
Hour of second time is: 16
Minute of second time is: 55
Second of second time is: 21
Is the first time equal to the second time? false
Is the first time before the second time? false
Is the first time after the second time? true
The difference in seconds between the first and second time is: 5918
The string representation of the first time is: 18:33:59
I seem to be struggling with the output of the seconds as everything else seems to follow properly.
there must be something wrong in this line of code I wrote that keeps outputting the value 0 instead of 59:return (int)(_secFromMid-((_secFromMid-_secFromMid%3600)+(_secFromMid%3600)));
Would appreciate some help! thanks so much!