public class Glass { public static void main(String[] args) { Glass glass = new Glass(300); double volume = glass.getCurrentVolume(); assert Math.abs(volume) < EPSILON; glass.addWater(100); volume = glass.getCurrentVolume(); assert Math.abs(volume - 100) < EPSILON; try { glass.removeWater(500); } catch (IllegalArgumentException e) { System.out.println("Exception caught"); } } private static final double EPSILON = 0.01; private double currentVolume; private final double maximumVolume; public Glass(double size) { currentVolume = 0; maximumVolume = size; } public double getCurrentVolume() { return currentVolume; } public double getMaximumVolume() { return maximumVolume; } public void addWater(double amount) { if (amount < 0) { throw new IllegalArgumentException("Negative amount!"); } double newAmount = currentVolume + amount; setVolume(newAmount); } public void removeWater(double amount) { if (amount < 0) { throw new IllegalArgumentException("Negative amount!"); } double newAmount = currentVolume - amount; setVolume(newAmount); } public void setVolume(double amount) { if (amount > maximumVolume) { throw new IllegalArgumentException("Too much water!"); } if (amount < 0) { throw new IllegalArgumentException("Not enough water!"); } currentVolume = amount; } public boolean isEmpty() { return currentVolume < EPSILON; } public boolean isFull() { return currentVolume > maximumVolume - EPSILON; } public void fillCompletely() { setVolume(maximumVolume); } }