invariant og assert()
Hvis jeg skal verificere at implementations invatriantet er overholdt, hvor i følgende kode skal jeg så lave et check, og hvorledes gøres dette med assert()?public class Box {
/**
* IMPLEMENTATION INVARIANT: x <= y <= z
*/
private int x,y,z;
public Box(int w, int h, int d) {
x = w; y = h; z = d;
while (x>y || y>z) {
if (x>y) {int temp = x; x = y; y = temp; }
if (y>z) {int temp = y; y = z; z = temp; }
}
}
public int volume() {
return (x*y*z);
}
public boolean fitsIn(Box b) {
return ( x<=b.x && y<=b.y && z<=b.z );
}
public Box combine(Box b) {
return new Box(Math.max(x,b.x),
Math.max(y,b.y),
Math.max(z,b.z));
}
public String toString()
{
return "Box("+x+","+y+","+z+")";
}
}
