Program to test all the methods in class definition


Discuss the following:

Q: 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.

Hint:

The CounterDemo class assumes that the Counter class will have methods named resetToZero(), increment(), decrement(), and getValue(). Make sure that you implement these methods.

If the output looks like:

Initial value is 0
After two increments, value is Counter@cf8583
After one decrement, value is Counter@cf8583
Result of calling counter.output() :
Counter value is 1
Counter@cf8583 equals Counter@4693c7? false
Counter@cf8583 equals Counter@4693c7? true
After resetting to zero, value is Counter@4693c7

then you have not properly defined a method named toString() that returns a string representation of the Counter's value. The strange output (e.g., "Counter@4693c7") nis produced by the default implementation of toString() in nclass java.lang.Object. n

The getValue(), equals(...), and toString() methods must be declared to return a value, and must include a return statement in the body of the method. The other methods should be declared with a return type of void. In this project, these methods should not include a return statement.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Program to test all the methods in class definition
Reference No:- TGS01935051

Now Priced at $25 (50% Discount)

Recommended (99%)

Rated (4.3/5)