Write a class named Triangle in Java that extends class GeometricObject. The Triangle class contains:
• Three double properties named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
• A no-arg constructor that creates a default triangle.
• A constructor that creates a triangle with the specified side1, side2, and side3.
• The get methods for all three properties.
• A method named getArea() that returns the area of this triangle.
o Use following formula to compute the area of a triangle.
s = (side1 + side2 + side3) / 2
• A method named getPerimeter() that returns the perimeter of this triangle.
• A method named toString() that returns a string description for the triangle.
o The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;
Below is the source code for the GeometricObject class.
public class GeometricObject {
private String color = "white";
private boolean filled;
/* Default constructor */
protected GeometricObject() {
}
/* Another constructor */
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
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;
}
}
The test class has been provided below.
public class TestTriangle {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 2.5, 4);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}
}
2/2