Last active
August 29, 2015 14:01
-
-
Save dalmat36/362b89900b51fef8b51c to your computer and use it in GitHub Desktop.
Class to test Time2 Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Time2 { | |
| private int hour; //0-23 | |
| private int minute; //0-59 | |
| private int second; //0-59 | |
| //set a new time value using universal time; ensure that | |
| //the data remains consistent by setting invalid values to zero | |
| public Time2() | |
| { | |
| this(0, 0, 0); | |
| } | |
| public Time2(int h) | |
| { | |
| this(h, 0, 0); | |
| } | |
| public Time2(int h, int m) | |
| { | |
| this(h, m, 0); | |
| } | |
| public Time2(int h, int m, int s) | |
| { | |
| setTime(h, m, s); | |
| } | |
| public Time2(Time2 time) | |
| { | |
| this(time.getHour(), time.getMinute(), time.getSecond()); | |
| } | |
| public void setTime(int h, int m, int s) | |
| { | |
| setHour(h); | |
| setMinute(m); | |
| setSecond(s); | |
| } | |
| /** | |
| * @return the hour | |
| */ | |
| public int getHour() { | |
| return hour; | |
| } | |
| /** | |
| * @param hour the hour to set | |
| */ | |
| public void setHour(int hour) { | |
| this.hour = ((hour >= 0 && hour < 24) ? hour : 0); | |
| } | |
| /** | |
| * @return the minute | |
| */ | |
| public int getMinute() { | |
| return minute; | |
| } | |
| /** | |
| * @param minute the minute to set | |
| */ | |
| public void setMinute(int minute) { | |
| this.minute = ((minute >= 0 && minute < 60) ? minute : 0); | |
| } | |
| /** | |
| * @return the second | |
| */ | |
| public int getSecond() { | |
| return second; | |
| } | |
| /** | |
| * @param second the second to set | |
| */ | |
| public void setSecond(int second) { | |
| this.second = ((second >= 0 && second < 60) ? second : 0); | |
| } | |
| //use explicit and implicit "this" to call toUniversalString | |
| public String buildString() | |
| { | |
| return String.format("%24s: %s\n%24s: %s", | |
| "this.toUniversalString()", this.toUniversalString(), | |
| "toUniversalString()", toUniversalString()); | |
| } | |
| //convert to string in universal-time format (HH:MM:SS) | |
| public String toUniversalString() | |
| { | |
| return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second); | |
| } | |
| //convert to String in standard-time format (H:MM:SS AM or PM) | |
| public String toString() | |
| { | |
| return String.format("%d:%02d:%02d %s", | |
| ((hour == 0 || hour == 12) ? 12: hour % 12), | |
| minute, second, (hour < 12 ? "AM" : "PM")); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Time2Test { | |
| public static void main(String[] args) { | |
| Time2 t1 = new Time2(); //00:00:00 | |
| Time2 t2 = new Time2(2); //02:00:00 | |
| Time2 t3 = new Time2(21,34); //21:34:00 | |
| Time2 t4 = new Time2(12,25,42); //12:25:42 | |
| Time2 t5 = new Time2(27, 74, 99); //00:00:00 | |
| Time2 t6 = new Time2(t4); //12:25:42 | |
| System.out.println("Constructed with:"); | |
| System.out.println("t1: all arguments defaulted"); | |
| System.out.printf(" %s\n", t1.toUniversalString()); | |
| System.out.printf(" %s\n", t1.toString()); | |
| System.out.println("t2: hour specified; minute and second defaulted"); | |
| System.out.printf(" %s\n", t2.toUniversalString()); | |
| System.out.printf(" %s\n", t2.toString()); | |
| System.out.println("t3: hour and minute specified; second defaulted"); | |
| System.out.printf(" %s\n", t3.toUniversalString()); | |
| System.out.printf(" %s\n", t3.toString()); | |
| System.out.println("t4: hour, minute and second specified"); | |
| System.out.printf(" %s\n", t4.toUniversalString()); | |
| System.out.printf(" %s\n", t4.toString()); | |
| System.out.println( "t5: all invalid values specified"); | |
| System.out.printf(" %s\n", t5.toUniversalString()); | |
| System.out.printf(" %s\n", t5.toString()); | |
| System.out.println("t6: Time2 object t4 specified"); | |
| System.out.printf(" %s\n", t6.toUniversalString()); | |
| System.out.printf(" %s\n", t6.toString()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment