Inf1 OP : Lab Sheet Week 7 Q5 - Shapes
Overview
Warning
Pair Programming:
This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.

In this exercise, your aim is to create two classes, Circle and Rectangle, which will both have a public method isPointInside(). This method will test whether a Point2DDouble falls with the given geometric shape.

Point2DDouble is similar to the class Point2D in Lab 5 Exercise 4. However, this time the class treats the x and y coordinates as being of type double. Here is the definition of the class:

public class Point2DDouble {

    private double x, y;

    public Point2DDouble(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public static double distance(Point2DDouble pt1, Point2DDouble pt2) {
        double dX = pt1.getX() - pt2.getX();
        double dY = pt1.getY() - pt2.getY();
        return Math.sqrt(dX * dX + dY * dY);
    }

    public double getX() {
            return x;
    }

    public double getY() {
            return y;
    }

}

You should make a copy of this code and place it in the same folder as the rest of your answers to this exercise.

Circle

Create a class Circle with the following API:

public Circle(Point2DDouble centre, double radius)
Class constructor.
public Circle()
Class constructor. The centre is a point at (0, 0) and the radius is 1.
public boolean isPointInside(Point2DDouble pt)
Returns true if the circle contains the point pt. You can compute this by calculating the distance between the pt and the circle’s centre, and seeing if this is smaller than the radius.

An automated test has been created for this exercise: CircleTest.java.

Rectangle

Create a class Rectangle with the following API:

public Rectangle(Point2DDouble topLeft, Point2DDouble bottomRight)
Class constructor
public Rectangle()
Class constructor for a rectangle whose top left is at (0, 0) and whose bottom right is at (1, 1).
public boolean isPointInside(Point2DDouble pt)
Return true if the rectangle contains the point pt. You can compute this by determining if the \( x \) coordinate of pt lies between the topLeft \( x \) coordinate and the bottomRight \( x \) coordinate. If this is true, and also true for the \( y \) coordinates, then the rectangle contains the point. We are assuming the origin of the coordinate system, point (0, 0), is in the top-left-most position, so \( x \) increases from left to right and \( y \) increases from top to bottom.

An automated test has been created for this exercise: RectangleTest.java.