设计抽象类GeometricObject及其子类Triangle和Circle。 GeometricObject类设计要求如下: ■ 一个名为color 的Color类型的私有数据域,表示对象的颜色。 ■ 一个名为filled 的Boolean类型的私有数据域,表示对象是否填充颜色。 ■ 一个名为dateCreated的Date类型的私有数据域,表示对象的创建日期。 ■ 一个无参构造方法。 ■ 一个能创建特定color 和filled 的有参构造方法。 ■ 相关数据域的访问器和修改器。 ■ 两个个名为draw和erase的抽象方法。 ■ 一个名为getArea的抽象方法。 ■ 一个名为getPerimeter的抽象方法。 ■ 重写toString方法。 java
时间: 2024-02-27 11:51:48 浏览: 170
抽象类GeometricObject的实现如下:
```java
import java.util.Date;
import java.awt.Color;
public abstract class GeometricObject {
private Color color;
private boolean filled;
private Date dateCreated;
public GeometricObject() {
this.color = Color.WHITE;
this.filled = false;
this.dateCreated = new Date();
}
public GeometricObject(Color color, boolean filled) {
this.color = color;
this.filled = filled;
this.dateCreated = new Date();
}
public Color getColor() {
return this.color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isFilled() {
return this.filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return this.dateCreated;
}
public abstract void draw();
public abstract void erase();
public abstract double getArea();
public abstract double getPerimeter();
@Override
public String toString() {
return "created on " + this.dateCreated + "\ncolor: " + this.color +
"\nfilled: " + this.filled;
}
}
```
Triangle子类的实现如下:
```java
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3, Color color, boolean filled) {
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return this.side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return this.side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return this.side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public void draw() {
System.out.println("Drawing triangle...");
}
@Override
public void erase() {
System.out.println("Erasing triangle...");
}
@Override
public double getArea() {
double s = (this.side1 + this.side2 + this.side3) / 2;
return Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3));
}
@Override
public double getPerimeter() {
return this.side1 + this.side2 + this.side3;
}
}
```
Circle子类的实现如下:
```java
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius, Color color, boolean filled) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing circle...");
}
@Override
public void erase() {
System.out.println("Erasing circle...");
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
}
```
阅读全文