Advertisement
theyellowdancer

Adam Lab03 Ex 19

Oct 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. public class Counter
  2. {
  3.     private int count;
  4.     private int max;
  5.    
  6.     public Counter() {
  7.         count = 0;
  8.         max = 10;
  9.     }
  10.    
  11.     public int getCount() {
  12.         return count;
  13.     }
  14.    
  15.     public int getMax() {
  16.         return max;
  17.     }
  18.    
  19.     public void setCount(int n) {
  20.         count = n;
  21.     }
  22.    
  23.     public void setMax(int n) {
  24.         max = n;
  25.     }
  26.    
  27.     public void increase() {
  28.         if (count < max) {
  29.             count++;
  30.         }
  31.     }
  32.    
  33.     public void decrease() {
  34.         if (count > 0) {
  35.             count--;  
  36.         }
  37.     }
  38.    
  39.     public void reset() {
  40.         count = 0;
  41.         max = 10;
  42.     }
  43.    
  44.     public void increase(int n) {
  45.         if (count + n <= max) {
  46.             count += n;
  47.         }
  48.     }
  49.    
  50.     public void decrease(int n) {
  51.         if (count - n >= 0) {
  52.             count -= n;
  53.         }
  54.     }
  55.    
  56.     public String toString() {
  57.         return ("The count is " + count + " and the max is " + max);
  58.     }
  59. }
  60.  
  61. public class CounterTest {
  62.  
  63.     public static void main(String[] args) {
  64.         Counter counter = new Counter();
  65.         System.out.println(counter.toString());
  66.         counter.increase();
  67.         System.out.println(counter.toString());
  68.         counter.increase(10);
  69.         System.out.println(counter.toString());
  70.         counter.increase(7);
  71.         System.out.println(counter.toString());
  72.         counter.decrease(8);
  73.         System.out.println(counter.toString());
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement