Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------- Clock.java ------------------------
- package cls;
- public class Clock {
- //Instance variables
- private int hours;
- private int minutes;
- final int MAX_HOURS = 24;
- final int MAX_MINUTES = 60;
- //Static variable
- private static boolean format24 = true;
- public Clock(int hours, int minutes) {
- setHours(hours);
- setMinutes(minutes);
- }
- public Clock(Clock clock) {
- this.hours = clock.hours;
- this.minutes = clock.minutes;
- }
- public int getHours() {
- return hours;
- }
- public void setHours(int hours) {
- if(this.minutes > MAX_HOURS) {
- System.out.println("Hours cannot be more then 24");
- } else {
- this.hours = hours;
- }
- }
- public int getMinutes() {
- return minutes;
- }
- public void setMinutes(int minutes) {
- if(this.minutes > MAX_MINUTES) {
- System.out.println("Minutes cannot be more then 60");
- } else {
- this.minutes = minutes;
- }
- }
- public static boolean isFormat24() {
- return format24;
- }
- public static void setFormat24(boolean format24) {
- Clock.format24 = format24;
- }
- @Override
- public String toString() {
- int h = format24 ? this.hours : this.hours % 12;
- String m = (minutes < 10) ? "0" + this.minutes : "" + this.minutes;
- return (h < 10) ? "0" + h + ":" + m : h + ":" + m;
- }
- }
- //------------------------- Tester class -------------------------------
- package tester;
- import cls.Clock;
- public class ClockTester {
- public static void main(String[] args) {
- Clock clock = new Clock(15, 40);
- System.out.println(clock.toString());
- clock.setHours(40);
- Clock.setFormat24(false);
- System.out.println(clock.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement