Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called \"Counter\" that represents a simple tally counter, which m

ID: 3795604 • Letter: W

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 class should contain a single integer as instance data, representing the counter. Write a constructor to initialize the counter to zero. Write a method called "click" that increments the counter and another method called "getCount" 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.

Explanation / Answer

The Counter Class

Here is the driver class - CounterTest

public class CounterTest

{

   public static void main(String[] args)

   {

      //Declaration

      Counter count1 = new Counter();

      Counter Count2 = new Counter();

      int sum;

      //Adds 1 to objects count1 and count2

      count1.click();

      //Print the current counts

      System.out.println("The current count for count1 is : " + count1 + " and for count2 is : " +    count2);

  //Add the sum of count1 and count2.

      sum = count1.getCount() + count2.getCount();

      //Dispalys the sum of both.

      //Resets the value of both count1 and count2.

      count1.reset();

count2.reset();

      System.out.println("The value of both when they are reset is: " + count1 +

             " and " + count2);

}

}

01 public class Counter
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote