1

I was coding a program using Java for a clock that is able to "tick" but there are problems with it. I assume it has something to do with the getter and setters, or the toString() method.

counter class

package clock;

public class Counter 
{
private int _count;
private String _name;

public String getName(){return _name;}
public void setName(String _name){this._name = _name;}

public int getCount(){return _count;}
public void setCount(int _count){this._count = _count;}

public Counter(String name)
{
    _name = name;
    _count = 0;
}

public void Increment()
{
    _count++;
}

public void Reset()
{
    _count = 0;
}
}

clock class

package clock;

import java.util.Calendar;


public class Clock {
private Counter _secCounter;
private Counter _minCounter;
private Counter _hrCounter;

public Clock()
{
    this._secCounter = new Counter("Seconds");
    this._minCounter = new Counter("Minutes");
    this._hrCounter = new Counter("Hour");

  Calendar currentTime = Calendar.getInstance();
  _secCounter.setCount(currentTime.get(Calendar.SECOND));
  _minCounter.setCount(currentTime.get(Calendar.MINUTE));
  _hrCounter.setCount(currentTime.get(Calendar.HOUR));
}

public Counter SecCounter(){return _secCounter;}
public Counter MinCounter(){return _minCounter;}
public Counter HrCounter(){return _hrCounter;}

public String Tick()
{
    _secCounter.Increment();
    if (_secCounter.getCount() == 60)
    {
        _secCounter.Reset();
        _minCounter.Increment();
    }

    if (_minCounter.getCount() == 60)
    {
        _minCounter.Reset();
        _hrCounter.Increment();
    }

    if (_hrCounter.getCount() == 24)
    {
        _hrCounter.Reset();
    }

    return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString();
}

@Override
public String toString()
{
    return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString();
}  
}

Main Class

package clock;

public class Main {

public static void main(String[] args) 
{
    Clock myClock = new Clock();
    for (int i = 0; i < 10; i++)
    {

        String currentTime = myClock.Tick();
        System.out.println(currentTime);
        i = 0;
    }


}
}

It outputs

clock.Counter@5c647e05:clock.Counter@33909752:clock.Counter@55f96302

Kinda new to Java and was translating the code from C#. Thanks for the help!

Corey
  • 15,524
  • 2
  • 35
  • 68
Wing Hang Khoo
  • 41
  • 2
  • 10
  • 3
    Please override toString method in the clock and tick classes [check this](https://www.javatpoint.com/understanding-toString()-method) – Ami May 31 '17 at 04:20
  • You have to override `toString` method in `Counter` class – Jay Smith May 31 '17 at 04:29

2 Answers2

1

As far as I can tell, you need to call _**Counter.getCount(), not toString(). You never overwrote the toString method, and so to get your clock's value back you need to use the method you wrote for getting the counter's value. If you want it to report it's name as well, you'll need to overwrite the toString() method in Counter to return something like return getName() + ": " + getCount();

Greg S.
  • 48
  • 8
  • In case it wasn't clear, what I mean by _**Counter is the name of the counter. The counters all followed that name pattern, so I went with it. – Greg S. Jun 01 '17 at 02:41
0

I recommend using java.util.Timer to execute Clock.Tick() method every 1 second.

public static void main(String[] args) {
        Clock myClock = new Clock();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                String currentTime = myClock.Tick();
                System.out.println(currentTime);
            }
        }, 0, 1000);
    }

You first create new Timer instance. Call its schedule method to make TimerTask run every 1000 milliseconds with initial delay 0.

Jay Smith
  • 2,331
  • 3
  • 16
  • 27