要求如下:
- 可以生成具有特定圆心、半径的圆对象
- 提供可以设置成员变量的方法
- 计算一个点(Point)对象是否在圆(Circle)对象内
源代码如下:
import java.util.Scanner;
class Point{//Point点类,用于确定一个点的坐标
double x;//x坐标
double y;//y坐标
public Point(double x,double y) {
this.x = x;
this.y = y;
}
void showPoint() {//显示点的坐标
System.out.print("点的坐标为: ");
System.out.println("(" + x + "," + y + ")");
}
}
public class Circle {//Circle圆类,用于确定一个圆
private double radius;//圆的半径
private Point point;//为类Point定义对象point作为圆的圆心坐标
public Circle(Point point,double radius) {
this.point = point;
this.radius = radius;
}
void showCircle(){//显示圆的圆心坐标和半径
System.out.print("圆心坐标为: ");
System.out.println("(" + point.x + "," + point.y + ")");
System.out.print("圆的半径为: ");
System.out.println(radius);
}
void judgement(Point a,Circle b){//判断点与圆的关系(包括圆外、圆上、圆内)
if((a.x-point.x)*(a.x-point.x)+(a.y-point.y)*(a.y-point.y)>(b.radius*b.radius))
System.out.print("点在圆外!");
else if((a.x-point.x)*(a.x-point.x)+(a.y-point.y)*(a.y-point.y)==(b.radius*b.radius))
System.out.print("点在圆上!");
else if((a.x-point.x)*(a.x-point.x)+(a.y-point.y)*(a.y-point.y)<(b.radius*b.radius))
System.out.print("点在圆内!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);//键盘输入
System.out.print("请输入点的坐标(x坐标、y坐标) : ");//输入提示语
Point point1 = new Point(in.nextDouble(),in.nextDouble());//点的坐标
System.out.print("请输入圆的坐标(x坐标、y坐标、半径): ");//输入提示语
Point point2 = new Point(in.nextDouble(),in.nextDouble());//圆心坐标
Circle circle1 = new Circle(point2,in.nextDouble());
point1.showPoint();
circle1.showCircle();
circle1.judgement(point1, circle1);
in.close();
}
}