构造方法:
没有参数,构造半径为 1,圆心为原点的圆。
指定半径和圆心,根据指定的半径和圆心构造圆。
半径
圆心(Point)
周长getPrimtere
面积gertArea
距离distance:如果距离小于半径之和,返回“相交”。
如果距离等于半径之和,返回“相切”。
如果距离大于半径之和,返回“相离”。
测试类,创建两个圆,求出每个圆的周长,面积和两个圆的关系
可以根据题目提示定义圆的各属性和方法,在写个测试类即可。
代码如下:
参考链接:
package circletest; // 改为你存放代码的包名,无包名则去掉此行
public class CircleTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//为了便于判断两圆关系 ,将两圆的纵坐标y都定位1,以及半径都定位1,通过改变横坐标x的值来判断两圆关系的方法是否正确
Circle c1 = new Circle(1,new Point(1,1));
Circle c2 = new Circle(1,new Point(3,1));
System.out.println("圆c1:");
System.out.println(c1); // 打印圆c1的周长和面积属性
System.out.println("圆c2:");
System.out.println(c2); // 打印圆c2的周长和面积属性
// 打印圆c1和圆c2的关系
System.out.println("圆c1和圆c2的关系为:"+c1.circleRelation(c2));
}
}
class Point{ // 坐标类Point,用于定义一个点的横坐标和纵坐标的属性
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = 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;
}
@Override
public String toString() {
return " [x=" + this.getX() + ", y=" + this.getY() + "]";
}
}
class Circle{ // 圆形类
private double r; //半径
private Point circleCenter; // 圆心
private double girth; // 周长
private double area; // 面积
public Circle() { // 无参的构造方法
this.r = 1; // 半径为1
this.circleCenter = new Point(0,0); // 圆心为原点
// https://blog.csdn.net/m0_67391907/article/details/126737857
// 求周长,结果保留两位小数
double area = 3.14*this.r*this.r;
String areaStr = String.format("%.2f", area);
double areaFinal = Double.parseDouble(areaStr);
this.setArea(areaFinal);
// 求面积,结果保留两位小数
double girth = 2*3.14*this.r;
String girthStr = String.format("%.2f", girth);
double girthFinal = Double.parseDouble(girthStr);
this.setPrimtere(girthFinal);
}
public Circle(double r, Point circleCenter) {// 指定半径和圆心的构造方法
this.r = r;
this.circleCenter = circleCenter;
// 同上,面积和周长都保留两位小数
double area = 3.14*this.r*this.r;
String areaStr = String.format("%.2f", area);
double areaFinal = Double.parseDouble(areaStr);
this.setArea(areaFinal);
double girth = 2*3.14*this.r;
String girthStr = String.format("%.2f", girth);
double girthFinal = Double.parseDouble(girthStr);
this.setPrimtere(girthFinal);
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Point getCircleCenter() {
return circleCenter;
}
public void setCircleCenter(Point circleCenter) {
this.circleCenter = circleCenter;
}
public double getPrimtere() {
return girth;
}
public void setPrimtere(double girth) {
this.girth = girth;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
@Override
// 打印圆的半径,圆心,周长和面积属性,如果只要打印周长和面积,可以把另外两个属性的打印去掉
public String toString() {
return "圆的属性 [圆心"+this.getCircleCenter()+", 半径=" + this.getR() + ", 周长=" + this.getPrimtere() + ", 面积=" + this.getArea() + "]";
}
// 判断当前对象和圆对象c的关系
public String circleRelation(Circle c2) {
Circle c1 = this;
// https://www.xz577.com/ask/346362.html
// https://www.xz577.com/ask/346420.html
// 求两圆的距离
double distance = Math.sqrt(Math.pow((c1.getCircleCenter().getX()-c2.getCircleCenter().getX()),2)+
Math.pow((c1.getCircleCenter().getY()-c2.getCircleCenter().getY()),2));
// 通过判断两圆的距离与其半径和的关系,来判断两圆的关系
if (distance<(c1.getR()+c2.getR())) {
return "相交";
}else if(distance==(c1.getR()+c2.getR())){
return "相切";
}else {
return "相离";
}
}
}
using System;
public class Point
{
public double X { get; set; }
public double Y { get; set; }
}
public class Circle
{
public double Radius { get; set; }
public Point Center { get; set; }
public Circle()
{
Radius = 1;
Center = new Point() { X = 0, Y = 0 };
}
public Circle(double radius, Point center)
{
Radius = radius;
Center = center;
}
public double GetPerimeter()
{
return 2 * Math.PI * Radius;
}
public double GetArea()
{
return Math.PI * Radius * Radius;
}
public string GetDistanceStatus(Circle other)
{
double distance = GetDistance(other);
if (distance < Radius + other.Radius)
{
return "相交";
}
else if (distance == Radius + other.Radius)
{
return "相切";
}
else
{
return "相离";
}
}
private double GetDistance(Circle other)
{
double dx = Center.X - other.Center.X;
double dy = Center.Y - other.Center.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
}
public class CircleTest
{
static void Main(string[] args)
{
Circle circle1 = new Circle();
Circle circle2 = new Circle(2, new Point() { X = 2, Y = 2 });
Console.WriteLine($"Circle 1 perimeter: {circle1.GetPerimeter()}");
Console.WriteLine($"Circle 1 area: {circle1.GetArea()}");
Console.WriteLine($"Circle 2 perimeter: {circle2.GetPerimeter()}");
Console.WriteLine($"Circle 2 area: {circle2.GetArea()}");
Console.WriteLine($"Circle 1 and circle 2 are {circle1.GetDistanceStatus(circle2)}");
}
}
Circle 类具有两个构造函数,一个不带参数(默认构造函数),另一个带有半径和圆心参数。它还具有计算周长、面积和圆之间距离的方法。测试类 CircleTest 实例化了两个圆,并计算了它们的周长、面积和圆之间的距离关系。
测试结果:
Circle 1 perimeter: 6.283185307179586
Circle 1 area: 3.141592653589793
Circle 2 perimeter: 12.566370614359172
Circle 2 area: 12.566370614359172
Circle 1 and circle 2 are 相交
回答不易,望采纳😘
不知道你这个问题是否已经解决, 如果还没有解决的话: