Object-Oriented Programming with Java, part I – Exercise 89

In this exercise we create the Clock class and implement the previously created (exercise 78) BoundedCounter class within it.

My Code

Main

public class Main {
    public static void main(String[] args) {
        Clock clock = new Clock(23, 59, 50);

        int i = 0;
        while (i < 20) {
            System.out.println(clock);
            clock.tick();
            i++;
        }
    }
}

Clock

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
        this.hours = new BoundedCounter(23) ;
        this.minutes = new BoundedCounter(59);
        this.seconds = new BoundedCounter(59);
        this.hours.setValue(hoursAtBeginning);
        this.minutes.setValue(minutesAtBeginning);
        this.seconds.setValue(secondsAtBeginning);
    }
    
    public void tick() {
        // Clock advances by one second
        this.seconds.next();
        if (this.seconds.getValue() == 0){
            this.minutes.next();
            if (this.minutes.getValue() == 0){
            this.hours.next();
        }
        }
        
    }
    
    public String toString() {
        // returns the string representation
        return "" + this.hours +":"+ this.minutes +":"+ this.seconds;
    }
}

BoundedCounter


public class BoundedCounter {

    private int value;
    private int upperLimit;

    public BoundedCounter(int upperLimit) {
        // write code here
        this.upperLimit = upperLimit;
        this.value = 0;
    }

    public void next() {
        // write code here
        if (this.value >= this.upperLimit) {
            this.value = 0;
        } else {
            this.value++;
        }
    }

    public int getValue() {
        // write here code that returns the value
        return this.value;
    }

    public void setValue(int setValue) {
        // write here code that returns the value
       if ((setValue >= 0) && (setValue <= this.upperLimit)){
           this.value = setValue;
       }
    }
    
    public String toString() {
        // write code here
        if (this.value < 10) {
            return "0" + this.value;
        } else {
            return "" + this.value + "";
        }
    }
}

Model Code

Main

public class Main {

    public static void main(String[] args) {

        Clock clock = new Clock(23, 59, 50);

 

        int i = 0;

        while (i < 20) {

            System.out.println(clock);

            clock.tick();

            i++;

        }

    }

}

Clock

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

        hours = new BoundedCounter(23);

        minutes = new BoundedCounter(59);

        seconds = new BoundedCounter(59);

        

        hours.setValue(hoursAtBeginning);

        minutes.setValue(minutesAtBeginning);

        seconds.setValue(secondsAtBeginning);

    }

    

    public void tick() {

        // Clock advances by one second

        seconds.next();

        if (seconds.getValue() == 0) {

            minutes.next();

            if (minutes.getValue() == 0) {

                hours.next();

            }

        }

    }

    

    public String toString() {

        // returns the string representation

        return hours.toString() + ":" + minutes.toString() + ":" + seconds.toString();

    }

}

BoundedCounter

public class BoundedCounter {

    // copy here the class BoundedCounter from last weeks assignment 78 

 

    private int value;

    private int limit;

 

    public BoundedCounter(int limit) {

        this.limit = limit;

        this.value = 0;

    }

 

    public void next() {

        if (this.value == this.limit) {

            this.value = 0;

        } else {

            this.value++;

        }

    }

 

    public int getValue() {

        return this.value;

    }

 

    public void setValue(int value) {

        if (value < 0 || value > this.limit) {

            return;

        }

 

        this.value = value;

    }

 

    @Override

    public String toString() {

        String initialZero = "0";

        if (this.value > 9) {

            initialZero = "";

        }

        return initialZero + this.value;

    }

}

Comments

Goes to show just how quickly you can forget how the code you previously wrote works…..the importance of comments.

Leave a comment