Circle2D.java
package package30;
public class Circle2D {
/*--------------------------------------------------------------------------*/
// 无参构造方法
/**创建x,y为0,r为1的圆*/
public Circle2D() {this.x=0;this.y=0;this.r=1;}
// 有参数构造方法
/**创建指定的x,y,r值的圆*/
public Circle2D(double X,double Y,double R) {this.x=X;this.y=Y;this.r=R;}
/*--------------------------------------------------------------------------*/
private double x,y,r; // 圆的中心点
// 定义方法设置属性值x,y,r
/**设置圆x的值*/
protected void setX(double X) {this.x=X;}
/**设置圆y的值*/
protected void setY(double Y) {this.y=Y;}
/**设置圆r的值*/
protected void setR(double R) {this.r=R;}
// 定义方法获取属性值x,y,r
/**获取圆x的值*/
protected double getX() {return this.x;}
/**获取圆y的值*/
protected double getY() {return this.y;}
/**获取圆r的值*/
protected double getR() {return this.r;}
/*--------------------------------------------------------------------------*/
/**返回计算圆的面积*/
protected double getArea() {
return this.r*this.r*Math.PI;
}
/**返回计算圆的周长*/
protected double getPerimeter() {
return this.r*Math.PI*2;
}
/**
*Math.sqrt()//计算平方根
*Math.cbrt()//计算立方根
*Math.pow(a, b)//计算a的b次方
*Math.max( , );//计算最大值
*Math.min( , );//计算最小值
*/
/**给定中心点,判断是否在圆内*/
protected boolean contains(double x1,double y1) {
double distance = Math.sqrt((x1-this.x)*(x1-this.x)+(y1-this.y)*(y1-this.y));
if(distance>this.r) {
return false;
}
if(distance<=this.r) {
return true;
}
return false;
}
/**给定中心点,半径 ,判断该圆是否在圆内*/
protected boolean contains(double x1,double y1,double r1) {
double distance = Math.sqrt((x1-this.x)*(x1-this.x)+(y1-this.y)*(y1-this.y));
if(distance+r1>this.r) {
return false;
}
if(distance+r1<=this.r) {
return true;
}
return false;
}
}
Test03.java
package package30;
public class Test03 {
public static void main(String[] args) {
// 测试一下
Circle2D circleOne = new Circle2D(0,5,1);
System.out.println("面积:"+circleOne.getArea()+
" 周长:"+circleOne.getPerimeter());
Circle2D circleTwo = new Circle2D(0,0,5);
System.out.println("面积:"+circleTwo.getArea()+
" 周长:"+circleTwo.getPerimeter());
if (circleTwo.contains(circleOne.getX(), circleOne.getY())) {
System.out.println("该点在圆Two内");
}else {
System.out.println("该点不在圆Two内");
}
if (circleTwo.contains(circleOne.getX(), circleOne.getY(), circleOne.getR())) {
System.out.println("circleOne在Two圆内");
} else {
System.out.println("circleOne不在Two圆内");
}
}
}
测试结果