public class Main {
public static void main(String[] args) {
}
}
|
public class Circle {
private XYPoint center;
private int radius;
public Circle(XYPoint center, int radius) {
setCenter(center);
setRadius(radius);
}
public Circle(int x, int y, int radius) {
this(new XYPoint(x,y),radius);
}
public Circle(int radius) {
this(0,0,radius);
}
public Circle() {
this(0);
}
public XYPoint getCenter() {
return center;
}
public int getCenterX() {
return center.getX();
}
public int getCenterY() {
return center.getY();
}
public int getRadius() {
return radius;
}
public int getDiameter() {
return 2 * getRadius();
}
public double getCircumference() {
return Math.PI * getDiameter();
}
public double getArea() {
return Math.PI * Math.pow(getRadius(),2);
}
public boolean overlap(Circle otherCircle) {
return (getRadius() + otherCircle.getRadius()) >=
XYPoint.distance(getCenter(),otherCircle.getCenter());
}
public void setCenter(XYPoint center) {
this.center = center;
}
public void setCenterX(int x) {
center.setX(x);
}
public void setCenterY(int y) {
center.setY(y);
}
public void setCenter(int x, int y) {
setCenterX(x);
setCenterY(y);
}
public void setRadius(int radius) {
this.radius = radius;
}
public String toString() {
return "a circle with center at " + center + " and radius " + radius;
}
}
|
public class Main {
public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println(c1);
XYPoint center = c1.getCenter();
center.setX(8);
center.setY(16);
System.out.println(c1);
c1.setCenter(-2,4);
System.out.println(c1);
center = new XYPoint(-3,6);
c1.setCenter(center);
c1.setRadius(2);
System.out.println(c1);
System.out.println("c1 has diameter " + c1.getDiameter());
System.out.println("c1 has circumference " + c1.getCircumference());
System.out.println("c1 has area " + c1.getArea());
Circle c2 = new Circle(-8,3,3);
System.out.println(c2);
double dist = XYPoint.distance(c1.getCenter(), c2.getCenter());
System.out.println("c1 and c2 are separated by a distance " + dist);
System.out.print("do c1 and c2 overlap? ");
boolean overlap;
overlap = c1.overlap(c2);
System.out.println(overlap);
c2.setRadius( 2 * c1.getRadius() );
System.out.println("c1 and c2 are separated by a distance " + dist);
System.out.print("do c1 and c2 overlap? ");
overlap = c1.overlap(c2);
System.out.println(overlap);
}
}
|
a circle with center at (0,0) and radius 0 a circle with center at (8,16) and radius 0 a circle with center at (-2,4) and radius 0 a circle with center at (-3,6) and radius 2 c1 has diameter 4 c1 has circumference 12.566370614359172 c1 has area 12.566370614359172 a circle with center at (-8,3) and radius 3 c1 and c2 are separated by a distance 5.830951894845301 do c1 and c2 overlap? false c1 and c2 are separated by a distance 5.830951894845301 do c1 and c2 overlap? true |