1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class Counter
{
int counter=0;
int stepValue;
public Counter(int step)
{
step=stepValue;
}
public void increase(){
counter = counter+stepValue;
}
public void decrease(){
counter = counter-stepValue;
}
public int getCount(){
return counter;
}
public static void main(String[] args)
{
Counter counter = new Counter(1); //create a new counter with a step value of 1
counter.increase(); //add 1
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter.increase(); //add 1
System.out.println("Expected Count: 2 -----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 1
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter = new Counter(10); //create a new counter with a step value of 10
System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());
counter.increase(); //add 10
System.out.println("Expected Count: 10 ----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 10
System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 10
System.out.println("Expected Count: -10 -----> Actual Count: " + counter.getCount());
}
} |
Partager