Advertisement
mmayoub

Static, Exercise 01, slide 24

Jul 18th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. Static, Exercise 01, slide 24
  2.  
  3. class Tester
  4. *****************************************************************************
  5. public class Tester {
  6.  
  7.     public static void main(String[] args) {
  8.         // create two clocks
  9.         Clock c1 = new Clock(23, 59);
  10.         Clock c2 = new Clock(c1);
  11.         c2.setHours(11, Clock.AmPm.AM);
  12.  
  13.         // display clocks on format 24 hours
  14.         System.out.println("Dispaly in 24 hours format:");
  15.         System.out.println("c1 = " + c1);
  16.         System.out.println("c2 = " + c2);
  17.  
  18.         // change display format to 12 hours
  19.         Clock.displayFormat = TimeFormat.TF12;
  20.  
  21.         // display clocks on format 12 hours
  22.         System.out.println("Dispaly in 12 hours format:");
  23.         System.out.println("c1 = " + c1);
  24.         System.out.println("c2 = " + c2);
  25.  
  26.         // clocks tick
  27.         System.out.println("after one tick:");
  28.         c1.tick();
  29.         c2.tick();
  30.  
  31.         System.out.println("c1 = " + c1);
  32.         System.out.println("c2 = " + c2);
  33.  
  34.         // clocks tick
  35.         System.out.println("after one tick:");
  36.         c1.tick();
  37.         c2.tick();
  38.  
  39.         System.out.println("c1 = " + c1);
  40.         System.out.println("c2 = " + c2);
  41.  
  42.         // change display format to 24 hours
  43.         Clock.displayFormat = TimeFormat.TF24;
  44.  
  45.         // display clocks on format 24 hours
  46.         System.out.println("Dispaly in 24 hours format:");
  47.         System.out.println("c1 = " + c1);
  48.         System.out.println("c2 = " + c2);
  49.     }
  50.  
  51. }
  52.  
  53.  
  54. // class Clock
  55. *****************************************************************************
  56. public class Clock {
  57.     // class attributes, shared to all class instances
  58.     // should be initialized even before calling constructor
  59.     private static final int DAY_HOURS = 24;
  60.     private static final int HOUR_MINUTES = 60; // maximum minutes in one hour
  61.     public static TimeFormat displayFormat = TimeFormat.TF24; // time format
  62.                                                                 // for
  63.  
  64.     public static enum AmPm {
  65.         AM, PM
  66.     };
  67.  
  68.     // instance attributes, always save data in format 24 hours
  69.     private int hours; // clock hours, always saved data in 24 format
  70.     private int minutes; // clock minutes
  71.  
  72.     // private AmPm meridiem; // AM or PM : indicated by hours
  73.  
  74.     // constructor, arguments in 12 hours format
  75.     public Clock(int hours, int minutes, AmPm meridiem) {
  76.         this(toFormat24(hours, meridiem), minutes);
  77.     }
  78.  
  79.     // constructor, arguments in 24 hours format
  80.     public Clock(int hours, int minutes) {
  81.         if (!this.setHours(hours)) {
  82.             this.hours = TimeFormat.TF24.minHour;
  83.         }
  84.         this.setMinutes(minutes);
  85.     }
  86.  
  87.     // copy constructor
  88.     public Clock(Clock otherClock) {
  89.         this.hours = otherClock.hours;
  90.         this.minutes = otherClock.minutes;
  91.     }
  92.  
  93.     // getter and setter for minutes
  94.     public int getMinutes() {
  95.         return this.minutes;
  96.     }
  97.  
  98.     private boolean setMinutes(int minutes) {
  99.         if (minutes < 0 || minutes >= HOUR_MINUTES)
  100.             return false;
  101.  
  102.         this.minutes = minutes;
  103.         return true;
  104.     }
  105.  
  106.     // getter and setter for hours
  107.     public int getHours() {
  108.         return this.hours;
  109.     }
  110.  
  111.     public boolean setHours(int hours) {
  112.         if (hours < TimeFormat.TF24.minHour || hours > TimeFormat.TF24.maxHour)
  113.             return false;
  114.  
  115.         this.hours = hours;
  116.         return true;
  117.     }
  118.  
  119.     public boolean setHours(int hours, AmPm meridiem) {
  120.         return setHours(toFormat24(hours, meridiem));
  121.     }
  122.  
  123.     private static int toFormat24(int hour, AmPm meridiem) {
  124.         return meridiem == AmPm.PM ? (hour + 12) % DAY_HOURS : hour;
  125.     }
  126.  
  127.     private static int toFormat12(int hour) {
  128.         if (hour == 0)
  129.             return displayFormat.maxHour;
  130.         if (hour > displayFormat.maxHour)
  131.             return hour - displayFormat.maxHour;
  132.         return hour;
  133.     }
  134.  
  135.     private String getMeridiem() {
  136.         if (this.hours == 0)
  137.             return minutes == 0 ? "midnight" : "AM";
  138.         if (hours < displayFormat.maxHour)
  139.             return "AM";
  140.         if (hours == displayFormat.maxHour)
  141.             return minutes == 0 ? "noon" : "PM";
  142.  
  143.         return "PM";
  144.     }
  145.  
  146.     public void tick() {
  147.         this.minutes += 1;
  148.         if (minutes == HOUR_MINUTES) {
  149.             minutes = 0;
  150.  
  151.             hours += 1;
  152.             if (hours > TimeFormat.TF24.maxHour)
  153.                 hours = TimeFormat.TF24.minHour;
  154.         }
  155.  
  156.     }
  157.  
  158.     @Override
  159.     public String toString() {
  160.         if (displayFormat == TimeFormat.TF24)
  161.             return String.format("%02d:%02d", this.hours, this.minutes);
  162.         else {
  163.             return String.format("%02d:%02d (%s)", toFormat12(this.hours),
  164.                     this.minutes, getMeridiem());
  165.         }
  166.     }
  167. }
  168.  
  169. enum TimeFormat
  170. **************************************************************************************
  171. public enum TimeFormat {
  172.     TF12(1, 12), TF24(0, 23);
  173.  
  174.     public final int minHour;
  175.     public final int maxHour;
  176.  
  177.     private TimeFormat(int minHour, int maxHour) {
  178.         this.minHour = minHour;
  179.         this.maxHour = maxHour;
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement