Java语言程序设计与数据结构(基础篇)课后练习题 第十章

这篇博客通过Java代码展示了如何实现2D圆、三角形和矩形的面积、周长计算,以及点、几何图形之间的包含关系判断。包括Circle2D、Triangle2D和MyRectangle2D类的详细定义和使用示例。
摘要由CSDN通过智能技术生成

}

public int getSize(){

return size;

}

}

10.11

==================================================================

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Circle2D c1 = new Circle2D(2, 2, 5.5);

System.out.println("The c1’s area is " + c1.getArea());

System.out.println("The c1’s perimeter is " + c1.getPerimeter());

System.out.println("The points contains is " + c1.contains(3, 3));

System.out.println("The circle contains is " + c1.contains(new Circle2D(4, 5, 10.5)));

System.out.println("The circle contains is " + c1.contains(new Circle2D(3, 5, 2.3)));

}

}

class Circle2D {

private double x;

private double y;

private double radius;

public Circle2D() {

this.x = 0;

this.y = 0;

this.radius = 1;

}

public Circle2D(double x, double y, double radius) {

this.x = x;

this.y = y;

this.radius = radius;

}

public double getX() {

return x;

}

public double getY() {

return y;

}

public double getRadius() {

return radius;

}

public double getArea() {

return Math.PI * radius * radius;

}

public double getPerimeter() {

return 2 * Math.PI * radius;

}

public boolean contains(double x, double y) {

return Math.pow((x - this.x), 2) + Math.pow((y - this.y), 2) < Math.pow(this.radius, 2);

}

public boolean contains(Circle2D circle) {

return Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) <= Math

.abs(circle.radius - this.radius);

}

public boolean overlaps(Circle2D circle) {

return Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) > Math

.abs(circle.radius - this.radius)

&& Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) < circle.radius + this.radius;

}

}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值