cs175:implementing_classes_lab_1
Programming Lab Exercise
- Topics: Implementing Classes
Counter.java, TestCounter.java
In DrJava, define a class called Counter in Counter.java.
An object of this class is used to count things, so it records a count that is a non-negative whole number.
In the Counter class, include methods to set the counter to 0 (reset), 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 using System.out.println.
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 value of the count
In TestCounter.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 -1") - Reset mycount again. - Show mycount. (Output should be: "Current value of count is 0")
Links to source code: Counter.java, TestCounter.java
cs175/implementing_classes_lab_1.txt · Last modified: 2014/10/14 20:28 by jchung