Constructor | Description |
---|---|
Add a "no-arg constructor" | Set both ivars to zero |
Add a constructor with one method parameter | Set both ivars to the one method parameter |
Add a constructor with two method parameters | Set the "width" and "height" ivars to the first and second method parameters respectively |
public Rectangle() { //Constructing a Rectangle: 0 by 0 this.width = 0; this.height = 0; }
public Rectangle(int aDimension) { //Constructing a Square: aDimension by aDimension //A square is special type of rectangle this.width = aDimension; this.height = aDimension; }
public Rectangle(int aWidth, int aHeight) { //Constructing a Rectangle: aWidth by aHeight this.width = aWidth; this.height = aHeight; }
Rectangle rec1, rec2, rec3, square1, square2;
rec1 = new Rectangle(11, 5);
rec2 = new Rectangle(1000, 500);
rec3 = new Rectangle(); //Using no-args constructor
square1 = new Rectangle(800);
square2 = new Rectangle(1);
public void testNoArgConstructor() { Rectangle rec; rec = new Rectangle(); System.out.println("\n-- testNoArgConstructor --"); System.out.println("Expected: 0 x 0"); System.out.println("Actual: " + rec.toString()); } public void testConstructorWithOneParam() { //Constructing a square //Which is a Rectangle with both sides equal Rectangle square; square = new Rectangle(8); System.out.println("\n-- testConstructorWithOneParam --"); System.out.println("Expected: 8 x 8"); System.out.println("Actual: " + square.toString()); } public void testConstructorWithTwoParams() { Rectangle rec; rec = new Rectangle(10, 5); System.out.println("\n-- testConstructorWithTwoParams --"); System.out.println("Expected: 10 x 5"); System.out.println("Actual: " + rec.toString()); }