Java

OOP GUIDE / WORK

 

COUNTER CLASS

 

Topics

  • Simple class design, Using objects

 

 

TASK – PART 1 – COUNTER CLASS

 

You are to design and create a Counter class.  It allows us to create Counter objects that will have a count value.  That value will always be initially set to zero. 

 

The expected functionality is as follows:

 

  • One can increment the counter by using the inc() method.

 

  • One can decrement the counter by using the dec() method.

 

  • One can reset the counter by using the reset() method.

 

  • One can use the toString() method to get the value of the count inside the counter.

 

 

TASK – PART 2 – TESTING YOUR COUNTER CLASS

 

Inside the main function of a class named CounterTester, add the statements that do the following:

 

  • Create a Counter object.
  • Output the Counter to screen.
  • Increment the Counter object.
  • Output the Counter to screen.
  • Increment the Counter object twice.
  • Output the Counter to screen.
  • Decrement the Counter object.
  • Output the Counter to screen.
  • Reset the Counter object.
  • Output the Counter to screen.
  • Create a for loop that will loop ten times.  Inside the loop, increment the counter.
  • After the loop, output the counter to screen.