//java Create a class that contains the following two methods: // Return true if
ID: 3759822 • Letter: #
Question
//java
Create a class that contains the following two methods: // Return true if the sum of any two sides is greater than the third side. Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid. The formula for computing the area of a triangle is as follows: Hint: use a while loop to check the validity of the user's input. Most questions are based on / taken from the exercises and examples listed in the textbook used by the course.Explanation / Answer
import java.util.Scanner;
public class Triangle {
public static boolean isValid(double side1, double side2, double side3) {
return (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1);
}
public static double area(double side1, double side2, double side3) {
double p = ( side1 + side2 + side3)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a,b,c;
while (true) {
System.out.print("Enter three sides in double: ");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
if (!isValid(a, b, c)) {
System.out.println("Invalid Input. Try Again.");
} else {
break;
}
}
System.out.println("Area: "+area(a, b, c));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.