画出Triangle类和GeometricObject类的UML图,并实现这些类。编写一个测试程序,提示用户输入三角形的三条边、颜色以及一个boolean值表明该三角形是否填充。程序需要根据输入创建一个具有指定边的三角形,并设置color和filled属性。程序需要显示面积、周长、颜色以及表明是否填充的真or假值
时间: 2024-03-06 21:47:50 浏览: 77
实验 8抽象类和接口的应用.zip
抱歉,作为一名语言模型AI,我无法提供画图的功能。但是,我可以给你提供Triangle类和GeometricObject类的UML图以及相应的Java代码实现:
**GeometricObject类的UML图:**
```
+GeometricObject
-color: String
-filled: boolean
-dateCreated: Date
+GeometricObject()
+GeometricObject(color: String, filled: boolean)
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): Date
+toString(): String
+getArea(): double
+getPerimeter(): double
```
**Triangle类的UML图:**
```
+Triangle
-side1: double
-side2: double
-side3: double
+Triangle()
+Triangle(side1: double, side2: double, side3: double)
+getSide1(): double
+setSide1(side1: double): void
+getSide2(): double
+setSide2(side2: double): void
+getSide3(): double
+setSide3(side3: double): void
+getArea(): double
+getPerimeter(): double
```
**GeometricObject类的Java代码实现:**
```java
import java.util.Date;
public class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
public GeometricObject() {
dateCreated = new Date();
}
public GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
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 String toString() {
return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
public double getArea() {
return 0;
}
public double getPerimeter() {
return 0;
}
}
```
**Triangle类的Java代码实现:**
```java
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
side1 = side2 = side3 = 1.0;
}
public Triangle(double side1, double side2, double side3) {
super();
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;
}
public double getArea() {
double s = (side1 + side2 + side3) / 2.0;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public double getPerimeter() {
return side1 + side2 + side3;
}
}
```
**测试程序的Java代码实现:**
```java
import java.util.Scanner;
public class TestTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides of the triangle: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.print("Enter the color of the triangle: ");
String color = input.next();
System.out.print("Is the triangle filled (true / false)? ");
boolean filled = input.nextBoolean();
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setColor(color);
triangle.setFilled(filled);
System.out.println("The area of the triangle is " + triangle.getArea());
System.out.println("The perimeter of the triangle is " + triangle.getPerimeter());
System.out.println("The color of the triangle is " + triangle.getColor());
System.out.println("The triangle is " + (triangle.isFilled() ? "filled" : "not filled"));
}
}
```
这个测试程序可以让用户输入三角形的三边、颜色和是否填充等信息,并根据用户输入创建一个具有指定边的三角形,并且设置color和filled属性。程序会显示面积、周长、颜色和是否填充的真假值。
阅读全文