(三角形类)设计一个扩展自抽象类 Geometricobject 的新的Triangle类。绘制Triangle类和Geometricobject类的UML 图并实现Triangle类。编写一个测试程序,提示用户输入三角形的三条边、一种颜色以及一个表明该三角形x下填充的布尔值。程序应该根据用户的输入,使用这些边以及颜色和是否填充的信息,创建一个 Triangle 对象。程序应该显示面积、周长颜色以及真或者假来表明是否被填充。
时间: 2024-03-03 09:48:44 浏览: 67
好的,请稍等一下,我会根据您的要求进行编写。
首先是 Geometricobject 类的 UML 图:
```
+------------------------+
| Geometricobject |
+------------------------+
|+color: String |
|+filled: boolean |
|+dateCreated: java.util.Date|
+------------------------+
|+Geometricobject() |
|+Geometricobject(color: String, filled: boolean)|
|+getColor(): String |
|+setColor(color: String): void|
|+isFilled(): boolean |
|+setFilled(filled: boolean): void|
|+getDateCreated(): java.util.Date|
|+toString(): String |
|+getArea(): double |
|+getPerimeter(): double |
+------------------------+
```
接下来是 Triangle 类的 UML 图:
```
+------------------------+
| Triangle |
+------------------------+
|-side1: double |
|-side2: double |
|-side3: double |
+------------------------+
|+Triangle() |
|+Triangle(side1: double, side2: double, side3: double, color: String, filled: boolean)|
|+getSide1(): double |
|+setSide1(side1: double): void|
|+getSide2(): double |
|+setSide2(side2: double): void|
|+getSide3(): double |
|+setSide3(side3: double): void|
|+getArea(): double |
|+getPerimeter(): double |
+------------------------+
```
下面是 Triangle 类的实现:
```java
public class Triangle extends Geometricobject {
private double side1;
private double side2;
private double side3;
public Triangle() {
}
public Triangle(double side1, double side2, double side3, String color, boolean filled) {
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 p = getPerimeter() / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
}
```
最后是测试程序:
```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, color, 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"));
}
}
```
阅读全文