Goodiny777

Clocker class and main

Jan 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package MyClock;
  2.  
  3. public class Clocker {
  4.     private int hours, minutes;
  5.     private final int max_hours = 24, max_minutes = 60, min_value =  0;
  6.     private static int format = 24;//default format
  7.  
  8.     public Clocker(int hours, int minutes) {
  9.         this.setHours(hours);
  10.         this.setMinutes(minutes);
  11.     } //main constructor
  12.  
  13.     public Clocker(Clocker object) {
  14.         this.hours = object.getHours();
  15.         this.minutes = object.getMinutes();
  16.     }  // cloning constructor
  17.  
  18.     public Clocker() {
  19.         this(0, 0);
  20.     } //constructor for cases we build an empty object
  21.  
  22.     public boolean setHours(int hours) {
  23.         if (hours <= min_value || hours > max_hours) {
  24.             this.hours = min_value;
  25.             return false;
  26.         } else {
  27.             this.hours = hours;
  28.             return true;
  29.         }
  30.     }
  31.  
  32.     public boolean setMinutes(int minutes) {
  33.         if (minutes <= min_value || minutes > max_minutes) {
  34.             this.minutes = min_value;
  35.             return false;
  36.         } else {
  37.             this.minutes = minutes;
  38.             return true;
  39.         }
  40.     }
  41.  
  42.     public int getHours() {
  43.         return hours;
  44.     }
  45.  
  46.     public int getMinutes() {
  47.         return minutes;
  48.     }
  49.  
  50.     /***
  51.      *
  52.      * getFormat and setFormat will be static because of the parameter "format" is static
  53.      */
  54.     public static boolean setFormat(int format) {
  55.         if (format != 12 && format != 24) {
  56.             Clocker.format = 0;
  57.             return false;
  58.         }
  59.         Clocker.format = format;
  60.         return true;
  61.     }
  62.  
  63.     public static String getFormat() {
  64.         return format == 12 ? "am:pm format" : "24 hours format";
  65.     }
  66.  
  67.     @Override
  68.     public String toString() {
  69.         if (format == 12) {
  70.             return (hours < 10 ? "0" : "") + (hours == 12 ? hours : hours % 12) + ":" + (minutes < 10 ? "0" : "") + minutes + (hours < 12 ? "am" : "pm");  //in case of am:pm format it will write am or pm in the end of the string
  71.         }else {
  72.             return (hours < 10 ? "0" : "") + hours + ":" +(minutes < 10 ? "0" : "")+ minutes; // if an hour is until 10 it will write 0 at
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. =======================================================================================================================================
  80. import java.util.*;
  81. import MyClock.*;
  82. public class objectsClassesFirstLesson {
  83.     public static void main(String[] args) {
  84.         Scanner sc = new Scanner(System.in);
  85.  
  86.         System.out.println("Plese write 12 for am:pm format or 24 for 24 format:");
  87.        
  88.         //we can use format before we do any object of class
  89.         Clocker.setFormat(sc.nextInt());
  90.        
  91.         // new object of class
  92.         Clocker shaon = new Clocker(12, 30);
  93.        
  94.         // see it prints right
  95.         System.out.println(shaon + " " + shaon.getFormat());
  96.        
  97.         // using cloner constructor
  98.         Clocker chas = new Clocker(shaon);
  99.        
  100.         // change the time of first object
  101.         shaon.setHours(0);
  102.         shaon.setMinutes(56);
  103.        
  104.         // checking cloner constructor works
  105.         System.out.println(shaon + " " + shaon.getFormat());
  106.         System.out.println(chas + " " + chas.getFormat());
  107.     }
  108. }
Add Comment
Please, Sign In to add comment