Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////MAIN CLASS ////////////////////////
- public class ClockView {
- public static void main(String[] args) {
- Clock clock = new Clock();
- clock.setHours(5);
- clock.setMinutes(4);
- clock.setSeconds(4);
- clock.show();
- clock.tick();
- clock.tick();
- clock.show();
- clock.reset();
- clock.show();
- }
- }
- ///////////////////// Clock Class ////////////////////////
- public class Clock {
- // Variables declaration
- private int hours, minutes, seconds;
- // Methods
- public int getHours() {
- return hours;
- }
- public int getMinutes() {
- return minutes;
- }
- public int getSeconds() {
- return seconds;
- }
- public boolean setHours(int h) {
- if(h < 0 || h > 23) {
- hours = 0;
- System.out.println("Hours should be between 0 and 59!");
- return false;
- }
- hours = h;
- return true;
- }
- public boolean setMinutes(int m) {
- if(m < 0 || m > 59) {
- minutes = 0;
- System.out.println("Minutes should be between 0 and 59!");
- return false;
- }
- minutes = m;
- return true;
- }
- public boolean setSeconds(int s) {
- if(s < 0 || s > 59) {
- seconds = 0;
- System.out.println("Seconds should be between 0 and 59!");
- return false;
- }
- seconds = s;
- return true;
- }
- public void tick() {
- seconds += 1;
- minutes += seconds / 60;
- hours += minutes / 60;
- seconds %= 60;
- minutes %= 60;
- hours %= 24;
- }
- public void show() {
- printElement(hours);
- printElement(minutes);
- if(seconds < 10) {
- System.out.print("0");
- }
- System.out.println(seconds);
- }
- public void reset() {
- hours = minutes = seconds = 0;
- }
- private void printElement(int n) {
- if(n < 10) {
- System.out.print("0");
- }
- System.out.print(n + ":");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement