设计抽象类GeometricObject及其子类Triangle和Circle。 GeometricObject类设计要求如下: ■ 一个名为color 的Color类型的私有数据域,表示对象的颜色。 ■ 一个名为filled 的Boolean类型的私有数据域,表示对象是否填充颜色。 ■ 一个名为dateCreated的Date类型的私有数据域,表示对象的创建日期。 ■ 一个无参构造方法。 ■ 一个能创建特定color 和filled 的有参构造方法。 ■ 相关数据域的访问器和修改器。 ■ 两个个名为draw和erase的抽象方法。 ■ 一个名为getArea的抽象方法。 ■ 一个名为getPerimeter的抽象方法。 ■ 重写toString方法。
时间: 2024-03-02 12:51:01 浏览: 28
好的,根据您的要求,我来为您设计这个类及其子类。
```java
import java.util.Date;
public abstract class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
public GeometricObject() {
this.color = "white";
this.filled = false;
this.dateCreated = new Date();
}
public GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
this.dateCreated = new Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
public abstract void draw();
public abstract void erase();
public abstract double getArea();
public abstract double getPerimeter();
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
}
class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(String color, boolean filled, double side1, double side2, double side3) {
super(color, filled);
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 s = (side1 + side2 + side3) / 2.0;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public void draw() {
System.out.println("Drawing a triangle...");
}
@Override
public void erase() {
System.out.println("Erasing a triangle...");
}
@Override
public String toString() {
return "Triangle:\n" + super.toString() + "\nside1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3;
}
}
class Circle extends GeometricObject {
private double radius;
public Circle() {
super();
this.radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(String color, boolean filled, double radius) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2.0 * Math.PI * radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle...");
}
@Override
public void erase() {
System.out.println("Erasing a circle...");
}
@Override
public String toString() {
return "Circle:\n" + super.toString() + "\nradius = " + radius;
}
}
```
以上就是我为您设计的GeometricObject类及其子类Triangle和Circle。如果您有任何问题或者需要进一步帮助,请随时告诉我。
阅读全文