novemberstorsm09

Time2

Jul 6th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. public class Time2
  2. {
  3.    
  4.    public int totalSeconds;
  5.    
  6.    // Time2 no-argument constructor:
  7.    // initializes each instance variable to zero
  8.    public void settotalSeconds (int hour, int minute, int second)
  9.    {
  10.        totalSeconds = (getHour() + getMinute() + getSecond());
  11.    }
  12.    public int gettotalSeconds()
  13.    {
  14.        return totalSeconds;
  15.    }
  16.    public Time2()
  17.    {
  18.       this(0, 0, 0); // invoke constructor with three arguments
  19.    }
  20.  
  21.    // Time2 constructor: hour supplied, minute and second defaulted to 0
  22.    public Time2(int hour)
  23.    {
  24.       this(hour, 0, 0); // invoke constructor with three arguments
  25.    }
  26.  
  27.    // Time2 constructor: hour and minute supplied, second defaulted to 0
  28.    public Time2(int hour, int minute)
  29.    {
  30.       this(hour, minute, 0); // invoke constructor with three arguments
  31.    }
  32.    
  33.  
  34.    // Time2 constructor: hour, minute and second supplied
  35.    public Time2(int hour, int minute, int second)
  36.    {
  37.       if (hour < 0 || hour >= 24)
  38.          throw new IllegalArgumentException("hour must be 0-23");
  39.  
  40.       if (minute < 0 || minute >= 60)
  41.          throw new IllegalArgumentException("minute must be 0-59");
  42.  
  43.       if (second < 0 || second >= 60)
  44.          throw new IllegalArgumentException("second must be 0-59");
  45.  
  46.       setHour(hour);
  47.       setMinute(minute);
  48.       setSecond(second);
  49.    }
  50.  
  51.    // Time2 constructor: another Time2 object supplied
  52.    public Time2(Time2 time)
  53.    {
  54.       // invoke constructor with three arguments
  55.       this(time.getHour(), time.getMinute(), time.getSecond());
  56.    }
  57.  
  58.    // Set Methods
  59.    // set a new time value using universal time;
  60.    // validate the data
  61.    public void setTime(int hour, int minute, int second)
  62.    {
  63.       if (hour < 0 || hour >= 24)
  64.          throw new IllegalArgumentException("hour must be 0-23");
  65.  
  66.       if (minute < 0 || minute >= 60)
  67.          throw new IllegalArgumentException("minute must be 0-59");
  68.  
  69.       if (second < 0 || second >= 60)
  70.          throw new IllegalArgumentException("second must be 0-59");
  71.  
  72.       setHour(hour);
  73.       setMinute(minute);
  74.       setSecond(second);
  75.    }
  76.  
  77.    // validate and set hour
  78.    public final void setHour(int hour)
  79.    {
  80.       if (hour < 0 || hour >= 24)
  81.          throw new IllegalArgumentException("hour must be 0-23");
  82.    
  83.       totalSeconds = totalSeconds - (getHour() * 60 * 60);
  84.    }
  85.  
  86.    // validate and set minute
  87.    public final void setMinute(int minute)
  88.    {
  89.       if (minute < 0 || minute >= 60)
  90.          throw new IllegalArgumentException("minute must be 0-59");
  91.  
  92.       totalSeconds = totalSeconds - (getMinute() *60);
  93.    }
  94.  
  95.    // validate and set second
  96.    public final void setSecond(int second)
  97.    {
  98.       if (second < 0 || second >= 60)
  99.          throw new IllegalArgumentException("second must be 0-59");
  100.  
  101.        totalSeconds = totalSeconds - (getSecond());
  102.    }
  103.    public void settotalSeconds(int totalSeconds)
  104.    {
  105.        totalSeconds = (getHour() + getMinute() + getSecond());
  106.    }
  107.    
  108.    // Get Methods
  109.    // get hour value
  110.    public int getHour()
  111.    {
  112.       return (totalSeconds / 60 / 60);
  113.    }
  114.  
  115.    // get minute value
  116.    public int getMinute()
  117.    {
  118.       return (totalSeconds / 60 % 60);
  119.    }
  120.  
  121.    // get second value
  122.    public int getSecond()
  123.    {
  124.       return (totalSeconds % 60);
  125.    }
  126.  
  127.    // convert to String in universal-time format (HH:MM:SS)
  128.    public String toUniversalString()
  129.    {
  130.       return String.format(
  131.          "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  132.    }
  133.  
  134.    // convert to String in standard-time format (H:MM:SS AM or PM)
  135.    public String toString()
  136.    {
  137.       return String.format("%d:%02d:%02d %s",
  138.          ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
  139.          getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
  140.    }
  141. } // end class Time2
Advertisement
Add Comment
Please, Sign In to add comment