Define a class called Counter An object of this class is use
Define a class called Counter. An object of this class is used to count things, so it records a count that is a nonnegative whole number. Include methods to set the counter to 0, 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. 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. You need only one instance variable.
this is the CounterTest code. i need help creating the class so that countertest can work.
public class CounterTest
{
public static void main(String[] args)
{
Counter testCounter = new Counter();
testCounter.setZero();
System.out.println(\"After setZero() testCounter = \"
+ testCounter.countIs());
int i;
String junk;
for(i = 0; i < 5; i++)
{
System.out.println(\"Loop count = \" + i);
testCounter.increment();
System.out.println(\"After increment() in loop \"
+ \"using printCounter(): \");
testCounter.printCounter();
System.out.println(\"Using countIs() to return value: \"
+ testCounter.countIs());
}
for(i = 0; i < 6; i++)
{
System.out.println(\"Loop count = \" + i);
testCounter.decrement();
System.out.println(\"After decrement() in loop \"
+ \"using printCounter(): \");
testCounter.printCounter();
System.out.println(\"Using countIs() to return value: \"
+ testCounter.countIs());
}
}
}
Solution
CounterTest.java
public class CounterTest
{
public static void main(String[] args)
{
Counter testCounter = new Counter();
testCounter.setZero();
System.out.println(\"After setZero() testCounter = \"
+ testCounter.countIs());
int i;
String junk;
for(i = 0; i < 5; i++)
{
System.out.println(\"Loop count = \" + i);
testCounter.increment();
System.out.println(\"After increment() in loop \"
+ \"using printCounter(): \");
testCounter.printCounter();
System.out.println(\"Using countIs() to return value: \"
+ testCounter.countIs());
}
for(i = 0; i < 6; i++)
{
System.out.println(\"Loop count = \" + i);
testCounter.decrement();
System.out.println(\"After decrement() in loop \"
+ \"using printCounter(): \");
testCounter.printCounter();
System.out.println(\"Using countIs() to return value: \"
+ testCounter.countIs());
}
}
}
Counter.java
public class Counter {
private int counter ;
public Counter(){
counter = 0;
}
public int countIs() {
return counter;
}
public void setZero(){
counter = 0;
}
public void increment(){
counter = counter + 1;
}
public void decrement(){
if(counter != 0){
counter = counter - 1;
}
}
public void printCounter(){
System.out.println(\"Count: \"+counter);
}
}
Output:
After setZero() testCounter = 0
Loop count = 0
After increment() in loop using printCounter():
Count: 1
Using countIs() to return value: 1
Loop count = 1
After increment() in loop using printCounter():
Count: 2
Using countIs() to return value: 2
Loop count = 2
After increment() in loop using printCounter():
Count: 3
Using countIs() to return value: 3
Loop count = 3
After increment() in loop using printCounter():
Count: 4
Using countIs() to return value: 4
Loop count = 4
After increment() in loop using printCounter():
Count: 5
Using countIs() to return value: 5
Loop count = 0
After decrement() in loop using printCounter():
Count: 4
Using countIs() to return value: 4
Loop count = 1
After decrement() in loop using printCounter():
Count: 3
Using countIs() to return value: 3
Loop count = 2
After decrement() in loop using printCounter():
Count: 2
Using countIs() to return value: 2
Loop count = 3
After decrement() in loop using printCounter():
Count: 1
Using countIs() to return value: 1
Loop count = 4
After decrement() in loop using printCounter():
Count: 0
Using countIs() to return value: 0
Loop count = 5
After decrement() in loop using printCounter():
Count: 0
Using countIs() to return value: 0


