-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecreasingCounter.java
44 lines (36 loc) · 1.02 KB
/
DecreasingCounter.java
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
public class DecreasingCounter {
private int value; // instance variable that remembers the value of the counter
private int initialValue;
public DecreasingCounter(int valueAtStart) {
this.initialValue = valueAtStart;
this.value = valueAtStart;
}
public void printValue() {
// do not touch this!
System.out.println("value: " + this.value);
}
public void decrease() {
// write here code to decrease counter value by one
if (this.value > 1) {
this.value--;
} else {
this.value = 0;
}
}
public void reset() {
this.value = 0;
}
public void setInitial() {
this.value = initialValue;
}
// and here the rest of the methods
}
public class Main {
public static void main(String[] args) {
DecreasingCounter counter = new DecreasingCounter(10);
counter.printValue();
counter.decrease();
counter.decrease();
counter.printValue();
}
}