Circle2D

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圆内");
		}
	}

}

测试结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值