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: 3685821 • 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.

Checklist

------

single instance variable
increment method
decrement method
toString display method
retrieve method

------

Explanation / Answer

public class Counter
{
public static void main(String[] args)
{
Counter counter = new Counter();
System.out.println("Initiale is " + counter.getValue());
counter.increment();
counter.increment();
System.out.println("After two increments, value is " + counter);
counter.decrement();
System.out.println("Afterdecrement, value is " + counter);
System.out.println("Resultalling counter.output() :");
counter.output();
Counter counter2 = new Counter();
System.out.println(counter + " equals " + counter2 + "? " +counter.equals(counter2));
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
counter2.resetToZero();
System.out.println("Aftertting to zero, value is " + counter2);
}
}
Class counter
{
public int value = 0;
public Counter(){
this.value = 0;
}
public int getValue()
{
return value;
}
public int increment()
{
return value++;
}
public int decrement()
{
return value--;
}
public int resetToZero()
{
return value=0;
}
public String toString()
{
return Integer.toString(value);
}
public void output()
{
System.out.println("Counter value is " + value);
}

}

Updated code

public class Counter
{
private int count;
public Counter(int aCount)
{
count = aCount;
}
public void setCountToZero()
{
count = 0;
}
public void addOneToCount()
{
count += 1;
}
public void subtractOneFromCount()
{
if (count == 0 )
return;
count--;
}
public void displayCount()
{
System.out.println("The count is " + count);
}
}

here you need another cass to exec counter class


public class CounterTest
{
public static void main(String[] args)
{
Counter myCounter = new Counter(10);
myCounter.displayCount();
myCounter.addOneToCount();
myCounter.displayCount();
myCounter.subtractOneFromCount();
myCounter.displayCount();
myCounter.setCountToZero();
myCounter.displayCount();
}
}

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