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

Exercise 11.1: (The Triangle class) For the formula to compute the area of a tri

ID: 3913844 • Letter: E

Question

Exercise 11.1:   (The Triangle class)

For the formula to compute the area of a triangle, see Programming Exercise 2.19.

The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes.

Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

HINT: Do not forget to Override the methods getArea() and getPerimeter(), like you do the toString().

@Override
public String toString()

Explanation / Answer

Trinalge class:

//Triangle class-STARTS

public class Triangle extends GeometricObject

{

//Three double data fields named side1, side2, and side3

private double side1,side2,side3;

private String color;

private boolean filled;

//A no-arg constructor that creates a default triangle.

public Triangle()

{

side1=side2=side3=1.0;

}

//A constructor that creates a triangle with the specified side1, side2, and side3.

public Triangle(double side1,double side2,double side3)

{

this.side1=side1;

this.side2=side2;

this.side3=side3;

}

//The accessor methods for all three data fields.

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;

}

public void setColor(String color)

{

this.color=color;

}

public String getColor()

{

return this.color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}

//A method named getArea() that returns the area of this triangle.

public double getArea()

{

//finding half of the perimeter

double p=this.getPerimeter()/2;

//Calculating area using p value

double area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));

//returning area

return area;

}

//A method named getPerimeter() that returns the perimeter of this triangle.

public double getPerimeter()

{

//returning perimeter

return side1+side2+side3;

}

//A method named toString() that returns a string description for the triangle.

public String toString()

{

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

}

}//Triangle class-ENDS

Test class:

import java.util.Scanner;

//Testing class-Driver class

public class TestTriangle

{

//main method-Driver method

public static void main(String[] args)

{

//Scanner class to read inputs from user

Scanner scan = new Scanner(System.in);

//prompts the user to enter three sides of the triangle

System.out.print("Enter side1 of the triangle:");

double side1=scan.nextDouble();

System.out.print("Enter side2 of the triangle:");

double side2=scan.nextDouble();

System.out.print("Enter side3 of the triangle:");

double side3=scan.nextDouble();

System.out.print("Enter color of the triangle:");

String color=scan.nextLine();

//skipping nextline

scan.next();

System.out.print("Triangle is filled or not?(Enter true,or flase)");

boolean isFilled=scan.nextBoolean();

//create a Triangle object with these sides

Triangle triangle = new Triangle(side1,side2,side3);

//setting color and filled values

triangle.setColor(color);

triangle.setFilled(isFilled);

System.out.println(" Area of triangle:"+triangle.getArea());

System.out.println(" Perimeter of triangle:"+triangle.getPerimeter());

System.out.println(" Color of triangle:"+triangle.getColor());

System.out.println(" Triangle filled:"+triangle.isFilled());

System.out.println(triangle.toString());

}

}

Output:

Enter side1 of the triangle:21
Enter side2 of the triangle:20
Enter side3 of the triangle:24
Enter color of the triangle:yellow
Triangle is filled or not?(Enter true,or flase)true

Area of triangle:199.27603343101748

Perimeter of triangle:65.0

Color of triangle:

Triangle filled:true
Triangle: side1 = 21.0 side2 = 20.0 side3 = 24.0