Create a Java program that asks the user to input the lengths of three sides of
ID: 3561747 • Letter: C
Question
Create a Java program that asks the user to input the lengths of three sides of a triangle. After receiving the three values, your program should provide answers for each of the following questions:
1. Is it really a triangle? If one side is long than the sum of the other two sides, then it isnot.
2. Is it a right triangle? That is, does it satisfy the Pythagorean theorem?
3. Is it an equilateral triangle? That is, are all three sides the same?
4. Is it an isosceles triangle? That is , are two sides the same? (If your program has already determined that the triangle is equilateral, then it hsould NOT state that it is isosceles.)
5. What is the area of the triangle?
6. What is the perimeter of the triangle?
The program must use a pop-up window (JOptionPane) to get the lengths of the sides from the user, and must show all of its results in a message window (also JOptionPane).
Things to remember:
example of finished product:
Welcome to the Triangle Program
Please enter the length of side 1: 2.0
Please enter the length of side 2: 2.0
Please enter the length of side 3: 2.0
My analysis of this triangle is
This is a triangle.
This is NOT a right triangle.
This is an equilateral triangle.
The area of the triangle is 1.732.
The perimeter of the triangle is 6.0.
This is my first homework in a Java program ever and we have not used many advanced "code words" so if you could stress using the basics it would be greatly appreciated. Thanks!
Explanation / Answer
/**
*
*
*/
import javax.swing.JOptionPane;
class triangle
{
public static void main(String []ar)
{
double a=JOptionPane.ShowInputDialog("please enter the length os side 1 :");
double b=JOptionPane.ShowInputDialog("please enter the length os side 2 :");
double c=JOptionPane.ShowInputDialog("please enter the length os side 3 :");
String s1,s2,s3,s4;
if((a+b>c)&&(b+c>a)&&(c+a>b))
s1="This is a triangle";
else
s1="This is not a triangle";
if((((a*a)+(b*b))==(c*c))||(((b*b)+(c*c))==(a*a))||(((c*c)+(a*a))==(b*b)))
s2="This is a right triangle";
else
s2="This is not a right triangle";
if((a==b)&&(b==c))
s3="This is an equilateral triangle";
else
{
s3="This is not an equilateral triangle";
if((a==b)||(b==c)||(c==a))
s4="This is an isosceles triangle";
else
s4="This is not an isosceles triangle";
}
double s=(a+b+c)/2;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
double perimeter=2*s;
JOptionPane.ShowMessageDialog(null,"Welcome to the triangle Program"+s1+s2+s3+s4+"the area of
triagnle is "+area+"The perimeter of the triangle is "+perimeter);
}
}