Liquid { @Override public boolean equals(Object other) { if (other == null) { return false; } if (other == this) { return true; } if (!(other instanceof Liquid)) { return false; } Liquid otherLiquid = (Liquid) other; return (Math.abs(otherLiquid.amount - amount) < 0.000001 && Math.abs(otherLiquid.temperature - temperature) < 0.000001 && otherLiquid.color.equals(color)); } Liquid(float amount, float temperature, Color color); final Color getColor(); final float getTemperature(); final float getAmount(); @Override boolean equals(Object other); @Override int hashCode();  }
@Test public void testEquals() { Color color = new Color(1, 1, 1); Liquid liquid1 = new Liquid(1, SimulationConstants.MIN_TEMPERATURE, color); Liquid liquid2 = new Liquid(1, SimulationConstants.MIN_TEMPERATURE, color); assertTrue(liquid1.equals(liquid2)); assertTrue(liquid2.equals(liquid1)); assertFalse(liquid1.equals(null)); assertFalse(liquid1.equals("test")); liquid2 = new Liquid(2, SimulationConstants.MIN_TEMPERATURE, color); assertFalse(liquid1.equals(liquid2)); assertFalse(liquid2.equals(liquid1)); liquid2 = new Liquid(1, SimulationConstants.MAX_TEMPERATURE, color); assertFalse(liquid1.equals(liquid2)); assertFalse(liquid2.equals(liquid1)); }