Constructing a Rectangle Copy

A constructor that makes a copy of an existing object should create an object that is a separate, independent object, but with the instance variables set so that it is an exact copy of the argument object.

You will create a such a constructor in the Rectangle class. Because of composition, you will have to create a constructor in the Point class that makes an exact copy of an existing Point and use it in the Rectangle class. Note that Point is immutable, so the constructor in the Point class is not really necessary.

// constructor to copy a Point in Point class
public Point(Point original)
{
    // Add your code here
}

// constructor to copy a Rectangle in Rectangle class, which
// will use the constructor from the Point class
public Rectangle(Rectangle original)
{
    upperLeft = new Point(original.upperLeft)
	// Finish the rest of the code for another
    // three instance variables
}      
Note that the following copy constructor is COMPLETELY WRONG:

public Rectangle(Rectangle original)
{
    upperLeft = original.upperLeft;
}

This is called aliasing. What this WRONG constructor is doing is setting the pointer to upperLeft equal to the pointer to the original Rectangle's upperLeft, like so:

With this WRONG constructor, every time the original's upperLeft changes, our new object's upperLeft will change as well.

Testing

Uncomment the last part of the test program in main. Use the same test case as in step 5 to test your program.

  Scanner scanner = new Scanner(System.in);
  int x = 1;
  int y = 1;

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

  Point upperLeft = new Point(x, y);

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

  Point lowerLeft = new Point(x, y);
  
  
  System.out.print("Enter the coordinates of the lowerRight point (x y):");
  x = scanner.nextInt();
  y = scanner.nextInt();

  Point lowerRight = new Point(x, y);
  
  
  System.out.print("Enter the coordinates of the upperRight point (x y):");
  x = scanner.nextInt();
  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());