Create a class named MyTriangle that contains the following three methods:
The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent in any particular order.
The area method returns the area of the triangle. Given the lengths of the three sides of the triangle, the area of the triangle can be computed using Heron's formula (do the research).
The triangleType method returns one of the following strings: "Equilateral", "Isosceles", "Scalene", "Invalid Triangle". You can determine the triangle's type if a, b, and c represent the sides in ascending order, you must first determine if the triangle is valid. If it is valid, the triangle is equilateral if a == c. Isosceles if a == b or b == c. Scalene otherwise.
Define a main method within your class that inputs from the user three sides for a triangle in any order. The main method should then display the triangle's type. If the triangle is valid, you must also output the area of the triangle. Zip your complete netbeans project and submit it by the due date.
Please make easy code! java code!
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the program
Source code
//Importing Scanner class to accept user input import java.util.Scanner; public class MyTriangle { //isValid() method to check triangle is valid or not public static boolean isValid(double sidea,double sideb,double sidec) { double a,b,c,temp; //assigning a,b,c with sidea,sideb and sidec a=sidea; b=sideb; c=sidec; //if a contain larger value than b,interchange both and make a contain smaller value if(a>b) { temp=a; a=b; b=temp; } //if a contain larger value than c,interchange both and make a contain smaller value if(a>c) { temp=a; a=c; c=temp; } //if b contain larger value than c,interchange both and make b contain smaller value if(b>c) { temp=b; b=c; c=temp; } //Now a,b,c hold values in ascending order //check sum of two smaller side is greater than third side if((a+b)>c) //if yes, valid triangle and return true return true; else //if no, invalid triangle return false return false; } //method to calculate area using Heron's formula public static double area(double a,double b,double c) { double s,area; //finding s s=(a+b+c)/2; //finding area area=Math.sqrt(s*(s-a)*(s-b)*(s-c)); return area; } public static String triangleType(double a,double b,double c) { double temp; //if a contain larger value than b,interchange both and make a contain smaller value if(a>b) { temp=a; a=b; b=temp; } //if a contain larger value than c,interchange both and make a contain smaller value if(a>c) { temp=a; a=c; c=temp; } //if b contain larger value than c,interchange both and make b contain smaller value if(b>c) { temp=b; b=c; c=temp; } //Now a,b,c hold values in ascending order //check sum of two smaller side is less than or equals third side if((a+b)<=c) //if yes triangle is invalid return "Triangle is invalid"; //otherwise check if a and c are equal(imply b also equals to both) else if (a==c) //equilateral return "Equilateral"; //otherwise check if a and b are equal or b and c are equal else if (a==b || b==c) //isosceles return "Isosceles"; //if none of the condition is true else //Scalene return "Scalene"; } public static void main(String[] args) { //variables to hold sides of triangle double a,b,c; //Creating a scanner object to read user input Scanner sc=new Scanner(System.in); System.out.print("Enter three sides of triangle: "); //reading sides a=sc.nextDouble(); b=sc.nextDouble(); c=sc.nextDouble(); //printing triangle's type System.out.println("Triangle type: "+triangleType(a,b,c)); //printing area if triangle is valid if(isValid(a,b,c)) System.out.println("Area of the triangle is: "+area(a,b,c)); } }
Screen shot of the code
Screen shot of the output
Get Answers For Free
Most questions answered within 1 hours.