设计一个Circle类,其属性为原点(类型为类MyPoint)和半径,并为此类编写以下三个方法:
一、计算圆的面积的calArea()方法;
二、计算周长的calLength()方法;
三、boolean inCircle(MyPoint mp)方法,功能是测试作为参数的某个点是否在当前对象圆内(圆内,包括圆上返回true,在圆外返回false)。
程序:
class MyPoint{
private int x,y;
public void setX(int x){
this.x=x;
}
public int getX(){
return this.x;
}
public void setY(int y){
this.y=y;
}
public int getY(){
return this.y;
}
public String toString(){
String str="("+this.x+","+this.y+")";
return str;
}
}
class Circle {
private MyPoint myPoint=null;
private double radius=0.0;
public Circle(MyPoint myPoint,int radius){
this.myPoint=myPoint;
this.radius=radius;
}
public double calArea(){
return Math.PI*radius*radius;
}
public double calLength(){
return 2*Math.PI*radius;
}
public boolean inCircle(MyPoint mp){
double a=Math.sqrt(Math.pow(mp.getX()-myPoint.getY(),2)+Math.pow(mp.getY()-myPoint.getY(),2));
if(a>radius){
return false;
}
return true;
}
}
public class T14{
public static void main(String args[]){
MyPoint mp=new MyPoint();
mp.setX(0);
mp.setY(0);
Circle c=new Circle(mp,5);
double area=c.calArea();
double length=c.calLength();
MyPoint mp2=new MyPoint();
mp2.setX(3);
mp2.setY(3);
boolean in=c.inCircle(mp2);
System.out.println("area="+area+",length="+length+",inCircle="+in);
}