Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Static, Exercise 01, slide 24
- class Tester
- *****************************************************************************
- public class Tester {
- public static void main(String[] args) {
- // create two clocks
- Clock c1 = new Clock(23, 59);
- Clock c2 = new Clock(c1);
- c2.setHours(11, Clock.AmPm.AM);
- // display clocks on format 24 hours
- System.out.println("Dispaly in 24 hours format:");
- System.out.println("c1 = " + c1);
- System.out.println("c2 = " + c2);
- // change display format to 12 hours
- Clock.displayFormat = TimeFormat.TF12;
- // display clocks on format 12 hours
- System.out.println("Dispaly in 12 hours format:");
- System.out.println("c1 = " + c1);
- System.out.println("c2 = " + c2);
- // clocks tick
- System.out.println("after one tick:");
- c1.tick();
- c2.tick();
- System.out.println("c1 = " + c1);
- System.out.println("c2 = " + c2);
- // clocks tick
- System.out.println("after one tick:");
- c1.tick();
- c2.tick();
- System.out.println("c1 = " + c1);
- System.out.println("c2 = " + c2);
- // change display format to 24 hours
- Clock.displayFormat = TimeFormat.TF24;
- // display clocks on format 24 hours
- System.out.println("Dispaly in 24 hours format:");
- System.out.println("c1 = " + c1);
- System.out.println("c2 = " + c2);
- }
- }
- // class Clock
- *****************************************************************************
- public class Clock {
- // class attributes, shared to all class instances
- // should be initialized even before calling constructor
- private static final int DAY_HOURS = 24;
- private static final int HOUR_MINUTES = 60; // maximum minutes in one hour
- public static TimeFormat displayFormat = TimeFormat.TF24; // time format
- // for
- public static enum AmPm {
- AM, PM
- };
- // instance attributes, always save data in format 24 hours
- private int hours; // clock hours, always saved data in 24 format
- private int minutes; // clock minutes
- // private AmPm meridiem; // AM or PM : indicated by hours
- // constructor, arguments in 12 hours format
- public Clock(int hours, int minutes, AmPm meridiem) {
- this(toFormat24(hours, meridiem), minutes);
- }
- // constructor, arguments in 24 hours format
- public Clock(int hours, int minutes) {
- if (!this.setHours(hours)) {
- this.hours = TimeFormat.TF24.minHour;
- }
- this.setMinutes(minutes);
- }
- // copy constructor
- public Clock(Clock otherClock) {
- this.hours = otherClock.hours;
- this.minutes = otherClock.minutes;
- }
- // getter and setter for minutes
- public int getMinutes() {
- return this.minutes;
- }
- private boolean setMinutes(int minutes) {
- if (minutes < 0 || minutes >= HOUR_MINUTES)
- return false;
- this.minutes = minutes;
- return true;
- }
- // getter and setter for hours
- public int getHours() {
- return this.hours;
- }
- public boolean setHours(int hours) {
- if (hours < TimeFormat.TF24.minHour || hours > TimeFormat.TF24.maxHour)
- return false;
- this.hours = hours;
- return true;
- }
- public boolean setHours(int hours, AmPm meridiem) {
- return setHours(toFormat24(hours, meridiem));
- }
- private static int toFormat24(int hour, AmPm meridiem) {
- return meridiem == AmPm.PM ? (hour + 12) % DAY_HOURS : hour;
- }
- private static int toFormat12(int hour) {
- if (hour == 0)
- return displayFormat.maxHour;
- if (hour > displayFormat.maxHour)
- return hour - displayFormat.maxHour;
- return hour;
- }
- private String getMeridiem() {
- if (this.hours == 0)
- return minutes == 0 ? "midnight" : "AM";
- if (hours < displayFormat.maxHour)
- return "AM";
- if (hours == displayFormat.maxHour)
- return minutes == 0 ? "noon" : "PM";
- return "PM";
- }
- public void tick() {
- this.minutes += 1;
- if (minutes == HOUR_MINUTES) {
- minutes = 0;
- hours += 1;
- if (hours > TimeFormat.TF24.maxHour)
- hours = TimeFormat.TF24.minHour;
- }
- }
- @Override
- public String toString() {
- if (displayFormat == TimeFormat.TF24)
- return String.format("%02d:%02d", this.hours, this.minutes);
- else {
- return String.format("%02d:%02d (%s)", toFormat12(this.hours),
- this.minutes, getMeridiem());
- }
- }
- }
- enum TimeFormat
- **************************************************************************************
- public enum TimeFormat {
- TF12(1, 12), TF24(0, 23);
- public final int minHour;
- public final int maxHour;
- private TimeFormat(int minHour, int maxHour) {
- this.minHour = minHour;
- this.maxHour = maxHour;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement