Ask Question
19 September, 06:41

Write a class called Counter that represents a simple tally counter, which might be used to count people as they enter a room. The Counter class should contain a single integer as instance data, representing the count. Write a constructor to initialize the count to zero. Write a method called click that increments the count and another method called get Count that returns the current count. Include a method called reset that resets the counter to zero. Finally, create a driver class called CounterTest that creates two Counter objects and tests their methods.

+3
Answers (1)
  1. 19 September, 07:11
    0
    Code is in java

    Explanation:

    Code is self explanatory, added come comments to explain more about the code.

    public class CounterDriver {

    public static void main (String[] args) {

    / / Creating counter first object

    Counter counter1 = new Counter ();

    //performing 2 clicks

    counter1. click ();

    counter1. click ();

    / / displaying counter 1 click count

    System. out. println ("Total Number of counter1 clicks : "+counter1. getCount ());

    / / resetting counter 1 click count

    counter1. reset ();

    / / again displaying click count which will be 0 after reset

    System. out. println ("Total Number of counter1 clicks : "+counter1. getCount ());

    / / Same operation goes with counter 2

    / / the only difference is that it performs 3 clicks

    Counter counter2 = new Counter ();

    counter2. click ();

    counter2. click ();

    counter2. click ();

    System. out. println ("Total Number of counter2 clicks : "+counter2. getCount ());

    counter2. reset ();

    System. out. println ("Total Number of counter2 clicks : "+counter2. getCount ());

    }

    }

    class Counter{

    int count;

    / / defining constructor which will initialize count variable to 0

    public Counter () {

    this. count=0;

    }

    / / adding click method to increment count whenever it's called

    public void click () {

    this. count++;

    }

    / / getCount method will return total count

    public int getCount () {

    return this. count;

    }

    / / reset method will set count to 0

    public void reset () {

    this. count = 0;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a class called Counter that represents a simple tally counter, which might be used to count people as they enter a room. The Counter ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers