一道java题
程序员文章站
2022-03-09 16:09:31
...
题目:
解题思路:
按照题目中走,需要写五个方法,一个测试;五个方法分别求周长、面积、点圆关系、以及圆圆重叠和相交。则有代码如下
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的小新萌,哪里写的不太好欢迎指出,哈哈,大家一起学习一起进步
上一篇: 一道 Hive题