import java.awt.*; public class Rectangle implements Shape { private int x; // we could use a Point instead of x, y private int y; private int width; private int height; public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public boolean contains(Point p) { return this.x <= p.getX() && p.getX() < (this.x + this.width) && this.y <= p.getY() && p.getY() < (this.y + this.height); } public void draw(Graphics g) { Point p = new Point(this.x, this.y); p.draw(g); g.drawRect(this.x, this.y, this.width, this.height); } public boolean equals(Object o) { Rectangle other = (Rectangle) o; return other.x == this.x && other.y == this.y && other.width == this.width && other.height == this.height; } public double getArea() { return this.width * this.height; } public double getPerimeter() { return 2 * this.width + 2 * this.height; } public int getX() { return this.x; } public int getY() { return this.y; } public int getWidth() { return this.width; } public int getHeight() { return this.height; } public int getMinX() { return this.x; } public int getMaxX() { return this.x + this.width; } public int getMinY() { return this.y; } public int getMaxY() { return this.y + this.height; } public Rectangle intersection(Rectangle rect) { int left = Math.max(this.x, rect.x); int top = Math.max(this.y, rect.y); int right = Math.min(this.x + this.width, rect.x + rect.width); int bottom = Math.min(this.y + this.height, rect.y + rect.height); int width = Math.max(0, right - left); int height = Math.max(0, bottom - top); Rectangle intersect = new Rectangle(left, top, width, height); return intersect; } public void translate(int dx, int dy) { this.x += dx; this.y += dy; } public String toString() { return "Rectangle[x=" + this.x + ",y=" + this.y + ",width=" + this.width + ",height=" + this.height + "]"; } }