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

Must be an original java code written by you. Not copied and pasted please. Also

ID: 3552265 • Letter: M

Question

Must be an original java code written by you. Not copied and pasted please. Also please include /**comments**/ in this format. Hints are below. This is how it should go.


Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There should be no input method or other mutator methods. The only method that can set the counter is the one that sets it to zero. Also, include a toString method and an equals method. Write a program (or programs) to test all the methods in your class definition.


Hints

/**

Demo program that exercises the methods of the Counter class

*/

public class CounterDemo

{

public static void main(String[] args)

{

// Make a new counter

Counter counter = new Counter();

System.out.println("Initial value is " + counter.getValue());

// Test the increment and toString() methods.

counter.increment();

counter.increment();

System.out.println( "After two increments, value is " + counter.toString());

// Test the decrement method

counter.decrement();

System.out.println("After one decrement, value is " + counter);

// Test the output() method

System.out.println("Result of calling counter.output() :");

counter.output();

// Make a second counter to test equals.

Counter counter2 = new Counter();

System.out.println(counter + " equals " + counter2 + "? " +

counter.equals(counter2));

Explanation / Answer

Dear,


public class CounterDemo   
{
    public static void main(String[] args)
    {
        Counter counter = new Counter();
        System.out.println("Initial value is " + counter.getValue());

       

//        Test the increment and toString() methods.

        counter.increment();

        counter.increment();

        System.out.println( "After two increments, value is " + counter.toString());

       

//        Test the decrement method

        counter.decrement();

        System.out.println("After one decrement, value is " + counter);

       

//        Test the output() method

        System.out.println("Result of calling counter.output() :");

        counter.output();

       

//        Make a second counter to test equals.

        Counter counter2 = new Counter();

        System.out.println(counter + " equals " + counter2 + "? " +

        counter.equals(counter2));
    }
}

class Counter
{
    private int value;
   
   public Counter()
   {
       value = 0;
   }
    // constructor initializes data
   public Counter(int x)
   {
     setValue(x);
    }
   public void setValue(int x)
   {
      value=(x<0?0:x);
    }
    public int getValue()
    {
      return value;
    }
    public void increment()
   {
        value=value+1;
    }
    public void decrement()
    {
        if(value-1>=0)
        value=value-1;
    }
    public String toString()
    {
        return value+" ";
    }
    public boolean isEqual(Counter c)
    {
        if(value==c.getValue())
            return true;
        else
            return false;
    }
    public void output()
    {
        System.out.println("Counter value is: "+value);
    }
   
}

Sample Run:


Initial value is 0
After two increments, value is 2
After one decrement, value is 1
Result of calling counter.output() :
Counter value is: 1
1 equals 0 ? false