Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Counter
- {
- private int count;
- private int max;
- public Counter() {
- count = 0;
- max = 10;
- }
- public int getCount() {
- return count;
- }
- public int getMax() {
- return max;
- }
- public void setCount(int n) {
- count = n;
- }
- public void setMax(int n) {
- max = n;
- }
- public void increase() {
- if (count < max) {
- count++;
- }
- }
- public void decrease() {
- if (count > 0) {
- count--;
- }
- }
- public void reset() {
- count = 0;
- max = 10;
- }
- public void increase(int n) {
- if (count + n <= max) {
- count += n;
- }
- }
- public void decrease(int n) {
- if (count - n >= 0) {
- count -= n;
- }
- }
- public String toString() {
- return ("The count is " + count + " and the max is " + max);
- }
- }
- public class CounterTest {
- public static void main(String[] args) {
- Counter counter = new Counter();
- System.out.println(counter.toString());
- counter.increase();
- System.out.println(counter.toString());
- counter.increase(10);
- System.out.println(counter.toString());
- counter.increase(7);
- System.out.println(counter.toString());
- counter.decrease(8);
- System.out.println(counter.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement