title: Java: How to not allow variable to go below 0
body: <p>I have a counter class I am testing. The counter increments up or down by 1. However, I do not want the number to go below zero, and I want to output an error message when the counter is set to a negative value. Below is the code for my counter class. Also, I am unsure how to have the boolean test display a string stating two instances of the counter are (or are not) equal. Thanks for your help.</p>
  
  <pre><code>public class Counter {
  
      private int counter;
  
      public Counter() {
          counter = 0;
      }
  
      //mutator adds 1 to counter
      public void plus1(){
          setCounter(getCounter() + 1);
      }
  
      //mutator subtracts 1 from counter
      public void minus1(){
          setCounter(getCounter() - 1);
      }
  
      //mutator
      public void setCounter(int newCounter){
          counter = newCounter;
      }
  
      //accessor
      public int getCounter(){
          return counter;
      }
  
      //polymorph
      public String toString(){
          return "The counter is currently at " + counter + ".";
      }
  
      public boolean equals(Counter a){
          return this.getCounter() == a.getCounter();
      }
  }
  </code></pre>