IllegalTriangleException Exercise 11.1 defined the Triangle class with three sid
ID: 3632785 • Letter: I
Question
IllegalTriangleException Exercise 11.1 defined the Triangle class with three sides. in a triangle,the sum of any two sides is greater the the other side. the Triangle rule must adhere to this rule. 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 public class Triangle (double side1, double side2, double side3 ) throws IllegalTriangleException { // implement it}Explanation / Answer
/* Illegal Triangle Exception*/
/* program to read 3 sides of a triangle */
/* through exception if the sum of 2 sides is not >= the third side*/
// create class triangle
public class Triangle {
//set some of the variables to be used in the class
private double sideA;
private double sideB;
private double sideC;
//we use a throw statement to check if the user input can create a valid triangle or if the input is invalid
// create constructor
public Triangle(double sideA, double sideB, double sideC)
// throw exception
throws IllegalTriangleException {
if (sideA + sideB >= sideC && sideB + sideC >= sideA && sideA + sideC >= sideB && sideA > 0 && sideB > 0 && sideC > 0){
success(); }
else{
throw new IllegalTriangleException(sideA, sideB, sideC);
}
}// end constructor
//we create a small method to let the user know they have a valid triangle
// create method success
public void success(){
JOptionPane.showMessageDialog(null, "Success, you have a valid triangle.");
}
//we create a few accesors and mutators for the sides
// create method setsideA
public void setSideA(double sideA) {
this.sideA = sideA;
}
// create method getSideA
public double getSideA() {
return sideA;
}
// create method setSideB
public void setSideB(double sideB) {
this.sideB = sideB;
}
// create method getSideB
public double getSideB() {
return sideB;
}
// create method setSideC
public void setSideC(double sideC) {
this.sideC = sideC;
}
// create method getSideC
public double getSideC() {
return sideC;
}
}// end class
----------------------------------------------------------------
// needed JoptionPnae
import javax.swing.JOptionPane;
// create class
public class TriangleTest {
/**
* The program ask the user for three inputs/sides of a triangle,
* the program passes this value into the classes and subclasses
* to check and see if the input entered are a valid triangle
* @param args
*/
// main method
public static void main(String[] args){
//we ask the user for the triangle sides.
String sideAString = JOptionPane.showInputDialog(null,"Enter the lenght for side A: ");
// read sideA
double sideA = Double.parseDouble(sideAString);
String sideBString = JOptionPane.showInputDialog(null,"Enter the lenght for side B: ");
// Read sideB
double sideB = Double.parseDouble(sideBString);
String sideCString = JOptionPane.showInputDialog(null,"Enter the lenght for side C: ");
// read sideC
double sideC = Double.parseDouble(sideCString);
//we do a try-catch statements to check and see if the input is valid, if the input is not valid the catch part takes care of it
try{
new Triangle(sideA, sideB, sideC);
}
catch(IllegalTriangleException ex){
JOptionPane.showMessageDialog(null, ex);
}
}
}// end class
--------------------------------------
/* create class IllegalTriangleException
public class IllegalTriangleException extends Exception {
//we set the variables to be used in the subclass
protected double sideA;
protected double sideB;
protected double sideC;
//i'm not quite sure what this is doing, i know is a constructor and we are passing the sides from the triangle class
//but not quite sure why but I know that without it we get an error in the throw statement.
public IllegalTriangleException(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
//we override the toString to display the user that he had invalid numbers
public String toString(){
return "You have entered invalid numbers";
}
}// end class