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

implement a Triangle class. It will contain methods that will: compute the area

ID: 3582124 • Letter: I

Question

implement a Triangle class. It will contain methods that will:
compute the area of the triangle
compute the perimeter of the triangle
display the triangle’s characteristics

Note: The area and the perimeter can be computed based on a formula that uses the three sites (a,b,c) of the triangle. Use Heron’s formula for the area.  

1. Declare your instance variables. Hint: from other classes they should be accessed by getters.

2. Create getters and setters for the sites. The setter should check for parameter validity. The site must take a positive value.

3. Create a default constructor that will initialize all the sites to 1.
Create a constructor that will take 3 parameters corresponding to site a, site b, site c.
Make sure that numberTriangles will be updated each time a new triangle is created.

4. Write the methods A method that takes the three sides of the triangle and computes the area. ( getArea())
A method that takes the three sides of the triangle and computes the perimeter (getPerimeter( ))
A method that will display: name, sites, area and perimeter of a given triangles (triangleInfo())

Put all the methods in the class Triangle.java

Now complete the following tester program named TriangleTester.java so that it exercises all of the methods of your class.

Test Triangle class:

Create a triangle with default values for sites named t1.
Create a triangle with the sites values: 3, 4, 5 named t2.
Compute the area of triangle t1 (by using getArea( ) method).
Compute area of triangle t2.
Compute the perimeter of the t1.
Compute the perimeter of t2.
Display the number of created triangles.
Display (using triangleInfo) the name, the area and the perimeter of triangle t1.

Explanation / Answer

public class Triangle {
   private static int numberOfTriangles = 0;
   private float a,b,c;

   Triangle() {
       a=1;
       b=1;
       c=1;
       ++numberOfTriangles;
   }

   Triangle(float x, float y, float z) {
       if (checkIsNotTriangle(x, y, z)) {
           throw new IllegalStateException("Invalid value of sides, please check!");
       }
       a=x;
       b=y;
       c=z;
       ++numberOfTriangles;
   }

   private boolean checkIsNotTriangle(float x, float y, float z) {
       if ((x<=0) || (y<=0) || (z<=0))
           return true;
       if ((x+y<=z) || (y+z<=x) || (z+x<=y))
           return true;
       return false;
   }

   public float getArea() {
       float s = getPerimeter()/2;
       float answer = s * (s-a) * (s-b) * (s-c);
       return (float) Math.sqrt(answer);
   }

   public float getPerimeter() {
       return (a+b+c);
   }

   public void triangleInfo() {
       System.out.println("Sides are a=" + a + " b=" + b + " c=" + c);
       System.out.println("Area is " + getArea());
       System.out.println("Perimeter is " + getPerimeter());
   }

   public float getSideA() {return a;}
   public float getSideB() {return b;}
   public float getSideC() {return c;}

   public void setSideA(float x) {
       if (checkIsNotTriangle(x, b, c)) {
           throw new IllegalStateException("Invalid value of sides, please check!");
       }
       a = x;
   }

   public void setSideB(float y) {
       if (checkIsNotTriangle(a, y, c)) {
           throw new IllegalStateException("Invalid value of sides, please check!");
       }
       b = y;
   }

   public void setSideC(float z) {
       if (checkIsNotTriangle(a, b, z)) {
           throw new IllegalStateException("Invalid value of sides, please check!");
       }
       c = z;
   }

   public int getNumberOfTriangles() {return numberOfTriangles;}
}

public class TriangleTester {
   public static void main(String[] args) {
       Triangle t1 = new Triangle();
       Triangle t2 = new Triangle(3,4,5);

       t1.getArea();
       t2.getArea();
       t1.getPerimeter();
       t2.getPerimeter();

       System.out.println("Number of created triangles is " + t1.getNumberOfTriangles());
       System.out.println("For triangle t1");
       t1.triangleInfo();
   }
}

Dr Jack
Hire Me For All Your Tutoring Needs
Quick quotes • Clear explanations • Study support
Chat Now And Get Quote