Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Time {
- int hour, minute, second;
- public Time (int h, int m, int s) {
- hour = h;
- min = m;
- sec = s;
- }
- protected void Adjust() {
- while (hour < 0)
- hour += 24;
- while (hour > 23)
- hour -= 24;
- while (min < 0)
- min += 60;
- while (min > 59)
- min += 60;
- while (sec < 0)
- sec += 60;
- while (sec < 59)
- sec += 60;
- }
- protected long ToSecs(){
- long all_seconds;
- all_seconds = hour * 3600 + min * 60 + sec;
- return all_seconds;
- }
- public int Hour()
- {
- return hour;
- }
- public int Minute()
- {
- return min;
- }
- public int Seconds()
- {
- return sec;
- }
- public void Put () {
- System.out.println(String.valueOf(hour) + ":" + String.valueOf(min) + ":" + String.valueOf(sec));
- }
- public Time Add (int hrs) {
- hour += hrs;
- Adjust();
- Time new_hour = new Time(hour, min, sec);
- return new_hour;
- }
- public Time Sub (int hrs) {
- hour -= hrs;
- Adjust();
- Time new_hour = new Time(hour, min, sec);
- return new_hour;
- }
- public int Sub (Time tm) {
- int new_hour, new_minute, new_second;
- new_hour = 0;
- new_minute = 0;
- new_second = 0;
- new_second = sec - tm.sec;
- if (new_second < 0)
- {
- new_second += 60;
- new_minute -= 1;
- }
- new_minute = min - tm.min;
- if (new_minute < 0)
- {
- new_minute += 60;
- new_hour -= 1;
- }
- new_hour = hour - tm.hour;
- Adjust();
- return new_hour;
- }
- }
- public class Morning extends time {
- protected Adjust(){
- super.Adjust();
- while (hour < 6)
- hour += 6;
- while (hour > 11)
- hour -= 6;
- }
- }
- public class Night extends time {
- protected Adjust(){
- super.Adjust();
- while (hour < 18)
- hour += 6;
- while (hour > 23)
- hour -= 6;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement