Advertisement
rushdie

מצגת 9 - CLOCK

Dec 15th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. public class Clock {
  2.  
  3.     enum HoursPerDay {
  4.         AM(24), PM(12);
  5.  
  6.         // enum Constructor
  7.         private HoursPerDay(final int _Hours) {
  8.             this._Hours = _Hours;
  9.         }
  10.  
  11.         // Internal state Parameter Private
  12.         private int _Hours;
  13.  
  14.         // Type of HoursPerDay if Value of Hours < 12 return AM
  15.         // Else > 12 return PM
  16.         public static HoursPerDay valueOf(int _Hours) {
  17.             if (_Hours > HOURS12 && _Hours <= HOURS24) {
  18.                 return HoursPerDay.PM;
  19.             } else if (_Hours <= HOURS12) {
  20.                 return HoursPerDay.AM;
  21.             }
  22.  
  23.             return HoursPerDay.PM;
  24.         }
  25.  
  26.     } // End of Enum
  27.  
  28.     private static final int MINUTES = 60;
  29.     private static int HOURS24 = 24;
  30.     private static int HOURS12 = 12;
  31.     private int _Min;
  32.     private int _Hour;
  33.  
  34.     // Clock Constructor
  35.     // _Hour parameter to hold the Hour value
  36.     // _Min parameter to hold the Minutes value
  37.  
  38.     public Clock(int _Hour, int _Min) {
  39.         // Check if the user entered the value of correct parameters
  40.         if (_Hour < HOURS24) {
  41.             this._Hour = _Hour;
  42.         } else // if not Prompt user to enter the right values and set hour to
  43.                 // zero
  44.         {
  45.             System.out.println("Hours should be between 0-23  ...");
  46.             this._Hour = 0;
  47.         }
  48.         // Check if the user entered the value of correct parameters
  49.         if (_Min < MINUTES) {
  50.             this._Min = _Min;
  51.         } else { // if not Prompt for mistake and set Minute to 00
  52.             System.out.println("MINUTES should be between 0-59... the MINUTES = 0");
  53.             this._Min = 0;
  54.         }
  55.     }
  56.  
  57.     // creating a copy of Clock to hold hour and minutes to manipulate output
  58.     public Clock(Clock Other) {
  59.         this._Hour = Other._Hour;
  60.         this._Min = Other._Min;
  61.  
  62.     }
  63.  
  64.     // Minute getter - return the minutes entered
  65.     public int getMin() {
  66.         return this._Min;
  67.     }
  68.  
  69.     // Hour getter - return hour entered
  70.     public int getH() {
  71.         return this._Hour;
  72.  
  73.     }
  74.  
  75.     // Method that set the format when Hours > 12 and < 24
  76.     // Format 12 will subtract 12 from the hour value
  77.     // Else user want to display 24 hours format
  78.  
  79.     public void format(int format) {
  80.         switch (format) {
  81.         case 12:
  82.             if (_Hour > HOURS12 && _Hour < 24) {
  83.                 this._Hour -= HOURS12;
  84.             } else {
  85.                 this._Hour = _Hour;
  86.             }
  87.             if (_Hour == 0) {
  88.                 this._Hour = _Hour;
  89.             }
  90.             break;
  91.  
  92.         case 24:
  93.             break;
  94.         }
  95.     }
  96.  
  97.     // Method to string to add 0 to the entered hour if Hour less than 10
  98.     // Minutes also is fixed when minutes are less than 10
  99.     @Override
  100.     public String toString() {
  101.         String str = "";
  102.         if (_Hour < 10) {
  103.             str += "0";
  104.         }
  105.         str += getH() + " : ";
  106.         if (_Min < 10) {
  107.             str += "0";
  108.         }
  109.         str += getMin();
  110.         return "Clock  " + str + " " + HoursPerDay.valueOf(_Hour);
  111.     }
  112.  
  113. }
  114. import java.util.Scanner;
  115.  
  116. public class ClockTester{
  117.  
  118. public static void main(String[] args){
  119.         Scanner s = new Scanner(System.in);
  120.         int h, h1;
  121.         Clock c1 = new Clock(8, 45);
  122.         System.out.println("The clock is :" + c1.toString());
  123.         System.out.println("Enter 12 if you like to see hour in 12 Hour format or 24 Format? ");
  124.         h = s.nextInt();
  125.         c1.format(h);
  126.         System.out.println("The clock is :" +c1.toString());
  127.        
  128.         System.out.println(" ==============The 2nd clock=============");
  129.  
  130.         Clock c2 = new Clock(23,40);
  131.         System.out.println("The clock is :" + c2.toString());
  132.         System.out.println("Enter 12 if you like to see hour in 12 Hour format or 24 Format? ");
  133.         h1 = s.nextInt();
  134.         c2.format(h1);
  135.         System.out.print("The clock is :" +c2.toString());
  136.  
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement