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

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

ID: 3635705 • Letter: D

Question

Design a class named Triangle that extends GeometricObject. The class contains:
Three double data fields 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 accessor methods for all three data fields.
A method named getArea() that returns the area of this triangle.
A method named getPerimeter() that returns the perimeter of this triangle.
A method named toString() that returns a string description for the triangle.

The formula to compute the area of a triangle:
s = side1 + side2 + side3)/2;

area = v(s(s - side1)(s - side2)(s -side3))

The toString() method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;

Draw the UML diagram that involves the classes Triangle and GeometricObject. Implement the class. 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. Write all three classes in three separated files: FirstInitialLastNameProject4.java, GeometricObject.java, and Triangle.java
Coding:

public class FirstInitialLastNameProject4 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
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);
}
}

class GeometricObject {
// Copy it from the book
}

class Triangle extends GeometricObject {
// Implement it
}

Explanation / Answer

Please Rate:Thanks

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;
}

//findPerimeter()
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());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote