Java作业-判断两圆关系

来源:互联网 发布:全国大学生网络文化节 编辑:程序博客网 时间:2024/05/21 08:51

目标效果:


Circle类:

package two;public class Circle {private double centerX;      //圆点X坐标private double centerY;      //圆点Y坐标private double R;         //半径public Circle(double centerX, double centerY, double r) {super();this.centerX = centerX;this.centerY = centerY;R = r;}public Circle() {super();}public double getCenterX() {return centerX;}public void setCenterX(double centerX) {this.centerX = centerX;}public double getCenterY() {return centerY;}public void setCenterY(double centerY) {this.centerY = centerY;}public double getR() {return R;}public void setR(double r) {R = r;}}


TestCircle类:

/** * 以Point类为基础,定义一个平面中的Circle类:   1、编写一个无参的构造函数;   2、编写一个有参的构造函数;   3、在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实例c2,调用实例方法判断c1和c2是否相重叠。 @author Vivinia */package two;import java.util.Scanner;public class TestCircle {static Circle circleOne,circleTwo;public static void main(String[] args) {circleOne=new Circle();     //调用无参生成c1scanfCircle();          //手动输入c1的信息circleTwo=new Circle(3,5,2);    //实例化并写入judgeTwoCircle();}//手动输入c1的信息private static void scanfCircle() {System.out.println("请输入c1的横、纵坐标以及半径r:");Scanner input=new Scanner(System.in);circleOne.setCenterX(input.nextDouble());circleOne.setCenterY(input.nextDouble());circleOne.setR(input.nextDouble());}//判断两个圆的关系public static void judgeTwoCircle() {switch (cal()) {case 0:System.out.println("两个圆相离");break;case 1:System.out.println("两个圆外切");break;case 2:System.out.println("两个圆相交");break;case 3:System.out.println("两个圆内切");break;case 4:System.out.println("两个圆内含");break;default:System.out.println("sorry");break;}}//计算返回关系类型private static int cal() {double i;i=Math.sqrt((circleOne.getCenterX()-circleTwo.getCenterX())*(circleOne.getCenterX()-circleTwo.getCenterX())+(circleOne.getCenterY()-circleTwo.getCenterY())*(circleOne.getCenterY()-circleTwo.getCenterY()));if(i>circleOne.getR()+circleTwo.getR())    //相离return 0;else if(i==Math.abs(circleOne.getR()+circleTwo.getR()))    //外切return 1;else if(i>Math.abs(circleOne.getR()-circleTwo.getR())&&i<circleOne.getR()+circleTwo.getR())   //相交return 2;else if(i==Math.abs(circleOne.getR()-circleTwo.getR()))     //内切return 3;else             //内含return 4;}}


原创粉丝点击