Exercise: Creating circles

Exercise: Circle - Part I

Point

  • contains x and y

  • copy Point.java

class Point {
    private double x, y;

    //=== constructors ===

    //default constructor
    Point() {
        x =0;
        y = 0;
    }

    Point(double new_x, double y) {
        x = new_x;
        this.y = y;
    }

    //copy constructor
    Point(Point p) {
        x = p.x; // Since this is the same class I can access without getter/setter
        y = p.y;
    }

    //=== static methods ===
    public static double distance(Point a, Point b) {
        double dist = Math.pow(a.x-b.x, 2) + Math.pow(a.y-b.y, 2);
        return Math.sqrt(dist);
    }

    //=== getters and setters ===
    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public String toString() {
        return "Point {" + " x=" + x + ", y=" + y + " }";
    }
}

Write the following class:

  • Circle

    • contains a point and radius

the Circle class should include the following functions (methods):

  • 3 constructors

    • default constructor

    • copy constructor

    • Circle(double x, double y, double radius)

  • getters and setters (for point and radius)

  • public static boolean intersect(Circle a, Circle b)

  • public double circumference ()

  • public double area()

  • toString()

Solution

Exercise: Circle - Part II - adding intersections and random circles

add the following functions to Circle

  • intersect - return if 2 circles intersect

  • intersections - print all the circles that intersect

  • createRandomCircles - generate x amount of random circles

Solution

Exercise: Circle - Part III - drawing circles with StdDraw

add the following functions to Circle

  • draw - draw the circle (non static)

  • setWindowForCircles - set the window size, find: min of x, min of y, max of x and max of y

    • find the 4 edges of the window

    • Then use StdDraw.setXscale() and StdDraw.setYscale()

Solution

Code until now

To make the circles move we add some dx and dy to their position, also we can increase and decrease their size. We also have to in account the boundary of the window.

Last updated

Was this helpful?