Create a class named MyTriangle that contains the following three methods: publi
ID: 3859169 • Letter: C
Question
Create a class named MyTriangle that contains the following three methods:
public static boolean isValid(double sidea, double sideb, double sidec)
public static double area(double sidea, double sideb, double sidec)
public static String triangletType(double a, double b, double c)
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.
Explanation / Answer
MyTriangle.java
public class MyTriangle {
// Declaring instance variables
private double side1;
private double side2;
private double side3;
// Zero argumented constructor
public MyTriangle() {
super();
}
/*
* This method is used to calculate the area of the triangle using Herons
* formula
*/
public static double area(double side1, double side2, double side3) {
double p = (side1 + side2 + side3) / 2;
double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
/*
* This method is used to check whether the three sides are valid sides of
* the triangle
*/
public static boolean isValid(double side1, double side2, double side3) {
if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
return true;
else
return false;
}
//This method will displays the Type of the Triangle
public static String triangletType(double a, double b, double c) {
// Declaring variable
double temp;
/*
* Checking whether first number is greater than second number or not if
* yes, Swapping will happen
*/
if (a > b) {
temp = a;
a = b;
b = temp;
}
/*
* Checking whether second number is greater than third number or not if
* yes, Swapping will happen
*/
if (b > c) {
temp = b;
b = c;
c = temp;
}
/*
* Checking whether first number is greater than second number or not if
* yes, Swapping will happen
*/
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (isValid(a, b, c)) {
if (a == c)
return "Equilateral";
else if (a == b || b == c)
return "Isosceles";
else
return "Scalene";
} else
return "Invalid Triangle";
}
}
____________________________
package org.students;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// declaring variables
double side1, side2, side3;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the 3s ides of the triangle
System.out.print("Enter Length of side 1:");
side1 = sc.nextDouble();
System.out.print("Enter Length of side 2:");
side2 = sc.nextDouble();
System.out.print("Enter Length of side 3:");
side3 = sc.nextDouble();
// Checking whether the three sides are valid or not
boolean bool = MyTriangle.isValid(side1, side2, side3);
/*
* if valid find area of triangle If not,display Error message
*/
if (bool) {
System.out.printf("Area of Triangle :%.2f ", MyTriangle.area(side1, side2, side3));
System.out.println("Triangle Type is :" + MyTriangle.triangletType(side1, side2, side3));
} else
System.out.println("** Invalid Triangle **");
}
}
___________________________
Output:
Enter Length of side 1:2
Enter Length of side 2:2
Enter Length of side 3:3
Area of Triangle :1.98
Triangle Type is :Isosceles
_________________
Output#2:
Enter Length of side 1:5
Enter Length of side 2:3
Enter Length of side 3:3
Area of Triangle :4.15
Triangle Type is :Isosceles
_________________
Output#3:
Enter Length of side 1:3
Enter Length of side 2:4
Enter Length of side 3:8
** Invalid Triangle **
____________________
Output#4:
Enter Length of side 1:6
Enter Length of side 2:6
Enter Length of side 3:6
Area of Triangle :15.59
Triangle Type is :Equilateral
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.