6.19 The NyTriangle class) Create a class named NyTriangle that contains the fol
ID: 3726395 • Letter: 6
Question
6.19 The NyTriangle class) Create a class named NyTriangle that contains the following two methods: Return true 1f the sum of any two sides s greater than the third side. public static boolean isVal1d double sidel, double side2, double side3) Return the area of the triangle. */ public static double area( double sidel, double side2, double side3) Write a test program that reads three sides for a triangle and compates the area if the inpat is valid. Otherwise, it displays that the input is invalid. The formala for computing the area of a triangle is given in Programming Exercise 2.19Explanation / Answer
import java.util.Scanner;
public class MyTriangle {
public static boolean isValid(double a, double b, double c) {
if (a+b > c && a+c > b && b+c > a)
return true;
else
return false;
}
public static double area(double s1, double s2, double s3) {
double s = (s1 + s2 + s3 )/ 2 ;
return Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
}
public static void main(String[] args) {
double s1, s2, s3;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first side :");
s1 = s.nextDouble();
System.out.print("Enter the second side :");
s2 = s.nextDouble();
System.out.print("Enter the third side :");
s3 = s.nextDouble();
if(isValid(s1, s2, s3)) {
System.out.println("Area:" + area(s1, s2, s3));
}else{
System.out.println("Do not form a valid triangle");
}
s.close();
}
}
/*
Sample run:
Enter the first side :3
Enter the second side :4
Enter the third side :5
Area:6.0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.