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.
import java.util.Scanner;
public class MyTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sideA, sideB, sideC, areaOfTriangle = 0.0;
boolean trianlgeValid = false;
// Asking for sides of triangle as INPUT from user
sideA = scanner.nextDouble();
sideB = scanner.nextDouble();
sideC = scanner.nextDouble();
// checking if the triangle is VALID or INVALID
trianlgeValid = isValid(sideA, sideB, sideC);
if (trianlgeValid == true) {
System.out.println("Valid triangle");
// If it is valid then calculate the area and type
areaOfTriangle = area(sideA, sideB, sideC);
System.out.println("Area : " + areaOfTriangle);
// Here we are directly printing the returned value from the method
System.out.println("Type : " + triangletType(sideA, sideB, sideC));
} else {
// If it is not valid then print this
System.out.println("InValid Triangle");
}
}
public static boolean isValid(double sidea, double sideb, double sidec) {
// Initializing an array for storing the sides of triangle to sort them
// ascending
// wise
double arr[] = new double[3];
boolean triangleValid = false;
// Copying the values of sides to array
arr[0] = sidea;
arr[1] = sideb;
arr[2] = sidec;
// Sorting the sides using SELECTION SORT
for (int i = 1; i < 3; i++) {
double temp = arr[i];
int j = i - 1;
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
// Then again copy the values of array to sides
sidea = arr[0];
sideb = arr[1];
sidec = arr[2];
// Calculating if the triangle is valid or not
if ((sidea + sideb) > sidec) {
// If it is then
triangleValid = true;
} else {
// If it is not then
triangleValid = false;
}
return triangleValid;
}
public static double area(double sidea, double sideb, double sidec) {
double areaOfTriangle = 0.0, s = 0.0;
// Here we are calculating the s value according to the HERON'S FORMULA
s = (sidea + sideb + sidec) / 2.0;
// And the finding the area using same HERON'S FORMULA
// We are using (Math.sqrt) to find square root the calculated value
areaOfTriangle = Math.sqrt((s * (s - sidea) * (s - sideb) * (s - sidec)));
return areaOfTriangle;
}
public static String triangletType(double a, double b, double c) {
String type = "";
double arr[] = new double[3];
// Using same technique as used above to sort the sides for calculating their
// types
arr[0] = a;
arr[1] = b;
arr[2] = c;
for (int i = 1; i < 3; i++) {
double temp = arr[i];
int j = i - 1;
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
a = arr[0];
b = arr[1];
c = arr[2];
// calculating the type
if (a == c) {
type = "Equilateral";
} else if (a == b || b == c) {
type = "Isosceles";
} else {
type = "Scalene";
}
// returning the calculated type
return type;
}
}
Get Answers For Free
Most questions answered within 1 hours.