Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Time2
- {
- public int totalSeconds;
- // Time2 no-argument constructor:
- // initializes each instance variable to zero
- public void settotalSeconds (int hour, int minute, int second)
- {
- totalSeconds = (getHour() + getMinute() + getSecond());
- }
- public int gettotalSeconds()
- {
- return totalSeconds;
- }
- public Time2()
- {
- this(0, 0, 0); // invoke constructor with three arguments
- }
- // Time2 constructor: hour supplied, minute and second defaulted to 0
- public Time2(int hour)
- {
- this(hour, 0, 0); // invoke constructor with three arguments
- }
- // Time2 constructor: hour and minute supplied, second defaulted to 0
- public Time2(int hour, int minute)
- {
- this(hour, minute, 0); // invoke constructor with three arguments
- }
- // Time2 constructor: hour, minute and second supplied
- public Time2(int hour, int minute, int second)
- {
- if (hour < 0 || hour >= 24)
- throw new IllegalArgumentException("hour must be 0-23");
- if (minute < 0 || minute >= 60)
- throw new IllegalArgumentException("minute must be 0-59");
- if (second < 0 || second >= 60)
- throw new IllegalArgumentException("second must be 0-59");
- setHour(hour);
- setMinute(minute);
- setSecond(second);
- }
- // Time2 constructor: another Time2 object supplied
- public Time2(Time2 time)
- {
- // invoke constructor with three arguments
- this(time.getHour(), time.getMinute(), time.getSecond());
- }
- // Set Methods
- // set a new time value using universal time;
- // validate the data
- public void setTime(int hour, int minute, int second)
- {
- if (hour < 0 || hour >= 24)
- throw new IllegalArgumentException("hour must be 0-23");
- if (minute < 0 || minute >= 60)
- throw new IllegalArgumentException("minute must be 0-59");
- if (second < 0 || second >= 60)
- throw new IllegalArgumentException("second must be 0-59");
- setHour(hour);
- setMinute(minute);
- setSecond(second);
- }
- // validate and set hour
- public final void setHour(int hour)
- {
- if (hour < 0 || hour >= 24)
- throw new IllegalArgumentException("hour must be 0-23");
- totalSeconds = totalSeconds - (getHour() * 60 * 60);
- }
- // validate and set minute
- public final void setMinute(int minute)
- {
- if (minute < 0 || minute >= 60)
- throw new IllegalArgumentException("minute must be 0-59");
- totalSeconds = totalSeconds - (getMinute() *60);
- }
- // validate and set second
- public final void setSecond(int second)
- {
- if (second < 0 || second >= 60)
- throw new IllegalArgumentException("second must be 0-59");
- totalSeconds = totalSeconds - (getSecond());
- }
- public void settotalSeconds(int totalSeconds)
- {
- totalSeconds = (getHour() + getMinute() + getSecond());
- }
- // Get Methods
- // get hour value
- public int getHour()
- {
- return (totalSeconds / 60 / 60);
- }
- // get minute value
- public int getMinute()
- {
- return (totalSeconds / 60 % 60);
- }
- // get second value
- public int getSecond()
- {
- return (totalSeconds % 60);
- }
- // convert to String in universal-time format (HH:MM:SS)
- public String toUniversalString()
- {
- return String.format(
- "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
- }
- // convert to String in standard-time format (H:MM:SS AM or PM)
- public String toString()
- {
- return String.format("%d:%02d:%02d %s",
- ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
- getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
- }
- } // end class Time2
Advertisement
Add Comment
Please, Sign In to add comment