Circle.java


Below is the syntax highlighted version of Circle.java from §9.1 Geometric Primitives.


/******************************************************************************
 *  Compilation:  javac Circle.java
 *  Execution:    java Circle
 *  Dependencies: Point.java
 *
 *  Implementation of a 2D circle with integer values for the 
 *  center (x, y) and radius.
 *
 ******************************************************************************/

public class Circle { 
    private final Point center;
    private final int radius;
   
    public Circle(Point center, int radius) {
        this.center = center;
        this.radius = radius;
    }

    public boolean contains(Point p) {  return p.distanceTo(center) <= radius; }

    public double area()      { return Math.PI * radius * radius; }
    public double perimeter() { return 2 * Math.PI * radius;      }

    public boolean intersects(Circle c) {
        return center.distanceTo(c.center) <= radius + c.radius;
    }


    // test client
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);

        Point center = new Point(50, 50);
        Circle circle = new Circle(center, 50);
    }
}


Copyright © 2002–2016, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Aug 30 10:09:18 EDT 2016.