Define a class for rectangle objects defined by two points, the top-left and bottom-right corners of the rectangle. Include a constructor to copy a rectangle, a method to return a rectangle object that encloses the current object and the rectangle passed as an argument, and a method to display the defining points of a rectangle. Test the class by creating four rectangles and combining these cumulatively to end up with a rectangle enclosing them all. Output the defining points of all the rectangles you create.
This is the Point class definition. It is used by the Rectangle class
public class Point {
// Create a Point object from a coordinate pair
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// Create a Point object from another Point
public Point(Point p) {
x = p.x;
y = p.y;
}
// Get the value of the x coordinate
double getX() {
return x;
}
// Get the value of the y coordinate
double getY() {
return y;
}
public String toString(){
return x +","+y;
}
// Fields store the point coordinates
private double x;
private double y;
}
This is the Rectangle class definition. It makes use of the Point class definition that is in the same directory as this file.
public class Rectangle {
private Point topLeft;
private Point bottomRight;
// Constructors
public Rectangle(double x1, double y1, double x2, double y2) {
// Ensure topleft uses the smaller of x1,x2 and y1,y2
// and topright uses the larger of x1,x2 and y1,y2
topLeft = new Point(Math.min(x1,x2),Math.min(y1,y2));
bottomRight = new Point(Math.max(x1,x2),Math.max(y1,y2));
}
public Rectangle(Point tl, Point br) {
this(tl.getX(), tl.getY(), br.getX(), br.getY());
}
public Rectangle(Rectangle rect) {
topLeft = new Point(rect.topLeft);
bottomRight = new Point(rect.bottomRight);
}
// Methods to create a rectangle enclosing the current rectangle and the argument
public Rectangle encloses(Rectangle rect) {
// Return a new rectangle defined by the minimum x,y for to left and the and maximum x,y for bottom right
return new Rectangle(Math.min(topLeft.getX(), rect.topLeft.getX()),
Math.min(topLeft.getY(), rect.topLeft.getY()),
Math.max(bottomRight.getX(), rect.bottomRight.getX()),
Math.max(bottomRight.getY(), rect.bottomRight.getY()));
}
public String toString() {
return "Rectangle: " + topLeft + " : " + bottomRight;
}
}
This is the main Class to Test the Rectangle Class.
public class TestRectangle {
public static void main(String args[]) {
Rectangle[] rects = new Rectangle[4];
Rectangle enclosing;
// Initialize the rectangles as arbitrary sizes and at arbitrary positions:
for(int i = 0 ; i < rects.length ; ++i) {
rects[i] = new Rectangle(Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100);
System.out.print("Coords of rectangle " + i + " are: ");
System.out.println(rects[i]);
}
// Initialize the enclosing rectangle to be first rectangle
enclosing = rects[0];
// Combine it with each the other rectangles in turn.
// This will result in the rectangle that encloses them all.
for(Rectangle rect : rects) {
enclosing = enclosing.encloses(rect);
}
System.out.println("\nCoords of Enclosing rectangle are ");
System.out.println(enclosing);
}
}
Output of Above Java Program
Coords of rectangle 0 are: Rectangle: 0.1903728640358593,58.04643476457927 : 0.2032621709617044,85.8337274416007
Coords of rectangle 1 are: Rectangle: 6.239529478079319,44.58944044246343 : 66.47238823570548,57.983153189860396
Coords of rectangle 2 are: Rectangle: 10.500877688164522,59.35612909169482 : 80.53072142287722,99.35041648038772
Coords of rectangle 3 are: Rectangle: 28.73052501676693,2.2641735796500972 : 44.35555027080914,11.239350082208022
Coords of Enclosing rectangle are
Rectangle: 0.1903728640358593,2.2641735796500972 : 80.53072142287722,99.35041648038772