Write the method called printTriangleType referred to in Sel
Write the method called printTriangleType referred to in Self-Check Problem 22. This method accepts three integer arguments representing the lengths of the sides of a triangle and prints the type of triangle that these sides form. Here are some sample calls to printTriangleType:
The output produced by these calls should be
Your method should throw an IllegalArgumentException if passed invalid values, such as ones where one side’s length is longer than the sum of the other two, which is impossible in a triangle. For example, the call of printTriangleType(2, 18, 2); should throw an exception.
Solution
Hi, Please find my code.Pleas elet me know in case of any issue.
public class PrintTriangle {
public static void printTriangleType(int s1, int s2, int s3){
// checking for invalid triangle side
if((s1 > s2+s3) || (s2 > s1+s3) || (s3 > s2+s1)){
throw new IllegalArgumentException(\"Triangle is not possible\");
}
// checking for right angle triangle
if((s1 == Math.sqrt(s2*s2 + s3*s3)) || (s2 == Math.sqrt(s1*s1 + s3*s3)) || (s3 == Math.sqrt(s2*s2 + s1*s1))){
System.out.print(\"rightangle ,\");
}
// checking for equilateral triangle
if((s1 == s2) && (s2 ==s3)){
System.out.println(\"equilateral\");
}
else if((s1==s2) || (s2==s3) || (s1==s3)){
System.out.println(\"isosceles\");
}
else{
System.out.println(\"scalene\");
}
}
public static void main(String[] args) {
printTriangleType(5, 7, 7);
printTriangleType(6, 6, 6);
printTriangleType(5, 7, 8);
try{
printTriangleType(2, 18, 2);
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}
/*
Sample run:
isosceles
equilateral
scalene
Triangle is not possible
*/

