chaibs

clockwithstatic

Jan 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package clocl;
  2.  
  3. import org.omg.Messaging.SyncScopeHelper;
  4.  
  5. public class Clock {
  6.     private int hour, minutes;
  7.     private final int maxHour = 24, maxMinutes = 60;
  8.     private static int format;
  9.  
  10.     public Clock(int h, int m) {// constructor
  11.         if (h < 0 || h >= this.maxHour) {// checking if hour input is correct
  12.             this.hour = 0;
  13.         } else {
  14.             this.hour = h;
  15.         }
  16.  
  17.         if (m < 0 || m >= this.maxMinutes) {// checking if minutes input is correct
  18.             this.minutes = 0;
  19.         } else {
  20.             this.minutes = m;
  21.         }
  22.     }
  23.  
  24.     public Clock(Clock other) {// copy constructor
  25.         this.hour = other.hour;
  26.         this.minutes = other.minutes;
  27.     }
  28.  
  29.     public int getHour() {
  30.         return hour;
  31.     }
  32.  
  33.     public boolean setHour(int h) {
  34.         if (h < 0 || h >= this.maxHour) {// checking if hours input is correct
  35.             this.hour = 0;
  36.             return false;
  37.         } else {
  38.             this.hour = h;
  39.             return true;
  40.         }
  41.     }
  42.  
  43.     public int getMinutes() {
  44.         return minutes;
  45.     }
  46.  
  47.     public boolean setMinutes(int m) {
  48.         if (m < 0 || m >= this.maxMinutes) {// checking if seconds input is correct
  49.             this.minutes = 0;
  50.             return false;
  51.         } else {
  52.             this.minutes = m;
  53.             return true;
  54.         }
  55.     }
  56.  
  57.     public static int getFormat() {
  58.         return format;
  59.     }
  60.  
  61.     public static boolean setFormat(int f) {// setting the clock format
  62.         if (f != 12 && f != 24) {
  63.             return false;
  64.         } else {
  65.             Clock.format = f;
  66.             return true;
  67.         }
  68.     }
  69.  
  70.     public String toString() {// method for showing the time
  71.         String res = "";
  72.         if (Clock.format == 24) {
  73.             if (this.hour < 10) {
  74.                 res = "0";
  75.             }
  76.             res += this.hour + ":";
  77.             if (this.minutes < 10) {
  78.                 res += "0";
  79.             }
  80.             res += this.minutes;
  81.             return "The hour is " + res;
  82.         } else {
  83.             if (this.hour < 10) {
  84.                 res = "0";
  85.             }
  86.             res += (hour == 12 ? hour : hour % 12) + ":";
  87.             if (this.minutes < 10) {
  88.                 res += "0";
  89.             }
  90.             res += this.minutes;
  91.             return "The hour is " + res;
  92.         }
  93.     }
  94.  
  95. }
  96. ------------------------------------------------------------------------------------------------------
  97. package clocl;
  98.  
  99. import java.util.Scanner;
  100.  
  101. public class Main {
  102.  
  103.     public static void main(String[] args) {
  104.         Clock c1 = new Clock(11, 51);// creating new clock obj
  105.         Clock c2 = new Clock(c1);// creating copy obj
  106.         Scanner s = new Scanner(System.in);
  107.         boolean res;
  108.  
  109.         do {
  110.             System.out.println("Enter the hour format you wish:(12/24) ");
  111.             res = Clock.setFormat(s.nextInt());
  112.         } while (res != true);// as long as the format isnt correct
  113.  
  114.         System.out.println(c1);// print result
  115.         System.out.println(c2);// print result
  116.        
  117.         c1.setHour(13);// setting the hour
  118.         c1.setMinutes(40);// setting the minutes
  119.         System.out.println(c1);// print result
  120.         System.out.println(c2);// print result
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment