idastan97

CSCI152 L3 P1

Jan 17th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. public class Clock {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     // Fields
  7.     private int hours;
  8.     private int minutes;
  9.     private int seconds;
  10.    
  11.     /**
  12.      *
  13.      */
  14.     public Clock(){
  15.         hours=0;
  16.         minutes=0;
  17.         seconds=0;
  18.     }
  19.    
  20.    
  21.     // Constructor
  22.     /**
  23.      * Sets the hours, minutes and seconds fields to the given parameters h, m
  24.      * and s, if h is between 0 and 23, m and s are between 0 and 59
  25.      * inclusively. If at least on of the parameters is not in the valid range,
  26.      * the fields remains unchanged.
  27.      *
  28.      * @param h the hours to set
  29.      * @param m the minutes to set
  30.      * @param s the seconds to set
  31.      * @throws Exception if the parameters are not in the range
  32.      */
  33.     public Clock(int h, int m, int s) throws Exception{
  34.         if (h>=24 || h<0 || m>=60 || m<0 || s>=60 || s<0){
  35.             throw new Exception("Invalid time format!");
  36.         } else {
  37.             hours = h; minutes = m; seconds = s;
  38.         }
  39.     }
  40.    
  41.     // Reset method
  42.     /**
  43.      * Resets all fields to 0.
  44.      *
  45.      */
  46.     public void resetToMidnight() {
  47.         hours = 0; minutes = 0; seconds = 0;
  48.     }
  49.  
  50.     // Check if morning method
  51.     /**
  52.      * Shows is it morning by returning boolean.
  53.      *
  54.      * @return
  55.      */
  56.     public boolean isMorning() {
  57.         return (hours>=5 && hours<12);
  58.     }
  59.  
  60.     // Advance one second method
  61.     /**
  62.      * Does one tick.
  63.      *
  64.      */
  65.     public void tick() {
  66.         seconds++;
  67.         if (seconds >= 60) {
  68.             seconds = 0;
  69.             // need to increment mins, etc.
  70.             minutes++;
  71.             if (minutes>=60){
  72.                 minutes=0;
  73.                 hours++;
  74.                 if (hours>=24){
  75.                     hours=0;
  76.                 }
  77.             }
  78.         }
  79.     }
  80.    
  81.     /**
  82.      * Shows the fields in time format.
  83.      *
  84.      * @return String
  85.      */
  86.     public String toString(){
  87.         String sh, sm, ss;
  88.         if (hours<10){
  89.             sh="0"+hours;
  90.         } else {
  91.             sh=""+hours;
  92.         }
  93.         if (minutes<10){
  94.             sm="0"+minutes;
  95.         } else {
  96.             sm=""+minutes;
  97.         }
  98.         if (seconds<10){
  99.             ss="0"+seconds;
  100.         } else {
  101.             ss=""+seconds;
  102.         }
  103.         return sh+":"+sm+":"+ss;
  104.     }
  105.  
  106.     /**
  107.      * Returns hors field value.
  108.      *
  109.      * @return the hours
  110.      */
  111.     public int getHours() {
  112.         return hours;
  113.     }
  114.  
  115.     /**
  116.      * Sets the hours field to the hours, if the hours is in range from 0 to 23
  117.      * inclusively. If the hours parameter is not in the range, the hours field
  118.      * remains unchanged.
  119.      *
  120.      * @param hours the hours to set
  121.      * @throws Exception is hours is not in the range
  122.      */
  123.     public void setHours(int hours) throws Exception{
  124.         if (hours>=24 || hours<0){
  125.             throw new Exception("Invalid hours format!");
  126.         }
  127.         this.hours = hours;
  128.     }
  129.  
  130.     /**
  131.      * Gets the value of minutes field.
  132.      *
  133.      * @return the minutes
  134.      */
  135.     public int getMinutes() {
  136.         return minutes;
  137.     }
  138.  
  139.     /**
  140.      * Sets the minutes field to the minutes, if the parameter minutes is in
  141.      * range from 0 to 59 inclusively. If the minutes parameter is not in the
  142.      * range, the minutes field remains unchanged.
  143.      *
  144.      * @param minutes the minutes to set
  145.      * @throws Exception if minutes is not in the range
  146.      */
  147.     public void setMinutes(int minutes) throws Exception{
  148.         if (minutes>=60 || minutes<0){
  149.             throw new Exception("Invalid minutes format!");
  150.         }
  151.         this.minutes = minutes;
  152.     }
  153.  
  154.     /**
  155.      * @return the seconds
  156.      */
  157.     public int getSeconds() {
  158.         return seconds;
  159.     }
  160.  
  161.     /**
  162.      * Sets the seconds field to the seconds, if the parameter seconds is in
  163.      * range from 0 to 59 inclusively. If the seconds parameter is not in the
  164.      * range, the seconds field remains unchanged.
  165.      *
  166.      * @param seconds the seconds to set
  167.      * @throws Exception if seconds is not in the range
  168.      */
  169.     public void setSeconds(int seconds) throws Exception{
  170.         if (seconds>=24 || seconds<0){
  171.             throw new Exception("Invalid seconds format!");
  172.         }
  173.         this.seconds = seconds;
  174.     }
  175. }
  176.  
  177.  
  178. public class testClock {
  179.     public static void main(String[] args){
  180.         Clock myClock = new Clock();
  181.         try{
  182.             myClock=new Clock(24, 13, 30);
  183.         } catch (Exception ex) {
  184.             System.out.println(ex.getMessage());
  185.         }
  186.         System.out.println(myClock);
  187.        
  188.         try{
  189.             myClock.setHours(24);
  190.         } catch (Exception ex){
  191.             System.out.println(ex.getMessage());
  192.         }
  193.         System.out.println(myClock);
  194.        
  195.         try{
  196.             myClock.setMinutes(60);
  197.         } catch (Exception ex){
  198.             System.out.println(ex.getMessage());
  199.         }
  200.         System.out.println(myClock);
  201.        
  202.         try{
  203.             myClock.setSeconds(60);
  204.         } catch (Exception ex){
  205.             System.out.println(ex.getMessage());
  206.         }
  207.         System.out.println(myClock);
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment