In: Computer Science
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 to denote three sides of a triangle. • A no-arg constructor that creates a default triangle with default values 1.0. • A constructor that creates a triangle with the specified side1, side2, and side3. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule and throw an IllegalTriangleException object if a triangle is created with sides that violate the rule • 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. 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.
CODE IN JAVA:
IllegalTriangleException.java file:
public class IllegalTriangleException extends Exception{
public IllegalTriangleException(String message)
{
super(message);
}
}
GeometricObject.java file:
public abstract class GeometricObject {
private String color ;
private boolean filled ;
public GeometricObject() {
this.color = "white" ; //
default
this.filled = false ;
}
public GeometricObject(String color, boolean filled)
{
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
}
Triangle.java file:
public class Triangle extends GeometricObject{
private double side1 ;
private double side2 ;
private double side3 ;
public Triangle() {
super();
this.side1 = 1.0 ;
this.side2 = 1.0 ;
this.side3 = 1.0 ;
}
public Triangle(String color, boolean filled,
double side1, double side2, double side3) throws
IllegalTriangleException {
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
if((side1 + side2 ) <
side3)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
else if((side1 + side3) <
side2)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
else if((side2 + side3) <
side1)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getPerimeter() {
return this.side1 + this.side2 +
this.side3 ;
}
public double getArea() {
double s = (this.side1 + this.side2
+ this.side3) / 2 ;
return Math.sqrt(s * (s -
this.side1) * (s - this.side2 ) * (s - this.side3)) ;
}
public String toString() {
return "Triangle [side1=" + side1 +
", side2=" + side2 + ", side3=" + side3 + "]";
}
}
TriangleDemo.java file:
public class Triangle extends GeometricObject{
private double side1 ;
private double side2 ;
private double side3 ;
public Triangle() {
super();
this.side1 = 1.0 ;
this.side2 = 1.0 ;
this.side3 = 1.0 ;
}
public Triangle(String color, boolean filled,
double side1, double side2, double side3) throws
IllegalTriangleException {
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
if((side1 + side2 ) <
side3)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
else if((side1 + side3) <
side2)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
else if((side2 + side3) <
side1)
throw new
IllegalTriangleException("the sum of any two sides should be
greater than the other side");
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getPerimeter() {
return this.side1 + this.side2 +
this.side3 ;
}
public double getArea() {
double s = (this.side1 + this.side2
+ this.side3) / 2 ;
return Math.sqrt(s * (s -
this.side1) * (s - this.side2 ) * (s - this.side3)) ;
}
public String toString() {
return "Triangle [side1=" + side1 +
", side2=" + side2 + ", side3=" + side3 + "]";
}
}
OUTPUT: