package javaapplication1;
public class Circle {
int radius;
int x;
int y;
public Circle(){
this.radius = 10;
this.x = 0;
this.y = 0;
}
public Circle(int r){
this.radius = r;
this.x = 0;
this.y = 0;
}
public Circle(int x, int y, int r){
this.radius = r;
this.x = x;
this.y = y;
}
void setRadius(int r){
this.radius = r;
}
void setLocation(int x, int y){
this.x = x;
this.y = y;
}
double getArea(){
return Math.PI * (this.radius * this.radius);
}
public String toString(){
return "[" + this.x + ", " + this.y + ", " + this.radius + "]";
}
boolean isOver(Circle c){
int ax = this.x - c.x;
int ay = this.y - c.y;
int length = ax * ax + ay * ay;
int r = this.radius + c.radius;
if (length <= r * r){
return true;
} else {
return false;
}
}
public static void main(String[] args){
Circle c = new Circle();
System.out.println("円cは");
System.out.println(c.toString());
Circle d = new Circle(5);
System.out.println("円dは");
System.out.println(d.toString());
System.out.println("面積は" + d.getArea());
Circle e = new Circle();
e.setLocation(10, 0);
e.setRadius(5);
System.out.println("円eは");
System.out.println(e.toString());
System.out.println("面積は" + e.getArea());
if (d.isOver(e)){
System.out.println("重なっている");
} else {
System.out.println("重なっていない");
}
}
}
最終更新:2012年01月20日 11:44