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

confused with the question, can anyone help answer these question?java language)

ID: 3580167 • Letter: C

Question

confused with the question, can anyone help answer these question?java language)

Write a class named Triangle with following methods and constructors: A default constructor that sets all sides to be of length 1, a specific constructor that takes three parameters (representing the length of sides of the triangle), a method named perimeter that returns the perimeter of the Triangle, a method named type that returns a String describing whether the the triangle is isosceles (two equal sides), equilateral(three equal sides) or scalene (all sides are different).

Explanation / Answer

class Triangle
{
int a,b,c;
  
// default constructor
Triangle()
{
a=1;
b=1;
c=1;
}
  
// 3 parameter constructor
Triangle(int x, int y, int z)
{
a = x;
b = y;
c = z;
}
  
// calculates and returns perimeter
int perimeter()
{
return a+b+c;
}
  
// returns type of triangle
String type()
{
if(a==b && b==c)
return "Triangle is equilateral";
else if (a==b || b==c || a==c)
return "Triangle is isosceles";
else
return "Triangle is scalene";
}
  

   public static void main (String[] args) throws java.lang.Exception
   {
   // test1
       Triangle cc = new Triangle();
       System.out.printf("a=%d b=%d c=%d ",cc.a,cc.b,cc.c);
       System.out.println(cc.perimeter());
       System.out.println(cc.type());
      
       //test2
       Triangle aa = new Triangle(1,1,3);
       System.out.printf("a=%d b=%d c=%d ",aa.a,aa.b,aa.c);
       System.out.println(aa.perimeter());
       System.out.println(aa.type());
   }
}

/* SAMPLE OUTPUT

a=1 b=1 c=1
3
Triangle is equilateral

a=1 b=1 c=3
5
Triangle is isosceles

*/