Advertisement
cd62131

qa8308212 water tank implementation

Oct 18th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.11 KB | None | 0 0
  1. public class QA8308212 {
  2.   protected static double capacity;
  3.   protected double water;
  4.   public QA8308212() {
  5.     capacity = 100.;
  6.     this.setWater(.0);
  7.   }
  8.   public void add(double water) {
  9.     if (this.put(water)) { return; }
  10.     System.out.println("OVERFLOW");
  11.   }
  12.   public double getWater() {
  13.     return this.water;
  14.   }
  15.   protected boolean put(double water) {
  16.     if (this.getWater() + water > capacity) {
  17.       this.setWater(capacity);
  18.       return false;
  19.     }
  20.     double addee = this.getWater();
  21.     this.setWater(addee + water);
  22.     return true;
  23.   }
  24.   protected void setWater(double water) {
  25.     this.water = water;
  26.   }
  27.   @Override
  28.   public String toString() {
  29.     return "Tank: " + this.getWater();
  30.   }
  31.   public static void main(String[] args) {
  32.     QA8308212 tank = new QA8308212();
  33.     System.out.println(tank);
  34.     tank.add(10.);
  35.     System.out.println(tank);
  36.     tank.add(20.);
  37.     System.out.println(tank);
  38.     tank.add(30.);
  39.     System.out.println(tank);
  40.     tank.add(100.);
  41.     tank.add(200.);
  42.     tank.add(300.);
  43.     tank.add(400.);
  44.     System.out.println(tank);
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement