根据给定的GeometricObject类层次图,分别实现GeometricObject、Circle和Triangle类。要求: (1)按照类图,定义各类的数据域属性和方法。 (2)默认的GeometricObject的颜色为white。默认的Circle对象的半径为10。默认的Triangle对象的三边为(3,4,5)。 (3)GeometricObject类的toString()方法,返回对象的颜色信息。Circle和Triangle的toString()方法,覆盖父类的toString(),分别返回颜色、半径或边长的信息。 (4)GeometricObject类实现java.lang.Comparable接口,覆盖public int compareTo()方法,实现两个几何对象在周长上的比较。 (5)Circle和Triangle类分别覆盖实现getPerimeter()方法。
时间: 2024-03-10 13:49:18 浏览: 232
设计一个扩展自抽象类GeometricObject 的新的Triangle 类
5星 · 资源好评率100%
以下是GeometricObject类的实现:
```java
public abstract class GeometricObject implements Comparable<GeometricObject> {
private String color;
public GeometricObject() {
this.color = "white";
}
public GeometricObject(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public abstract double getArea();
public abstract double getPerimeter();
@Override
public String toString() {
return "GeometricObject{" +
"color='" + color + '\'' +
'}';
}
@Override
public int compareTo(GeometricObject o) {
if (this.getPerimeter() > o.getPerimeter()) {
return 1;
} else if (this.getPerimeter() < o.getPerimeter()) {
return -1;
} else {
return 0;
}
}
}
```
以下是Circle类的实现:
```java
public class Circle extends GeometricObject {
private double radius;
public Circle() {
super();
this.radius = 10;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle{" +
"color='" + getColor() + '\'' +
", radius=" + radius +
'}';
}
}
```
以下是Triangle类的实现:
```java
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
this.side1 = 3;
this.side2 = 4;
this.side3 = 5;
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(double side1, double side2, double side3, String color) {
super(color);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public double getArea() {
double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public String toString() {
return "Triangle{" +
"color='" + getColor() + '\'' +
", side1=" + side1 +
", side2=" + side2 +
", side3=" + side3 +
'}';
}
}
```
阅读全文