Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BoundedCounter {
- private int value;
- private int upperLimit;
- public BoundedCounter(int upperLimit) {
- this.upperLimit = upperLimit;
- this.value = 0;
- }
- public void next() {
- if (this.value < this.upperLimit) {
- this.value++;
- } else {
- this.value = 0;
- }
- }
- public String toString() {
- if (this.value < 10) {
- return "0" + this.value;
- } else {
- return "" + this.value;
- }
- }
- public int getValue() {
- return this.value;
- }
- public void setValue(int number) {
- if (number > 0 && number <= this.upperLimit) {
- this.value = number;
- }
- }
- }
- -----------------------------
- public class Clock {
- private BoundedCounter hours;
- private BoundedCounter minutes;
- private BoundedCounter seconds;
- public Clock(int hoursAtBeginning, int minutesAtBeginning, int secondsAtBeginning) {
- // the counters that represent hours, minutes and seconds are created and set to have the correct initial values
- BoundedCounter hours = new BoundedCounter(23);
- hours.setValue(hoursAtBeginning);
- BoundedCounter minutes = new BoundedCounter(59);
- minutes.setValue(minutesAtBeginning);
- BoundedCounter seconds = new BoundedCounter(59);
- seconds.setValue(secondsAtBeginning);
- //System.out.println(hours + " " + minutes + " " + seconds);
- }
- public void tick() {
- seconds.setValue(seconds.getValue() + 1);
- }
- public String toString() {
- return hours + ":" + minutes + ":" + seconds;
- }
- }
- -------------------------------------
- public class Main {
- public static void main(String[] args) {
- Clock clock = new Clock(23, 59, 50);
- System.out.println(clock);
- // int i = 0;
- // while (i < 20) {
- // System.out.println(clock);
- // clock.tick();
- // i++;
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement