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 © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 12:50:46 EDT 2017.