User Tools

Site Tools


cs175:cs-175_sp13_counter_class

Programming Lab Exercise

53029_tallycounter-500x500.jpg

Counter2.java, TestCounter2.java

Define a class called Counter2. An object of this class is used to count things, so it records a count that is a non-negative whole number. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Also include an accessor method that returns the current count value, as well as a method that displays the count on the screen. Do not define an input method. The only method that can set the counter is the one that sets it to zero. Write a program (TestCounter2.java) to test your class definition.

Notes:

Use the “private” access modifier for the Counter class instance variable.

Use these names for the Counter class methods:

   resetCount    - set the counter to 0
   increaseCount - to increase the count by 1
   decreaseCount - to decrease the count by 1
   getCount      - accessor method that *returns* the current count value
   showCount     - method that displays the count on the screen

Write the increaseCount method so that if the count exceeds 9999, the count rolls over and becomes 0 again.

Be sure that no method allows the value of the counter to become negative.

In TestCounter2.java do the following:

   - Declare a Counter class object variable called mycount.
   - Reset mycount.
   - Increase mycount 3 times.
   - Show mycount. (Output should be: "Current value of count is 3")
   - Decrease mycount 4 times.
   - Show mycount. (Output should be: "Current value of count is 0")
   - Increase mycount 9999 times.
   - Show mycount. (Output should be: "Current value of count is 9999")
   - Increase mycount 1 time.
   - Show mycount. (Output should be: "Current value of count is 0")

cs175/cs-175_sp13_counter_class.txt · Last modified: 2018/01/23 14:48 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki