98.1k views
4 votes
Create the IllegalTriangleException class, and modify the constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:

User Sonnia
by
3.3k points

1 Answer

5 votes

Complete question:

Create the Illegal Triangle Exception class, and modify the constructor of the Triangle class to throw an Illegal Triangle Exception object if a triangle is created with sides that violate the rule as follows:

/** Construct a triangle with the specified sides */

public Triangle (double side1, double side 2, double side 3)

throws IllegalTriangleException {

// Implement it

}

Answer:

We have to guess at the original Triangle class as you didn't provide it, so I'll provide a skeleton.

For a triangle to be "legal" all sides should have a positive, non-zero length; and the sum of any two sides should be greater than the length of the third.

public class IllegalTriangleException extends Exception {

public IllegalTriangleException() {}

public IllegalTriangleException(String msg) { super(msg); }

};

public class Triangle

{

// Missing info ...

private double a, b, c;

public Triangle (double side1, double side 2, double side3 )

throws IllegalTriangleException

side2 <= 0.0

};

Step-by-step explanation:

User Menahem Gil
by
3.4k points