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: 3753354 • 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 toString() method is implemented as follows:

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

The program should then create a Triangle object with these sides and set the colour and filled properties using the input. The program should display the area, perimeter, colour, true or false to indicate whether it is filled or not, and the string description for the triangle.

Sample runs:

Enter three sides: 1 2 3

Invalid triangle. Enter three sides: 2 3 1

Invalid triangle. Enter three sides: 3 1 2

Invalid triangle. Enter three sides: 3 4 5

Enter the color: yellow

yellow is an invalid color. Enter the color: blue

Enter a boolean value for filled: 0

0 is an invalid boolean value. Enter the boolean value for filled: 1

1 is an invalid boolean value. Enter the boolean value for filled: False

The area is 6.0

The perimeter is 12.0

The color is blue

Is filled? false

Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0

Enter three sides: 3 4 5

Enter the color: red

Enter a boolean value for filled: true

The area is 6.0

The perimeter is 12.0

The color is red

Is filled? true

Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0

Explanation / Answer

GeometricObject.java

public abstract class GeometricObject {
//Declaring abstract methods
public abstract double getPerimeter();
public abstract double getArea();

}

__________________

Triangle.java

public class Triangle extends GeometricObject {

//Declaring instance variables
double side1,side2,side3;

//Zero argumented constructor
public Triangle() {
super();
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}

//Parameterized constructor
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}


//getters and setters
public double getSide1() {
return side1;
}

public void setSide1(double side1) {
this.side1 = side1;
}

public double getSide2() {
return side2;
}

public void setSide2(double side2) {
this.side2 = side2;
}

public double getSide3() {
return side3;
}

public void setSide3(double side3) {
this.side3 = side3;
}

//This method will calculates the perimeter of the Triangle
@Override
public double getPerimeter() {

return side1+side2+side3;
}

//This method will calculates the area of the Triangle
@Override
public double getArea() {
double p=(side1+side2+side3)/2;
double area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
return area;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Triangle :side1=" + side1 + ", side2=" + side2 + ", side3="+ side3;
}

}

_______________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
//Declaring variables
double side1,side2,side3;

//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);

//getting the inputs entered by the user
System.out.print("Enter side1 :");
side1=sc.nextDouble();

System.out.print("Enter side2 :");
side2=sc.nextDouble();

System.out.print("Enter side3 :");
side3=sc.nextDouble();

//Creating an object of Triangle class by pasing the user entered inputs
Triangle t=new Triangle(side1, side2, side3);

//Displaying the area of the Triangle
System.out.printf("Area of The Triangle is :%.2f ",t.getArea());

//Displaying the Perimeter of the Triangle
System.out.println("Perimeter of The Triangle is :"+t.getPerimeter());

}

}

______________________

Output:

Enter side1 :4.5
Enter side2 :5.0
Enter side3 :5.5
Area of The Triangle is :10.61
Perimeter of The Triangle is :15.0

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