JAVA编程,定义一个(圆)类并添加一个方法,计算(点)对象是否在圆内,并写程序验证。需要完整步骤!
1个回答
展开全部
Java程序:
import java.awt.Point;
public class Test {
public static void main(String[] args) {
Circle c = new Circle(new Point(0, 0), 2);
Point p = new Point(1, 1);
if(c.isInner(p)) {
System.out.println("坐标点p在圆内");
}
else {
System.out.println("坐标点p不在圆内");
}
}
}
class Circle {
/**
* 圆心坐标
*/
protected Point center;
/**
* 圆半径
*/
protected double radius;
public Circle(Point center, double radius) {
this.center = new Point(center.x, center.y);
this.radius = radius;
}
/**
* 判断p是否在圆内
* @param p 待判断点
* @return 是否在圆内
*/
public boolean isInner(Point p) {
double dist = 0;
dist = Math.sqrt((p.x - center.x) * (p.x - center.x) + (p.y - center.y) * (p.y - center.y));
return (dist <= radius ? true : false);
}
}
运行测试:
坐标点p在圆内
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询