Hey I am learning exceptions and this is what I am tasked. I have the following code, I believe I have done this right I just dont quite comprehend what I am doing
In a triangle, the sum of any two sides is greater than the other side. The Triangle2 class must adhere to this rule. Create the custom IllegalTriangleException class, and modify the constructor of the Triangle2 class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle2(double side1, double side2, double side3) throws IllegalTriangleException
{
// Implement it
}
You will make a custom exception class, IllegalTriangleException. See Listing 12.10, pg. 470, for an example. TestTriangle.java has been provided that has a main that you will need to add exception handling code. You will need to detect and throw the exception the constructor for class Triangle2. Triangle2 is already started in TestTriangle.java. The class is named Triangle2 to distinguish from the Triangle class from an earlier assignment.
public class TestTriangle {
public static void main(String[] args) throws IllegalTriangleException {
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
Triangle2 triangle = new Triangle2(side1, side2, side3);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is "
+ triangle.getPerimeter());
System.out.println(triangle);
}
}
class Triangle2 {
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;
/** Constructor */
public Triangle2() {
}
/** Constructor */
public Triangle2(double side1, double side2, double side3)throws IllegalTriangleException {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}