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

This exercise builds a simple clock like counter with seconds, minutes and hours created from the class.

My Code

Main


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        // write here code to ensure that BoundedCounter works as expected
        // before starting 78.3 remove the extra code and use the skeleton shown
        // in the assignment
        BoundedCounter seconds = new BoundedCounter(59);
        BoundedCounter minutes = new BoundedCounter(59);
        BoundedCounter hours = new BoundedCounter(23);

        System.out.print("seconds: ");
        int s = Integer.parseInt( reader.nextLine() );// read the initial value of seconds from the user
        System.out.print("minutes: ");
        int m = Integer.parseInt( reader.nextLine() );// read the initial value of minutes from the user
        System.out.print("hours: ");
        int h = Integer.parseInt( reader.nextLine() );// read the initial value of hours from the user

        seconds.setValue(s);
        minutes.setValue(m);
        hours.setValue(h);

        int i = 0;
        while ( i < 121 ) {
            System.out.println( hours + ":" + minutes + ":" + seconds);   // the current time printed
            seconds.next();
            // advance minutes
            if (seconds.getValue() == 0){
                minutes.next();
            }
            // if minutes become zero, advance hours
            if ((minutes.getValue() == 0) && (seconds.getValue() == 0)){
                hours.next();
            }
            i++;
        }

    }
}

BoundedCounter

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author masonbee
 */
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

import java.util.Scanner;

 

public class Main {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        // write here code to ensure that BoundedCounter works as expected

        // before starting 78.3 remove the extra code and use the skeleton shown

        // in the assignment

 

        BoundedCounter seconds = new BoundedCounter(59);

        BoundedCounter minutes = new BoundedCounter(59);

        BoundedCounter hours = new BoundedCounter(23);

 

        System.out.print("seconds: ");

        int sek = Integer.parseInt(reader.nextLine());

        System.out.print("minutes: ");

        int min = Integer.parseInt(reader.nextLine());

        System.out.print("hours: ");

        int hou = Integer.parseInt(reader.nextLine());

 

        seconds.setValue(sek);

        minutes.setValue(min);

        hours.setValue(hou);

 

        int i = 0;

        while (i < 121) {

            System.out.println(hours + ":" + minutes + ":" + seconds);

            seconds.next();

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

                minutes.next();

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

                    hours.next();

                }

            }

 

            i++;

        }

    }

}

 



BoundedCounter

public class BoundedCounter {

 

    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

It works fine but my solution to advance the clock at the end of main is not nearly as nice as theirs.

Leave a comment