Write a class called Triangle that represents a triangle A t

Write a class called Triangle that represents a triangle. A triangle object has three double precision instance variables for the lengths of the three sides. The constructor for Triangle objects takes three double precision values and initializes the three sides: public Triangle (double a, double b, double c) 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.) Don\'t assume that lengths are integer values. Include the following methods that return Boolean values indicating if the particular property holds. Give your methods exactly these names. public booleanisTriangle ()(the sides make up a legit triangle) public booleanisScalene ()(no two sides are the same length) public booleanisEquilateral ()(all three sides are the same length) public booleanisRightO(is a right triangle) public boolean isIsosceles ()(two or three sides are the same) Three sides make a triangle if the sum of two sides is greater than the third side. This must be true for all three combinations of sides. For the other \"is\" methods, immediately return false if the sides do not make up a legitimate triangle. Use iSTriangle () 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 if two or three sides are the same length. (Sometimes isosceles is taken to mean two sides are the same and the third is different.) Also include a method area () which calculates and returns the area of the triangle (as a double precision value). It returns -1 if the object is not a triangle. Include the toString () method that returns a string based on the object: public double area ()(use Heron\'s formula from program 3) public String toString() You don\'t need to use Number Format with the toString () method (although you can do this if you want.) Use BlueJ to develop and test your class. For this project, there will be no main method. There will be no System.out .print () statements at all since all user interaction will be through BlueJ. After your source file compiles, you will be able (in BlueJ) to click on the rectangle that represents the class to instantiate it. Now a red rectangle should appear that represents an object. Clicking and selecting methods of the object should now work. Instantiate several Triangles with various sides and verify that your class works correctly.

Solution

As per the given guidelines, please find the Class triangle with the three double precision variable and constructor for the initialization. All the required functions have been added as per their required functionality. For the area function, have used the heron\'s formula s(s-a)(s-b)(s-c). Kindly please import the required data.

public class Triangle {
   private double a;
   private double b;
   private double c;

   public Triangle(double a, double b, double c) {
       this.a = a;
       this.b = b;
       this.c = c;
   }

   public boolean isTriangle() {
       if (((a + b) > c) && ((b + c) > a) && ((c + a) > b))
           return true;
       return false;
   }

   public boolean isScalene() {
       if ((a != b) && (a != c) && (b != c)) {
           return true;
       }
       return false;
   }

   public boolean isEquilateral() {
       if ((a == b) && (b == c)) {
           return true;
       }
       return false;
   }

   public boolean isRight() {
       if (Math.pow(a, 2) == (Math.pow(b, 2) + Math.pow(c, 2))) {
           return true;
       } else if (Math.pow(b, 2) == (Math.pow(a, 2) + Math.pow(c, 2))) {
           return true;
       } else if (Math.pow(c, 2) == (Math.pow(a, 2) + Math.pow(b, 2))) {
           return true;
       }
       return false;
   }

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

   public double area() {
       double s = (a + b + c) / 2;
       return Math.sqrt(s * (s - a) * (s - b) * (s - c));
   }

   @Override
   public String toString() {
       return \"Triangle [a=\" + a + \", b=\" + b + \", c=\" + c + \", isTriangle()=\"
               + isTriangle() + \", isScalene()=\" + isScalene()
               + \", isEquilateral()=\" + isEquilateral() + \", isRight()=\"
               + isRight() + \", isIsosceles()=\" + isIsosceles() + \", area()=\"
               + area() + \"]\";
   }

}

 Write a class called Triangle that represents a triangle. A triangle object has three double precision instance variables for the lengths of the three sides. T
 Write a class called Triangle that represents a triangle. A triangle object has three double precision instance variables for the lengths of the three sides. T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site