I have an assignment to take my Triangle class program and add an execption to i
ID: 670987 • Letter: I
Question
I have an assignment to take my Triangle class program and add an execption to it to make it tell the user if its valid or not. My question is how is the best way to combine these programs to give the best result? I have been working on this one for a while with no avail...
Here is the assignment statement:
"Modify the Triangle class to throw an exception
in the constructor and set routines if the triangle is not valid (i.e., does not
satisfy the triangle inequality). Modify the main routine to prompt the user
for the sides of the triangle and to catch the exception and re-prompt the
user for valid triangle sides. In addition, for any valid triangle supply a
method to compute and display the area of the triangle using Herod’s
method: where a, b, and c are the lengths of the sides and p is 1/2 of the
perimeter of the triangle, A = [p(p-a)(p-b)(p-c)]1/2. The area, A, should be a
double displayed with 2 digits after the decimal point. Supply a getPerimeter
method to compute the perimeter and an integer. Display the triangle, its
area and perimeter. Allow the user to repeatedly enter triangle sides and do
not exit the process until the user enters 0 for a side length."
My Triangle Program:
package triangle;
public class Triangle {
private int sideA;
private int sideB;
private int sideC;
public static void main(String[] args) {
System.out.println("Enter integers below for type of triangle");
}
public Triangle(int sideA, int sideB, int sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public int getSideA() {
return sideA;
}
public void setSideA(int sideA) {
this.sideA = sideA;
}
public int getSideB() {
return sideB;
}
public void setSideB(int sideB) {
this.sideB = sideB;
}
public int getSideC() {
return sideC;
}
public void setSideC(int sideC) {
this.sideC = sideC;
}
public boolean isScalene() {
//Check for valid
if(isValid()) {
return sideA!=sideB && sideB!=sideC && sideC!=sideA;
} else {
return false;
}
}
public boolean isRight() {
//Check for valid
if(isValid()) {
if(sideA*sideA==Math.sqrt(sideB*sideB+sideC*sideC)
|| sideB*sideB==Math.sqrt(sideA*sideA+sideC*sideC)
|| sideC*sideC==Math.sqrt(sideA*sideA+sideB*sideB)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public boolean isIsosceles() {
//Check for valid
if(isValid()) {
if(sideA==sideB || sideB==sideC || sideC==sideA) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public boolean isEquilateral() {
//Check for valid
if(isValid()) {
return sideA==sideB && sideB==sideC && sideC==sideA;
} else {
return false;
}
}
public boolean isValid() {
return sideA+sideB > sideC || sideA+sideC > sideB || sideB+sideC > sideA;
}
}
Explanation / Answer
public class IllegalTriangleException extends Exception { private double area; /** Construct an exception*/ public IllegalTriangleException(double area) { super( "The sum of any two sides is greater than the other side" + area); this.area = area; } /**return the side*/ public double getArea() { return area; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.