In: Computer Science
Write a class with 2 methods. The first method determines if any two sides added together are greater than the remaining side. The second method calculates the triangle’s area. // Method 1: If sum of any two sides is greater than the remaining side, return true public static boolean isValid(double sid1, double side2, double side3) // Method 2: Returns the triangle area public static double area(double side1, double side2, double side3) Develop a test program that takes in the three sides of a triangle and uses the isValid( )method to determine if the input is valid according to the above criteria. If the inputs are valid display the area. Otherwise display an error message and continue to ask for inputs until a valid set is received. The formula for computing the area of a triangle is as follows: ? = (????1 + ????2 + ????3)/2 ???? = √?(? − ????1)(? − ????2)(? − ????3) Hint: use a while loop to check the validity of the user’s input
I need the answer for Java Eclipse IDE. Thank you :))
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
Triangle.java
---
import java.util.Scanner;
public class Triangle {
// Method 1: If sum of any two sides is greater than
the remaining side, return true
public static boolean isValid(double side1, double
side2, double side3) {
if(side1 + side2 > side3
&& side2+ side3 > side1 && side1+side3 >
side2)
return
true;
else
return
false;
}
// Method 2: Returns the triangle area
public static double area(double side1, double side2,
double side3) {
double s = (side1 + side2 +
side3)/2;
double v = s * (s-side1) *
(s-side2) * (s-side3);
return Math.sqrt(v);
}
public static void main(String[] args) {
double side1, side2, side3;
boolean valid;
Scanner keybd = new
Scanner(System.in);
do {
System.out.println("Enter 3 sides of a triangle: ");
side1 =
keybd.nextDouble();
side2 =
keybd.nextDouble();
side3 =
keybd.nextDouble();
valid =
isValid(side1, side2, side3);
if(!valid)
System.out.println("The sides do not form a
triangle");
}while(!valid);
System.out.println("The area of the
triangle is " + area(side1, side2, side3));
}
}
output
---
Enter 3 sides of a triangle:
1 1 2
The sides do not form a triangle
Enter 3 sides of a triangle:
1 2 3
The sides do not form a triangle
Enter 3 sides of a triangle:
3 4 5
The area of the triangle is 6.0