Write a class named Triangle in Java that extends class GeometricObject. The Tri
ID: 671206 • Letter: W
Question
Write a class named Triangle in Java that extends class GeometricObject. The Triangle class contains: Three double properties named sidel, 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 sidel, side2, and side3 The get methods for all three properties. A method named getArea that returns the area of this triangle. . . Use following formula to compute the area of a triangle. o s = (side 1 + side2 + side3) / 2 area(s (s side1) (s - side2) (s - side3)) A method named get Perimeter0 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: side 1 = " + side 1 + " side2 = " + side2 +" side3 " + Below is the source code for the GeometricObject class. public class Geometricobject private String color = "white"; private boolean filled; /*Default construetor / protected Geometricobject) /Another constructor protected Geometricobject (String color, boolean filled) this.colorcolor this.filled filled; public String getcolor O return color public void setColor (String color) this.color = color; public boolean isFilled) I return filled; public void setFilled (boolean filled) this.filled-filled; 1/2Explanation / Answer
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
/* Default constructor */
public Triangle() {
}
/* Another constructors */
public Triangle(double side1, double side2, double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
/* Get Methods for all three properties */
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
/* To calculate Perimeter of a Triangle */
public double getPerimeter() {
return (side1 + side2 + side3);
}
/* To calculate area of a Triangle */
public double getArea() {
int s = getPerimeter() / 2;
return Math.sqrt(s * ((s-side1)*(s-side2)*(s-side3)));
}
/* Description of Triangle for result */
public String toString() {
return "Triangle: Side1 = "+side1+ "side2 = "+side2+"side3 = "+side3+" "+
"The Triangle area is "+getArea()+" The Triangle Perimeter is"+getPerimeter();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.