Define a class called Counter whose objects count things. An object of this clas
ID: 3630528 • Letter: D
Question
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 will 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.This is what I have:
**
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));
// Increment counter2 so that they are equal
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Reset counter2 to zero
counter2.resetToZero();
System.out.println("After resetting to zero, value is " + counter2);
}
}
/**
Class that counts things.
*/
class Counter {
/**
Current value of the counter
*/
private int value = 0;
Explanation / Answer
Way 1:You didn't define an 'equals' method in your 'Counter' class. Doing so will solve your problem. Without that you are getting the 'equals' method defined on the 'Object' class, which will only return true when called on identical objects (the same instance). This is a common source of misunderstanding in Java.
EDIT: Here is a definition of the 'equals' method for the 'Counter' class that makes your program work:
public boolean equals(Object o) { return ((Counter)o).getValue() == value; }
What that leaves out is first making sure the passed object 'o' is really of the class 'Counter'. If not it should return false.
Way2:
Overloading the equals() method is a bit different:
private boolean equals (Counter counter_two) {
if (value == counter_two.getValue())
return true;
return false;
}
That should work since all you are comparing are the two values. I also based that method on the code that you had first posted, where 'value' was a private variable rather than public as it is in your latest bit of code. If you keep it public, counter_two.value can replace counter_two.getValue().
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.