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

Define a class called Counter. An object of this class is used to count things.

ID: 3687425 • Letter: D

Question


Define a class called Counter. An object of this class is used to count things. So it records a count that is a nonnegative whole number.

Include methods to set the counter to 0, to increase the count by a number passed in via a variable, and to decrease the count by a number passed in via a variable. Be sure that no method allows the value of the counter to become negative.

Include an access method that returns the current count value, as well as a toString() method that displays the count on the screen. Do not define an input method that sets the counter.

The only method that can set the counter is the one that sets it to zero. Write a program to test your class definition (Hint: you need only one instance variable).

Each method in the class should contain documentation as to its function.

You need two different methods, and accessor method [getXXXXX()] and toString(). The accessor method will return the value of your counter instance variable. The toString method will return a String, which will be used to display the information, any way you want it.

The toString method returns a String, which can be used to display the information anyway you want it.

Counter    ctrObject = new Counter();

System.out.println( ctrObject );

is the same as calling

System.out.println( ctrObject.toString());

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Counter {

   int counter;

   /**
   * constructor to set counter as 0
   */
   public Counter() {
       // TODO Auto-generated constructor stub
       counter = 0;
   }

   /**
   * returns counter
   *
   * @return
   */
   public int getCounter() {

       return counter;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Counter [counter=" + counter + "]";
   }

   /**
   * method to increase the counter by a count
   *
   * @param count
   */
   public void increaseCounter(int count) {
       this.counter += count;

   }

   /**
   * method to decrease the counter by a count
   *
   * @param count
   */
   public void decreaseCounter(int count) {
       if ((this.counter - count) >= 0)
           this.counter -= count;

   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       Counter ctrObject = new Counter();
       System.out.println(ctrObject);
       ctrObject.decreaseCounter(12);
       System.out.println("After Decrease 12:" + ctrObject);

       ctrObject.increaseCounter(12);
       System.out.println("After Increase 12:" + ctrObject);

       ctrObject.decreaseCounter(10);
       System.out.println("After Decrease 10:" + ctrObject);

   }

}

OUTPUT:

Counter [counter=0]
After Decrease 12:Counter [counter=0]
After Increase 12:Counter [counter=12]
After Decrease 10:Counter [counter=2]

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