JAVA 请问各位大佬这道题怎么做~
1、设计Circle类:定义相应的字段、构造方法和成员方法(新增的都是静态类成员),具体如下表。要求:1)在主类中调用不同的构造函数创建多个Circle类的对象;2)调用...
1、 设计Circle类:定义相应的字段、构造方法和成员方法(新增的都是静态类成员),具体如下表。要求:1)在主类中调用不同的构造函数创建多个Circle类的对象; 2)调用成员方法和类方法,验证这些方法的使用方式。
展开
1个回答
展开全部
/**
* by sleest 2019/05/28, 仅供参考
* 我没有写注释, 希望您能自己梳理一遍
* 题目不难, 此案例能够很好的帮助您理解封装重载和静态实例化等一些相关概念
* 位置方法里需要考虑内外交, 内外离, 稍微多点代码
* 不要停止思考.
*/
public class Circle {
private int x;
private int y;
private int radius;
private static int COUNT = 0;
private Circle() {
}
public Circle(int radius) {
ensureRadiusLT0(radius);
this.x = 0;
this.y = 0;
this.radius = radius;
COUNT++;
}
public Circle(int x, int y) {
this.x = x;
this.y = y;
this.radius = 1;
COUNT++;
}
public Circle(int x, int y, int radius) {
ensureRadiusLT0(radius);
this.x = x;
this.y = y;
this.radius = radius;
COUNT++;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
private void ensureRadiusLT0(int radius) {
if (radius <= 0) {
throw new IllegalArgumentException("半径需大于0");
}
}
private void ensureAnotherCircleNotEmpty(Circle another) {
if (another == null) {
throw new IllegalArgumentException("另一个圆不能为空");
}
}
public double getArea() {
return Math.PI * this.radius * this.radius;
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
public double distanceWith(Circle other) {
ensureAnotherCircleNotEmpty(other);
return Math.sqrt(Math.pow(this.x - other.getX(), 2) + Math.pow(this.y - other.getY(), 2));
}
public static double distanceWith(Circle c1, Circle c2) {
if (c1 == null || c2 == null) {
throw new IllegalArgumentException("参数圆不能为空");
}
return Math.sqrt(Math.pow(c1.getX() - c2.getX(), 2) + Math.pow(c1.getY() - c2.getY(), 2));
}
public String positionWith(Circle other) {
ensureAnotherCircleNotEmpty(other);
if (this.x == other.getX() && this.y == other.getY()) {
return "同心";
}
double distance = distanceWith(other);
double absRadiusDiff = Math.abs(this.radius - other.getRadius());
double sumRadius = this.radius + other.getRadius();
if (distance > sumRadius || distance < absRadiusDiff) {
return "相离";
} else if (distance == sumRadius || distance == absRadiusDiff) {
return "相切";
} else if (distance < sumRadius && distance > absRadiusDiff) {
return "相交";
} else {
return "未考虑到的情况";
}
}
public static String positionWith(Circle c1, Circle c2) {
if (c1 == null || c2 == null) {
throw new IllegalArgumentException("参数圆不能为空");
}
if (c1.getX() == c2.getX() && c1.getY() == c2.getY()) {
return "同心";
}
double distance = distanceWith(c1, c2);
double absRadiusDiff = Math.abs(c1.getRadius() - c2.getRadius());
double sumRadius = c1.getRadius() + c2.getRadius();
if (distance > sumRadius || distance < absRadiusDiff) {
return "相离";
} else if (distance == sumRadius || distance == absRadiusDiff) {
return "相切";
} else if (distance < sumRadius && distance > absRadiusDiff) {
return "相交";
} else {
return "未考虑到的情况";
}
}
public static int getCount() {
return COUNT;
}
@Override
public String toString() {
return new StringBuilder("圆[半径为: ").append(this.radius).append(", 坐标为(").append(this.x).append(", ")
.append(this.y).append("), 实例为: ").append(super.toString())
.append("]").toString();
}
public static void main(String[] args) {
Circle c1 = new Circle(5, 0);
Circle c2 = new Circle(6);
Circle c3 = new Circle(4, 4, 1);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println();
System.out.println(String.format("实例化了%d个圆", Circle.getCount()));
System.out.println();
System.out.println(String.format("c1面积为: %f", c1.getArea()));
System.out.println(String.format("c3周长为: %f", c3.getPerimeter()));
System.out.println();
System.out.println(String.format("c1和c2的位置关系为: %s", c1.positionWith(c2)));
System.out.println(String.format("c1和c3的位置关系为: %s", Circle.positionWith(c1, c3)));
System.out.println(String.format("c2和c3的位置关系为: %s", Circle.positionWith(c2, c3)));
}
}
执行main结果:
追问
多谢大佬~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询