Rectangle rec1, rec2, rec3; rec1 = new Rectangle(); rec2 = new Rectangle(11, 5); rec3 = new Rectangle(8);
public class Rectangle { private int width; private int height; //No-arg Constructor (which is called when we do "new Rectangle()" public Rectangle() { //Constructing a Rectangle: 0 by 0 this.width = 0; this.height = 0; } public Rectangle(int aWidth, int aHeight) { //Constructing a Rectangle: aWidth by aHeight this.width = aWidth; this.height = aHeight; } public Rectangle(int aDimension) { //Constructing a Square: aDimension by aDimension //A square is special type of rectangle this.width = aDimension; this.height = aDimension; } public void setSmall() { //Set ivars to arbritrary small values width = 5; height = 1; } public void setLarge() { //Set ivars to arbritrary small values width = 100; height = 50; } //------------------------------------------ //Getters public int getWidth() { return width; } public int getHeight() { return height; } //------------------------------------------ //Setters public void setWidth(int aWidth) { width = aWidth; } public void setHeight(int aHeight) { height = aHeight; } //------------------------------------------ //Common public String toString() { //Return a "nice" display string return "" + width + " x " + height; } //------------------------------------------ public void setDimensions(int aWidth, int aHeight) { width = aWidth; height = aHeight; } public void growBy(int amount) { width = width + amount; height = height + amount; } public void growBy(int widthAmount, int heightAmount) { width = width + widthAmount; height = height + heightAmount; } }