public class Rectangle { private int width; private int height; public Rectangle(int theWidth, int theHeight) { // Only allow nonnegative width and height. if (theWidth > 0) width = theWidth; else width = 0; if (theHeight > 0) height = theHeight; else height = 0; } public int getWidth( ) { return width; } public int getHeight( ) { return height; } public int getArea( ) { return width * height; } public String toString( ) { return String.format("%dx%d area=%d", width, height, getArea( )); } public static void main(String[ ] args) { Rectangle r = new Rectangle(12, 15); System.out.println("Width of r is " + r.getWidth( )); System.out.println("Height of r is " + r.getHeight( )); System.out.println("Area of r is " + r.getArea( )); System.out.println("toString output of r is "); System.out.println(r); System.out.println( ); // Test behavior of negative width and/or // height in constructor. System.out.println(new Rectangle(-5, 15)); System.out.println(new Rectangle(12, -8)); System.out.println(new Rectangle(-5, -8)); } }