We do allow rectangles that have zero width or zero height or both.
We needed conditional statements before we could check for errors. In general, error checking looks like:
if ( error ) // Handle errorWe can check for errors in the constructor and the setWidth(), setHeight() methods.
public class Rectangle { private int width = 0, height = 0 ; public Rectangle() { } public Rectangle( int initWidth, int initHeight ) { if ( validSize( initWidth ) && validSize( initHeight ) ) { width = initWidth ; height = initHeight ; } } public int getHeight() { return height ; } public int getWidth() { return width ; } public void setHeight( int newHeight ) { if ( validSize( newHeight ) ) { height = newHeight ; } } public void setWidth( int newWidth ) { if ( validSize( newWidth ) ) { width = newWidth ; } } public int getArea() { return width * height ; } public int getPerimeter() { return 2 * ( width + height ) ; } // Private method private boolean validSize( int size ) { return size >= 0 ; } }