/* * Implements a box by recording its lower left and * upper right corners, using the Point class. * Don Chamberlin, March 2009 */ class Box { Point lowerLeft; Point upperRight; Box(Point p1, Point p2) { lowerLeft = new Point(Math.min(p1.getX(), p2.getX()), Math.min(p1.getY(), p2.getY())); upperRight = new Point(Math.max(p1.getX(), p2.getX()), Math.max(p1.getY(), p2.getY())); } public String toString() { return "Box(" + lowerLeft.getX() + "," + lowerLeft.getY() + ")-(" + upperRight.getX() + "," + upperRight.getY() + ")"; } double perimeter() { return 2.0 * Math.abs(upperRight.getX() - lowerLeft.getX()) + 2.0 * Math.abs(upperRight.getY() - lowerLeft.getY()); } double area() { return Math.abs(upperRight.getX() - lowerLeft.getX()) * Math.abs(upperRight.getY() - lowerLeft.getY()); } boolean contains(Point p) { if (p.getX() >= lowerLeft.getX() && p.getX() <= upperRight.getX() && p.getY() >= lowerLeft.getY() && p.getY() <= upperRight.getY()) return true; else return false; } } // end of class Box