In Java, write a main method called triangleType. The triangleType method is sen
ID: 3554273 • Letter: I
Question
In Java, write a main method called triangleType. The triangleType method is sent 3 integers A, B, and C which are assumed to represent the sides of a triangle. The method must put these integers in ascending order in order to determine the type of triangle. The method must return a string indicating if the triangle is EQUILATERAL. ISOSCELES, SCALENE, or INVALID TRIANGLE.
If A, B, and C represent the sides in ascending order, the rules for determining the triangle type are as follows: if A + B <= C, then the sides do not represent a valid triangle. If A=C then the triangle is EQUILATERAL. If A=B or B=C, then the triangle is ISOSCELES; otherwise the triangle is SCALENE.
After the method is defined, the main method must repeatedly have the user enter 3 integers, call the triangleType method and display the return type. Be sure not to have an infinite loop by allowing the user to quit.
Explanation / Answer
please rate -thanks
import java.util.Scanner;
public class main
{public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int a,b,c;
System.out.print("Enter the 1st side of a triangle(0 when done): ");
a=in.nextInt();
while(a!=0)
{System.out.print("Enter the 2nd side of the triangle: ");
b=in.nextInt();
System.out.print("Enter the 3rd side of the triangle: ");
c=in.nextInt();
System.out.println("The triangle is "+triangleType(a,b,c));
System.out.print(" Enter the 1st side of a triangle(0 when done): ");
a=in.nextInt();
}
}
public static String triangleType(int num1,int num2,int num3)
{int a,b,c;
if(num1<num2)
{if(num1<num3)
{a=num1; //num1 is smallest
if(num2<num3)
{b=num2;
c=num3;
}
else
{b=num3;
c=num2;
}
}
else //num3 is smallest
{a=num3;
b=num1;
c=num2;
}
}
else //num2 or num3 smallest
if(num2<num3)
{a=num2; //num2 smallest
if(num1<num3)
{b=num1;
c=num3;
}
else
{b=num3;
c=num1;
}
}
else
{a=num3;
b=num2;
c=num1;
}
if(a+b<=c)
return "INVALID TRIANGLE";
if(a==c)
return "EQUILATERAL";
if(a==b||b==c)
return "ISOSCELES";
return "SCALENE";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.