为Cricle类添加一个方法,计算一个点(Point对象)是否在圆(Cricle对象)内,并写程序验证(java)
1个回答
展开全部
import java.util.Scanner;
class Point{
private double x;
private double y;
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 Point(double x, double y) {
this.x = x;
this.y = y;
}
}
public class Circle {
private Point point;
private double radius;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(Point point, double radius) {
this.point = point;
this.radius = radius;
}
public void check(Point point){
double widthX=point.getX()-this.getPoint().getX();
double widthY=point.getY()-this.getPoint().getY();
double distance=Math.sqrt(Math.pow(widthX, 2)+Math.pow(widthY, 2));
if(distance<this.getRadius()){
System.out.println("该点在圆内!");
}
else if(distance==this.getRadius()){
System.out.println("该点在圆上!");
}
else{
System.out.println("该点在圆外!");
}
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入要创建的圆的中心点X坐标:");
double centerX=input.nextDouble();
System.out.println("请输入要创建的圆的中心点y坐标:");
double centerY=input.nextDouble();
System.out.println("请输入要创建的圆的半径:");
double radius=input.nextDouble();
Point point1=new Point(centerX,centerY);
Circle circle=new Circle(point1,radius);
System.out.println("您创建的圆的中心点X坐标:"+point1.getX()+"y坐标:"+point1.getY()+"半径:"+circle.getRadius());
System.out.println("请输入要判断的点的X坐标:");
double pointX=input.nextDouble();
System.out.println("请输入要判断的点的y坐标:");
double pointY=input.nextDouble();
Point point2=new Point(pointX,pointY);
circle.check(point2);
}
}
class Point{
private double x;
private double y;
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 Point(double x, double y) {
this.x = x;
this.y = y;
}
}
public class Circle {
private Point point;
private double radius;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(Point point, double radius) {
this.point = point;
this.radius = radius;
}
public void check(Point point){
double widthX=point.getX()-this.getPoint().getX();
double widthY=point.getY()-this.getPoint().getY();
double distance=Math.sqrt(Math.pow(widthX, 2)+Math.pow(widthY, 2));
if(distance<this.getRadius()){
System.out.println("该点在圆内!");
}
else if(distance==this.getRadius()){
System.out.println("该点在圆上!");
}
else{
System.out.println("该点在圆外!");
}
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入要创建的圆的中心点X坐标:");
double centerX=input.nextDouble();
System.out.println("请输入要创建的圆的中心点y坐标:");
double centerY=input.nextDouble();
System.out.println("请输入要创建的圆的半径:");
double radius=input.nextDouble();
Point point1=new Point(centerX,centerY);
Circle circle=new Circle(point1,radius);
System.out.println("您创建的圆的中心点X坐标:"+point1.getX()+"y坐标:"+point1.getY()+"半径:"+circle.getRadius());
System.out.println("请输入要判断的点的X坐标:");
double pointX=input.nextDouble();
System.out.println("请输入要判断的点的y坐标:");
double pointY=input.nextDouble();
Point point2=new Point(pointX,pointY);
circle.check(point2);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询