Design a new Triangle class that extends the abstract Geometric Object class. Dr
ID: 3861435 • Letter: D
Question
Design a new Triangle class that extends the abstract Geometric Object class. Draw the UMI. diagram for the classes Triangle and Geometric Object and then implement the Triangle class. 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.Explanation / Answer
Triangle.java
public class Triangle
{
public double s1,s2,s3;
public String color;
public boolean filled;
public Triangle(double s1,double s2,double s3, String color, boolean filled)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.color = color;
this.filled = filled;
}
void displayinfo()
{
double p = s1 + s2 + s3;
System.out.println( "perimeter of Triangle: " + p);
p = p/2;
System.out.println( "Area of Triangle: " + Math.sqrt( p*(p-s1)*(p-s2)*(p-s3) ));
System.out.println( "color of Triangle: " + this.color);
System.out.println( "filled with color: " + this.filled) ;
}
}
driver.java
import java.util.Scanner;
public class driver
{
public static void main(String[] args)
{
float s1,s2,s3;
String color;
boolean filled;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length of side1:");
s1 = scanner.nextFloat();
System.out.println("Enter length of side2:");
s2 = scanner.nextFloat();
System.out.println("Enter length of side3:");
s3 = scanner.nextFloat();
System.out.println("Enter color of Trianlge:");
color = scanner.next();
System.out.println("Enter 1 if Trianlge is filled else 2:");
int a = scanner.nextInt();
if(a == 1)
{
filled = true;
}
else
{
filled = false;
}
Triangle T = new Triangle(s1,s2,s3,color,filled);
T.displayinfo();
}
}
Sample Output:
Enter length of side1:
3
Enter length of side2:
4
Enter length of side3:
5
Enter color of Trianlge:
Red
Enter 1 if Trianlge is filled else 2:
1
perimeter of Triangle: 12.0
Area of Triangle: 6.0
color of Triangle: Red
filled with color: true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.