public void setWidth(int aWidth) {
this.width = aWidth;
}
public void testSetWidth() { Rectangle rec; rec = new Rectangle(); rec.setWidth(11); System.out.println("\n-- testSetWidth --"); System.out.println("Set Width to 11"); System.out.println("Actual Width: " + rec.getWidth()); }
public void setDimensions(int aWidth, int aHeight) {
this.width = aWidth;
this.height = aHeight;
}
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); System.out.println("\n-- testSetDimensions --"); System.out.println("Set dimensions to 8 and 7"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); }
public void growBy(int amount) { this.width = this.width + amount; this.height = this.height + amount; } public void growBy(int widthAmount, int heightAmount) { this.width = this.width + widthAmount; this.height = this.height + heightAmount; }
public class Rectangle { private int width; private int height; public void setSmall() { //Set ivars to arbritrary small values this.width = 5; this.height = 1; } public void setLarge() { //Set ivars to arbritrary small values this.width = 100; this.height = 50; } //------------------------------------------ //Getters public int getWidth() { return this.width; } public int getHeight() { return this.height; } //------------------------------------------ //Setters public void setWidth(int aWidth) { this.width = aWidth; } public void setHeight(int aHeight) { this.height = aHeight; } //------------------------------------------ //Common public String toString() { //Return a "nice" display string return "" + this.width + " x " + this.height; } //------------------------------------------ public void setDimensions(int aWidth, int aHeight) { this.width = aWidth; this.height = aHeight; } public void growBy(int amount) { this.width = this.width + amount; this.height = this.height + amount; } public void growBy(int widthAmount, int heightAmount) { this.width = this.width + widthAmount; this.height = this.height + heightAmount; } }
public class RectangleTests { public void testSetSmall() { Rectangle rec; rec = new Rectangle(); rec.setSmall(); System.out.println("\n-- testSetSmall --"); System.out.println("Width: " + rec.getWidth()); System.out.println("Height: " + rec.getHeight()); } public void testSetLarge() { Rectangle rec; rec = new Rectangle(); rec.setLarge(); System.out.println("\n-- testSetLarge --"); System.out.println("Width: " + rec.getWidth()); System.out.println("Height: " + rec.getHeight()); } public void testSetWidth() { Rectangle rec; rec = new Rectangle(); rec.setWidth(11); System.out.println("\n-- testSetWidth --"); System.out.println("Set Width to 11"); System.out.println("Actual Width: " + rec.getWidth()); } public void testSetHeight() { Rectangle rec; rec = new Rectangle(); rec.setHeight(5); System.out.println("\n-- testSetHeight --"); System.out.println("Set Height to 5"); System.out.println("Actual Height: " + rec.getHeight()); } public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); System.out.println("\n-- testSetDimensions --"); System.out.println("Set dimensions to 8 and 7"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); System.out.println("\n-- testGrowByWithOneParam --"); System.out.println("Sent growBy with 5 (after setting dimensions to 100)"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); System.out.println("\n-- testGrowByWithTwoParams --"); System.out.println("Sent growBy with 3 and 2 (after setting dimensions to 100)"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } //======================================= public static void main(String[] args) { RectangleTests test = new RectangleTests(); test.testSetSmall(); test.testSetLarge(); test.testSetWidth(); test.testSetHeight(); test.testSetDimensions(); test.testGrowByWithOneParam(); test.testGrowByWithTwoParams(); } }