Source Code Structure
-
Open your "LabAssignments" project.
-
Create a new package with the name "lab4".
-
Create the following classes:
-
Point: You will need to implement the constructor
and methods of the Point class in
Point.java
.
-
Rectangle: You will need to implement the
constructor and methods of the Rectangle class in
Rectangle.java
.
-
RectangleDriver:
-
Select create the static main method stub. Note
that only
RectangleDriver.java
contains the main
method, so only
generate a
main method stub for this class.
-
Copy and paste the following code into RectangleDriver's main
method. In your
main method, the test code will retrieve coordinates of a
rectangle from the user and display the length, width, area, and
perimeter of that rectangle.
/*
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());
*/