Java语言程序设计第十章编程练习题10.13


public class MyRectangle2D {

	public double x=0;
	public double y=0;
	public double width=1;
	public double height=1;
	public double getX() {
		return x;
	}
	
	public void setX(double x) {
		this.x=x;
	}
	
	public double getY() {
		return y;
	}
	
	public void setY(double y) {
		this.y=y;
	}
	
	public double getWidth() {
		return width;
	}
	
	public void setWidth(double width) {
		this.width=width;
	}
	
	public double getHeight() {
		return height;
	}
	
	public void setHeight(double height) {
		this.height=height;
	}
	
	public MyRectangle2D(double x,double y,double width,double height) {
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
	
	public double getArea() {
		return width*height;
	}
	
	public double getPerimeter() {
		return 2*(width+height);
	}
	
	public boolean contains(double x,double y) {
		boolean result =false;
		if((Math.abs(x-this.x)<=width/2)&&(Math.abs(y-this.y)<=height/2))
				result=true;
		return result;	
	}
	
	public boolean contains(MyRectangle2D r) {
		return  contains(r.x-r.width/2,r.y-r.height/2)&&
				contains(r.x-r.width/2,r.y+r.height/2)&&
				contains(r.x+r.width/2,r.y-r.height/2)&&
				contains(r.x+r.width/2,r.y+r.height/2);
	}
	
	public boolean overlaps(MyRectangle2D r) {
		if(contains(r)==true)
			return false;
		else
			return  contains(r.x-r.width/2,r.y-r.height/2)||
					contains(r.x-r.width/2,r.y+r.height/2)||
					contains(r.x+r.width/2,r.y-r.height/2)||
					contains(r.x+r.width/2,r.y+r.height/2);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyRectangle2D r1 =new MyRectangle2D(2,2,5.5,4.9);
		System.out.println("面积为"+r1.getArea()+"周长为"+r1.getPerimeter());
		System.out.println("点(3,3)是否在矩形内"+r1.contains(3,3));
		System.out.println("矩形(4,5,10,5,3.2)是否在该矩形内"+r1.contains(new MyRectangle2D(3,5,2.3,5.4)));
		System.out.println("是否与矩形(3,5,2.3,5.4)重叠"+r1.overlaps(new MyRectangle2D(3,5,2.3,5.4)));
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值