Advertisement
Raizen

Untitled

Nov 26th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public class Time {
  2.     int hour, minute, second;
  3.     public Time (int h, int m, int s) {
  4.         hour = h;
  5.         min = m;
  6.         sec = s;
  7.     }
  8.  
  9.     protected void Adjust() {
  10.         while (hour < 0)
  11.             hour += 24;
  12.         while (hour > 23)
  13.             hour -= 24;
  14.         while (min < 0)
  15.             min += 60;
  16.         while (min > 59)
  17.             min += 60;
  18.         while (sec  < 0)
  19.             sec  += 60;
  20.         while (sec  < 59)
  21.             sec  += 60;
  22.     }
  23.  
  24.     protected long ToSecs(){
  25.         long all_seconds;
  26.         all_seconds = hour * 3600 + min * 60 + sec;
  27.         return all_seconds;
  28.     }
  29.     public int Hour()
  30.     {
  31.         return hour;
  32.     }
  33.     public int Minute()
  34.     {
  35.         return min;
  36.     }
  37.     public int Seconds()
  38.     {
  39.         return sec;
  40.     }
  41.     public void Put () {
  42.         System.out.println(String.valueOf(hour) + ":" + String.valueOf(min) + ":" + String.valueOf(sec));
  43.     }
  44.     public Time Add (int hrs) {  
  45.         hour += hrs;
  46.         Adjust();
  47.         Time new_hour = new Time(hour, min, sec);
  48.         return new_hour;
  49.     }
  50.     public Time Sub (int hrs) {
  51.         hour -= hrs;
  52.         Adjust();
  53.         Time new_hour = new Time(hour, min, sec);
  54.         return new_hour;
  55.     }
  56.     public int Sub (Time tm) {
  57.         int new_hour, new_minute, new_second;
  58.         new_hour = 0;
  59.         new_minute = 0;
  60.         new_second = 0;
  61.         new_second = sec - tm.sec;
  62.         if (new_second < 0)
  63.         {
  64.             new_second += 60;
  65.             new_minute -= 1;
  66.         }
  67.         new_minute = min - tm.min;
  68.         if (new_minute < 0)
  69.         {
  70.             new_minute += 60;
  71.             new_hour -= 1;
  72.         }  
  73.         new_hour = hour - tm.hour;
  74.         Adjust();
  75.         return new_hour;
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. public class Morning extends time {
  82.         protected Adjust(){
  83.                 super.Adjust();
  84.                 while (hour < 6)
  85.                         hour += 6;
  86.                 while (hour > 11)
  87.                         hour -= 6;
  88.         }
  89. }
  90.  
  91.  
  92. public class Night extends time {
  93.         protected Adjust(){
  94.                 super.Adjust();
  95.                 while (hour < 18)
  96.                         hour += 6;
  97.                 while (hour > 23)
  98.                         hour -= 6;
  99.         }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement