Three lengths form a triangle only if they are all positive and the sum of any two is greater than the third. Write:
- a checked exception
IllegalTriangleException extends Exceptionthat receives the three sides, builds the messageSides 1.0, 2.0 and 5.0 cannot form a triangle, and offersgetLongestSide(); - a class
CheckedTrianglewhose constructor is declaredthrows IllegalTriangleExceptionand throws it for impossible sides; withgetPerimeter()andgetArea()(Heron's formula); - a public class
TriangleCheckthat reads triples of sides until the end of the input, tries to build each triangle, and prints either its perimeter and area or the rejection, then the totals.
For the input
3 4 5
1 2 5
2 2 2
0 4 4
10 1 1
the program prints
Triangle 3.0 4.0 5.0: perimeter 12.00, area 6.00
Rejected: Sides 1.0, 2.0 and 5.0 cannot form a triangle (longest side 5.0)
Triangle 2.0 2.0 2.0: perimeter 6.00, area 1.73
Rejected: Sides 0.0, 4.0 and 4.0 cannot form a triangle (longest side 4.0)
Rejected: Sides 10.0, 1.0 and 1.0 cannot form a triangle (longest side 10.0)
2 triangle(s) built, 3 rejected