Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Clock {
- enum HoursPerDay {
- AM(24), PM(12);
- // enum Constructor
- private HoursPerDay(final int _Hours) {
- this._Hours = _Hours;
- }
- // Internal state Parameter Private
- private int _Hours;
- // Type of HoursPerDay if Value of Hours < 12 return AM
- // Else > 12 return PM
- public static HoursPerDay valueOf(int _Hours) {
- if (_Hours > HOURS12 && _Hours <= HOURS24) {
- return HoursPerDay.PM;
- } else if (_Hours <= HOURS12) {
- return HoursPerDay.AM;
- }
- return HoursPerDay.PM;
- }
- } // End of Enum
- private static final int MINUTES = 60;
- private static int HOURS24 = 24;
- private static int HOURS12 = 12;
- private int _Min;
- private int _Hour;
- // Clock Constructor
- // _Hour parameter to hold the Hour value
- // _Min parameter to hold the Minutes value
- public Clock(int _Hour, int _Min) {
- // Check if the user entered the value of correct parameters
- if (_Hour < HOURS24) {
- this._Hour = _Hour;
- } else // if not Prompt user to enter the right values and set hour to
- // zero
- {
- System.out.println("Hours should be between 0-23 ...");
- this._Hour = 0;
- }
- // Check if the user entered the value of correct parameters
- if (_Min < MINUTES) {
- this._Min = _Min;
- } else { // if not Prompt for mistake and set Minute to 00
- System.out.println("MINUTES should be between 0-59... the MINUTES = 0");
- this._Min = 0;
- }
- }
- // creating a copy of Clock to hold hour and minutes to manipulate output
- public Clock(Clock Other) {
- this._Hour = Other._Hour;
- this._Min = Other._Min;
- }
- // Minute getter - return the minutes entered
- public int getMin() {
- return this._Min;
- }
- // Hour getter - return hour entered
- public int getH() {
- return this._Hour;
- }
- // Method that set the format when Hours > 12 and < 24
- // Format 12 will subtract 12 from the hour value
- // Else user want to display 24 hours format
- public void format(int format) {
- switch (format) {
- case 12:
- if (_Hour > HOURS12 && _Hour < 24) {
- this._Hour -= HOURS12;
- } else {
- this._Hour = _Hour;
- }
- if (_Hour == 0) {
- this._Hour = _Hour;
- }
- break;
- case 24:
- break;
- }
- }
- // Method to string to add 0 to the entered hour if Hour less than 10
- // Minutes also is fixed when minutes are less than 10
- @Override
- public String toString() {
- String str = "";
- if (_Hour < 10) {
- str += "0";
- }
- str += getH() + " : ";
- if (_Min < 10) {
- str += "0";
- }
- str += getMin();
- return "Clock " + str + " " + HoursPerDay.valueOf(_Hour);
- }
- }
- import java.util.Scanner;
- public class ClockTester{
- public static void main(String[] args){
- Scanner s = new Scanner(System.in);
- int h, h1;
- Clock c1 = new Clock(8, 45);
- System.out.println("The clock is :" + c1.toString());
- System.out.println("Enter 12 if you like to see hour in 12 Hour format or 24 Format? ");
- h = s.nextInt();
- c1.format(h);
- System.out.println("The clock is :" +c1.toString());
- System.out.println(" ==============The 2nd clock=============");
- Clock c2 = new Clock(23,40);
- System.out.println("The clock is :" + c2.toString());
- System.out.println("Enter 12 if you like to see hour in 12 Hour format or 24 Format? ");
- h1 = s.nextInt();
- c2.format(h1);
- System.out.print("The clock is :" +c2.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement