当前位置: 代码迷 >> 综合 >> Java---定义一个圆(Circle)类表示三维空间中的圆(两个成员变量:圆心Point类、半径)
  详细解决方案

Java---定义一个圆(Circle)类表示三维空间中的圆(两个成员变量:圆心Point类、半径)

热度:12   发布时间:2023-12-13 01:56:20.0

要求如下:

  1. 可以生成具有特定圆心、半径的圆对象
  2. 提供可以设置成员变量的方法
  3. 计算一个点(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 stubScanner 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();}}
  相关解决方案