java代码 版本1: 设计抽象类GeometricObject及其子类Triangle和Circle。 GeometricObject类设计要求如下: ■ 一个名为color 的Color类型的私有数据域,表示对象的颜色。 ■ 一个名为filled 的Boolean类型的私有数据域,表示对象是否填充颜色。 ■ 一个名为dateCreated的Date类型的私有数据域,表示对象的创建日期。 ■ 一个无参构造方法。 ■ 一个能创建特定color 和filled 的有参构造方法。 ■ 相关数据域的访问器和修改
时间: 2024-03-24 14:41:35 浏览: 151
此提示要求几何老师协助创建有助于学生理解基本几何概念的课程计划
下面是 Java 代码实现:
GeometricObject.java
```java
import java.awt.Color;
import java.util.Date;
public abstract class GeometricObject {
private Color color;
private boolean filled;
private Date dateCreated;
public GeometricObject() {
this.color = Color.BLACK;
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 color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
public abstract double getArea();
public abstract double getPerimeter();
}
```
Triangle.java
```java
public class Triangle extends GeometricObject {
private double side1, side2, side3;
public Triangle() {
super();
this.side1 = 1;
this.side2 = 1;
this.side3 = 1;
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(Color 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;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
}
```
Circle.java
```java
public class Circle extends GeometricObject {
private double radius;
public Circle() {
super();
this.radius = 1;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(Color 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 * Math.PI * radius;
}
}
```
这里我们定义了一个抽象类GeometricObject,其中包含了三个私有数据域color,filled和dateCreated以及两个构造方法和相关的访问器和修改器。同时,GeometricObject还包含两个抽象方法getArea和getPerimeter,分别用于计算几何对象的面积和周长。在Triangle和Circle两个子类中,分别实现了这两个抽象方法,并定义了各自的属性和行为。
阅读全文