一道java题

题目:
题目

解题思路:
按照题目中走,需要写五个方法,一个测试;五个方法分别求周长、面积、点圆关系、以及圆圆重叠和相交。则有代码如下

public class Circle2D {
    double x,y,r;														//成员变量
    public  Circle2D(double x,double y,double r){		
            this.x = x;
            this.y = y;
            this.r = r;
    }
                                             //面积 
    public double getErea(){
        double temp = Math.PI*r*r;
        return temp;
    }
    																		//周长
    public double getPerimeter(){
        double temp = r*2*Math.PI;
        return temp;
    }
                                                        //点圆判断方法
    public void contains(double x,double y){
        boolean temp = Math.pow((this.x - x),2) + Math.pow((this.y - y),2) <= this.r*this.r;
        if(temp == true){
            System.out.println("The point inside circle.");
        }
        else{
            System.out.println("The point outside circle.");
        }
    }
                                                    //圆圆重叠判断方法
    public void contains(Circle2D circle){
        if((Math.pow((circle.x - this.x),2) + Math.pow((circle.y - this.y),2)) <= Math.pow((circle.r - this.r),2)){
            System.out.println("The circle is inside another circle");
        }
        else{
            System.out.println("The circle is not inside another circle");
        }
    }
                                                    //圆圆相交方法
    public void overlaps(Circle2D circle){
        if((Math.pow((circle.x-this.x),2) + Math.pow((circle.y-this.y),2)) > Math.pow((circle.r-this.r),2) && (Math.pow((circle.x-this.x),2) + Math.pow((circle.y-this.y),2)) < Math.pow((circle.r+this.r),2)){
            System.out.println("The circle overlaps another circle");
        }
        else{
            System.out.println("The circle does not overlap with another circle");
        }
    }

                                                            //测试
    public static void main(String args[]){
        Circle2D circle = new Circle2D(2,3,5.5);
        System.out.println("面积:" + circle.getErea());
        System.out.println("周长:" + circle.getPerimeter());
        circle.contains(3,3);
        circle.contains(new Circle2D(4,5,10.5));
        circle.overlaps(new Circle2D(5,3,2.3));
    }
}

大概就是这样了,

结尾有话说:本人也是一个刚学Java的小新萌,哪里写的不太好欢迎指出,哈哈,大家一起学习一起进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值