Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called Triangle that represents a triangle It has three double pre

ID: 3686372 • Letter: W

Question

Write a class called Triangle that represents a triangle It has three double precision instance variables representing the three sides Write a constructor for Triangle objects that takes three double precision values The lengths of sides a. b. and c are double precision and expected to be greater than zero. However, the user might supply bad data Accept whatever values the user provides The lengths are not in any particular order (don't assume that a is the smallest or largest.) Include the following methods that return Boolean values indicating if the particular property holds. Give your methods exactly these names public Three sides make a triangle if the sum of two sales a greater than the third sale This must be true for al three combinations of sides For the other "is" methods, immediately return false 4 the sides do not make up a legitimate triangle Use Is Triangle () to test this Otherwise, continue testing the relevant property Use the Pythagorean Theorem to determine if a triangle is a right triangle A triangle is isosceles 4 two or three sales are the same length (Sometimes isosceles is taken to mean two sides the same and the third different) Also include a method area() which calculates and returns the area of the triangle (as a double precision value) Return -1 if the object a not a triangle Include a to string() method that returns a String based on the object public double areal) (use Herons formula from program 3) public String to String () You don't need to use NunterFornar with the toStr mg () method (although you can do this if you want) Use Blue J to develop and For this project there be no method There will be no System.out.; tint statements since al user interaction wd be through BlueJ After your source fUe corrpdes. you wfl be able (m BlueJ) to click on the rectangle that represents the class to instantiate 4. Now a red rectangle should appear that represents an object Clidung and selecting methods of the object should now work Instantiate several Triangles with various sides and verify that your class works correctly Start your source fie with comments that describe it:

Explanation / Answer

Here is the code

import java.util.*;

public class Triangle
{
double a,b,c;
  
public Triangle(double a, double b, double c)
{
this.a=a;
this.b=b;
this.c=c;
}
  
public boolean isTriangle()
{
// check if sum of two sides is greater than third for all combinations
if ((a<(b+c))&&(b<(c+a))&&(c<(a+b)))
return true;
else return false;
}
  
public boolean isScalene()
{
if(isTriangle())
{
if(a!=b && a!=c && b!=c)
return true;
else return false;
}
else return false;
}
  
public boolean isEquilateral()
{
if(isTriangle())
{
if(a==b && a==c)
return true;
else return false;
}
else return false;
}
public boolean isRight()
{
if(isTriangle())
{

if((a*a)==(b*b)+(c*c) || (b*b)==(a*a)+(c*c) || (c*c)==(b*b)+(a*a))
return true;
else return false;
}
else return false;
}
  
public boolean isIsosceles()
{
if(isTriangle())
{

if((a==b && a!=c) || (a==c && a!=b) || (b==c && c!=a) )
return true;
else return false;
}
else return false;
}
  
public double area()
{
if(isTriangle())
{
double s,area;
s = (a+b+c)/2; // obtain value of s
area = (float)Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
else return 0;
}
  
public String toString()
{
if(isTriangle())
{
return "Triangle Sides: "+a+", "+b+", "+c+" Triangle Area: "+area();
}
else return "Invalid Triangle";
}   

// Test the program. Later on you can remove this main method and test in bluej

public static void main(String a[])
{
Triangle t= new Triangle(5,4,3);

System.out.println(t);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote