Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package clocl;
- import org.omg.Messaging.SyncScopeHelper;
- public class Clock {
- private int hour, minutes;
- private final int maxHour = 24, maxMinutes = 60;
- private static int format;
- public Clock(int h, int m) {// constructor
- if (h < 0 || h >= this.maxHour) {// checking if hour input is correct
- this.hour = 0;
- } else {
- this.hour = h;
- }
- if (m < 0 || m >= this.maxMinutes) {// checking if minutes input is correct
- this.minutes = 0;
- } else {
- this.minutes = m;
- }
- }
- public Clock(Clock other) {// copy constructor
- this.hour = other.hour;
- this.minutes = other.minutes;
- }
- public int getHour() {
- return hour;
- }
- public boolean setHour(int h) {
- if (h < 0 || h >= this.maxHour) {// checking if hours input is correct
- this.hour = 0;
- return false;
- } else {
- this.hour = h;
- return true;
- }
- }
- public int getMinutes() {
- return minutes;
- }
- public boolean setMinutes(int m) {
- if (m < 0 || m >= this.maxMinutes) {// checking if seconds input is correct
- this.minutes = 0;
- return false;
- } else {
- this.minutes = m;
- return true;
- }
- }
- public static int getFormat() {
- return format;
- }
- public static boolean setFormat(int f) {// setting the clock format
- if (f != 12 && f != 24) {
- return false;
- } else {
- Clock.format = f;
- return true;
- }
- }
- public String toString() {// method for showing the time
- String res = "";
- if (Clock.format == 24) {
- if (this.hour < 10) {
- res = "0";
- }
- res += this.hour + ":";
- if (this.minutes < 10) {
- res += "0";
- }
- res += this.minutes;
- return "The hour is " + res;
- } else {
- if (this.hour < 10) {
- res = "0";
- }
- res += (hour == 12 ? hour : hour % 12) + ":";
- if (this.minutes < 10) {
- res += "0";
- }
- res += this.minutes;
- return "The hour is " + res;
- }
- }
- }
- ------------------------------------------------------------------------------------------------------
- package clocl;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Clock c1 = new Clock(11, 51);// creating new clock obj
- Clock c2 = new Clock(c1);// creating copy obj
- Scanner s = new Scanner(System.in);
- boolean res;
- do {
- System.out.println("Enter the hour format you wish:(12/24) ");
- res = Clock.setFormat(s.nextInt());
- } while (res != true);// as long as the format isnt correct
- System.out.println(c1);// print result
- System.out.println(c2);// print result
- c1.setHour(13);// setting the hour
- c1.setMinutes(40);// setting the minutes
- System.out.println(c1);// print result
- System.out.println(c2);// print result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment