Example: tally counter

Problem: find out how many people attend a concert or board a bus.

Features: 
1. Whenever the operator pushes a button, the counter value advances by one. (count method)
2. A physical counter has a display to show the current value.  (getValue method)

Counter tally = new Counter();

tally.count();

tally.count();

int result = tally.getValue(); // Sets result to 2

 

3. Each counter needs to store a variable that keeps track of how many times the counter has been advanced

Instance variables store the data of an object
Instance of a class:
an object of the class
The class declaration specifies the instance variables:


Each object of a class has its own set of instance variables
You should declare all instance variables as private

Supply the body of a method public void reset() that resets the counter back to zero

Answer:
public void reset()
{
   
value = 0;
}