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

Question 6. 6. (TCO 2) Define and implement the overloaded constructors that sup

ID: 3558814 • Letter: Q

Question

Question 6. 6. (TCO 2) Define and implement the overloaded constructors that support the following test function for a Triangle class. The data members for the Triangle class are triangleBase and height. Both are integer data types. public static void main(String args[]) { //t1 will take all default value Triangle t1 = Triangle(); //t2 will take all supplied values Triangle t2 = Triangle(3, 10); //t3 will take supplied triangleBase, height will take default value Triangle t3 = Triangle(5); //t4 will take the same value of t2 Triangle t4 = t2; //the rest of the code }

Explanation / Answer

//Define and implement the overloaded constructors that support the following test function for a Triangle class.
//The data members for the Triangle class are triangleBase and height. Both are integer data types.

class Triangle
{
private int triangleBase;
private int height;
public Triangle()
{
triangleBase = 1;
height = 2;
}
public Triangle(int base)
{
triangleBase = base;
height = 2;
}
public Triangle(int base,int h)
{
triangleBase = base;
height = h;
}


int getTriangleBase() { return triangleBase; }
int getHeight() { return height; }

}

public class Demo
{
public static void main(String args[])
{
//t1 will take all default value
Triangle t1 = new Triangle();
//t2 will take all supplied values
Triangle t2 = new Triangle(3, 10);
//t3 will take supplied triangleBase, height will take default value
Triangle t3 = new Triangle(5);
//t4 will take the same value of t2
Triangle t4 = t2;
System.out.println(t4.getTriangleBase());
//the rest of the code
}
}

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