Testing

We may run the first part of our test program to test the code for various input values. Four points will be constructed via user input and methods will be invoked to compute the length, width, area and perimeter of the rectangle.

Uncommenting the first part of our test program in the main method:

  1. The test code will read the x- and y-coordinates of a point strictly in the following order:

    upperLeft, lowerLeft, lowerRight and upperRight

    You do not need to perform any error checking. You should assume that:

    • The x- and y-coordinates are positive integers.
    • The coordinates are entered strictly in order, i.e. upperLeft, lowerLeft, lowerRight and upperRight
    • The coordinates entered by the user form a meaningful rectangle (or square)
  2. Once the test code has the coordinates stored in 'Point' variables, it creates an object of type Rectangle.
  3. The test code displays the length, width, area, and perimeter of the rectangle.
Scanner scanner = new Scanner(System.in);
int x = 1;
int y = 1;

System.out.print("Enter the x-coordinate of the upperLeft :");
x = scanner.nextInt();
System.out.print("Enter the y-coordinate of the upperLeft :");
y = scanner.nextInt();

Point upperLeft = new Point(x, y);

System.out.print("Enter the x-coordinate of the lowerLeft :");
x = scanner.nextInt();
System.out.print("Enter the y-coordinate of the lowerLeft :");
y = scanner.nextInt();

Point lowerLeft = new Point(x, y);

System.out.print("Enter the x-coordinate of the lowerRight :");
x = scanner.nextInt();
System.out.print("Enter the y-coordinate of the lowerRight :");
y = scanner.nextInt();

Point lowerRight = new Point(x, y);

System.out.print("Enter the x-coordinate of the upperRight:");
x = scanner.nextInt();
System.out.print("Enter the y-coordinate of the upperRight:");
y = scanner.nextInt();

Point upperRight = new Point(x, y);

Rectangle rectangle = new Rectangle(upperLeft, lowerLeft, lowerRight, upperRight);

System.out.printf("Length of rectangle : %.2f\n", rectangle.getLength());
System.out.printf("Width of rectangle : %.2f\n", rectangle.getWidth());
System.out.printf("Area of rectangle : %.2f\n", rectangle.getArea());
System.out.printf("Perimeter of rectangle : %.2f\n", rectangle.getPerimeter());

//test the copy constructor
/*Rectangle rectangleNew = new Rectangle(rectangle);

System.out.printf("Length of rectangleNew : %.2f\n", rectangleNew.getLength());
System.out.printf("Width of rectangleNew : %.2f\n", rectangleNew.getWidth());
System.out.printf("Area of rectangleNew : %.2f\n", rectangleNew.getArea());
System.out.printf("Perimeter of rectangleNew : %.2f\n", rectangleNew.getPerimeter());*/

Test Cases

Input

(1,4), (1,1), (6,1), (6,4)

Output

Length of rectangle : 5.00
Width of rectangle : 3.00
Area of rectangle : 15.00
Perimeter of rectangle : 16.00

Input

(0,2), (2,0), (6,4),(4,6)

Output

Length of rectangle : 5.66
Width of rectangle : 2.83
Area of rectangle : 16.00
Perimeter of rectangle : 16.97