(Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides, and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not
时间: 2024-03-22 13:41:36 浏览: 112
stb-master_lostz1g_opengl_learnopengl_learnOpenGL-master_triangl
Sure! Here's the UML diagram for the Triangle and GeometricObject classes:
```
--------------------- --------------------
| GeometricObject | | Triangle |
--------------------- --------------------
|+color: String | |-side1: double |
|+filled: boolean | |-side2: double |
|+GeometricObject() | |-side3: double |
|+GeometricObject(color: String, filled: boolean)| |-Triangle(side1: double, side2: double, side3: double)|
|+getColor(): String | |+getSide1(): double|
|+setColor(color: String): void| |+getSide2(): double|
|+isFilled(): boolean | |+getSide3(): double|
|+setFilled(filled: boolean): void| |+getArea(): double|
|+getPerimeter(): double| |+getPerimeter(): double|
|+toString(): String | |+toString(): String|
--------------------- --------------------
```
And here's the implementation of the Triangle class:
```java
public class Triangle extends GeometricObject {
private double side1, side2, side3;
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 double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
}
}
```
And here's the test program:
```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 a 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());
}
}
```
Hope that helps! Let me know if you have any other questions.
阅读全文