Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please include comments in the program . The program must be able to be compiled

ID: 3629497 • Letter: P

Question

Please include comments in the program .
The program must be able to be compiled.


Design a class named Triangle that extends GeometricObject. This class contains:

* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.
* A no-arg constructor that creates a default triangle.
* A constructor that creates a triangle with the specified side1, side2, and side3.
* The accessor methods for all three data fields.
* A method named getArea() that returns the area of the triangle.
* A method named getPerimeter() that returns the perimeter of this triangle.
* A method name toString() that returns a string description for the triangle.

The GeometricObject class can be found on page 375 of the textbook (it is named GeometricObject1 there, but change the name).
The formula for computing the area of a triangle can be found pn page 68 of the textbook.

Write a test program that creates a Triangle object with sides 1, 1.5, 1, color yellow, and filled true, and displays the area, perimeter, color, and whether filled or not.

Explanation / Answer

class GeometricObject

{

private String color = "white";
private boolean filled;

public Geometricobject(String color, double weight)
{
this.color = color;
this.weight = weight;
}

//getColor()
public String getColor()
{
return color;
}

//setColor
public void setColor(String color)
{
this.color = color;
}

//getWeight
public double getWeight()
{
return weight;
}

//setWeight
public void setWeight(double weight)
{
this.weight = weight;
}

//for find area and perimeter
public abstract double findArea();
public abstract double findPerimeter();
}
}
//Triangle class extends Geometricobject
class Triangle extends Geometricobject
{
//variable declartion
double side1,side2,side3;
boolean filled;
//findArea()
public double findArea()
{
//varaible declartion
double area,s;
//calculatting s
s = ( side1 + side2 + side3 ) /2;
//calculatting area
area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3));
//return area
return area;
}//end findArea()

//Triangle
public Triangle(double s1,double s2,double s3)
{
side1=s1;
side2=s2;
side3=s3;
}
public void setFilled(boolean tri)
{
filled = tri;
}
public boolean getFilled()
{
return filled;
}


public double findPerimeter()
{
double perimeter;
//calculatting perimeter
perimeter = (side1 + side2 + side3);
return perimeter;
}
public String toString()
{
return "Triangle: side1 = " + side1 + " side2 = " + side2 + "side3 = " + side3;
}

public static void main(String args[])
{
Triangle tri = new Triangle(1, 1.5, 1);
tri.setColor("yellow");
tri.setFilled(true);

System.out.println("The area is " + tri.findArea());
System.out.println("The perimeter is " + tri.findPerimeter());
System.out.println(" color is " + tri.getColor());
System.out.println("filled " + tri.getFilled());
}
}