Design an interface named Colorable
with a void method named howToColor()
.
Design a class named Triangle extending GeometricObject
and implementing Colorable
.
Triangle implements howToColor
method to display the message "Color all three sides".
Triangle implements thetoString
method to display its color and its sides.
In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the TriangleException
class, and complete and implement the constructor of the Triangle class to throw a TriangleException
object if a triangle is created with sides that violate the rule.
The output of the test program should be as indicated below.
abstract class GeometricObject {
private String color = "white";
protected GeometricObject(String color) {
this.color = color;
}
public String toString() {
return "Color: " + color;
}
public abstract double getPerimeter();
}
class Triangle {
private double side1, side2, side3;
public Triangle(double side1, double side2, double side3) throws TriangleException {
}
}
class TriangleException {
}
Code is testing:
try {
Triangle t1 = new Triangle(1.5, 2, 3);
System.out.println(t1.toString());
System.out.println("Perimeter for t1: " + t1.getPerimeter());
t1.howToColor();
Triangle t2 = new Triangle(1, 2, 3);
System.out.println(t2.toString());
System.out.println("Perimeter for t2: " + t2.getPerimeter());
t2.howToColor();
} catch (TriangleException ex) {
System.out.println(ex.getMessage());
System.out.println("Side1: " + ex.getSide1());
System.out.println("Side2: " + ex.getSide2());
System.out.println("Side3: " + ex.getSide3());
}
According to this code result must be:
Color: red Sides: 1.5, 2.0, 3.0
Perimeter for t1: 6.5
Color all three sides.
Triangle inequality violation!
Side1: 1.0
Side2: 2.0
Side3: 3.0
So it must be true for the input in the example. For example for t2.getPerimeter()
statement we have to create a getPerimeter method by return value of side1 + side2 + side3. But t2 part does not belong to us it belongs to the user.
I couldn't answer this question I need your help. In the first step, I got an error. I can solve other parts but I couldn't solve throws Exception part. I tested original version of code in Eclipse (without any changing) but I get a throws Exception error. If you try the code like me you will get error too. Now How can I solve this problem, I'm new in Java.
Replace class TriangleException
with class TriangleException extends Exception
. All user-defined exception should be subclasses of Exception
.