import java.awt.*; import java.util.*; public class Circle { // class constants static public final int DEFAULT_RADIUS = 1; static public final Point DEFAULT_CENTER = new Point(0, 0); //instance variables private int radius; private Point center; // constructors // default constructor public Circle() { this.setRadius(DEFAULT_RADIUS); this.setCenter(DEFAULT_CENTER); } // fully specified public Circle(int r, Point p) { this.setRadius(r); this.setCenter(p); } // radius get and set attribute methods public int getRadius() { return this.radius; } public void setRadius(int r) { if ( r > 0 ) { this.radius = r; } else { throw new NumberFormatException("Bad radius: " + r); } } // center get and set attribute methods public Point getCenter() { return this.center; } public void setCenter(Point p) { if ( p != null ) { this.center = p; } else { throw new NullPointerException("Bad center"); } } // other methods // produce string representation public String toString() { String result = "Circle( radius: " + this.getRadius() + ", center: (" + this.getCenter().x + ", " + this.getCenter().y + ") )"; return result; } // compute distance from center to a specified point public double getDistance(Point there) { Point here = this.getCenter(); int x1 = here.x; int y1 = here.y; int x2 = there.x; int y2 = there.y; int deltax = x1 - x2; int deltay = y1 - y2; double distance = Math.sqrt( deltax*deltax + deltay*deltay); return distance; } // compute distance from center to the center of a specified public double getDistance(Circle c) { Point p = c.getCenter(); return this.getDistance(p); } // test whether this circle has same representation as there public boolean equals(Circle there) { int r1 = this.getRadius(); Point p1 = this.getCenter(); int r2 = there.getRadius(); Point p2 = there.getCenter(); return ( r1 == r2 ) && p1.equals(p2); } // duplicate public Object clone() { int r = this.getRadius(); Point p = this.getCenter(); return new Circle(r, p); } // test whether this circle and there refer to the same object public boolean same(Circle there) { return this == there; } // test whether this circle has a common representation as there public boolean equals(Object there) { if ( there instanceof Circle) { return this.equals( (Circle) there ); } else { return false; } } // demo public static void main(String[] args) { int r = 5; Point p = new Point(50, 50); Circle c1 = new Circle(); Circle c2 = new Circle(r, p); Circle c3 = (Circle) c2.clone(); try { c1.setRadius(-10); } catch (Exception e) { c1.setRadius(1000); } System.out.println(); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("c3 = " + c3); System.out.println(); System.out.println("c1 == c2: " + ( c1 == c2 ) ); System.out.println("c1 == c3: " + ( c1 == c3 ) ); System.out.println("c2 == c3: " + ( c2 == c3 ) ); System.out.println(); System.out.println("c1.equals(c2): " + c1.equals(c2) ); System.out.println("c1.equals(c3): " + c1.equals(c3) ); System.out.println("c2.equals(c3): " + c2.equals(c3) ); System.out.println(); } }