Triangle类)设计一个继承了抽象类GeometricObject的新的Triangle类。绘制Triangle类和GeometricObject类的UML图并实现Triangle类。编写测试程序,提示用户输入三角形的三条边,颜色以及一个表明该三角形是否填充的布尔值。程序应该使用这些边来创建一个Triangle对象,并根据用户的输入来设置color和filled属性。程序应该显示面积,周长,颜色以及表明是否被填充的真或假的值
时间: 2023-06-19 14:09:08 浏览: 129
以下是Triangle类和GeometricObject类的UML图:
![Triangle类和GeometricObject类的UML图](https://img-blog.csdn.net/20180211232417604?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhlbmdhbmpp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
以下是Triangle类的实现:
```java
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
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;
}
@Override
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + 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 side1: ");
double side1 = input.nextDouble();
System.out.print("Enter side2: ");
double side2 = input.nextDouble();
System.out.print("Enter side3: ");
double side3 = input.nextDouble();
System.out.print("Enter color: ");
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("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Filled: " + triangle.isFilled());
}
}
```
阅读全文