triangle demo class java code The program should create an RightTriangle object
ID: 3665464 • Letter: T
Question
triangle demo class java code
The program should
create an RightTriangle object called first using the no argument constructor. It should then get values using the appropriate mutator methods. And finally it should display the base, height, hypotenuse and area by calling the appropriate method for each value. [ hint do this inside either a print, println, or printf statement(s).
create an RightTriangle object called second using the constructor (i.e. not the no argument constructor). And finally it should display the , base, height, hypotenuse and area by calling the appropriate method for each value. [ hint do this inside either a print, println, or printf statement(s).
Explanation / Answer
class RightTriangle
{
int base, height, hyp;
RightTriangle()
{
}
RightTriangle(int b, int h, int hy)
{
this.base = b;
this.height =h;
this.hypo = hy;
}
public void setBase(int b)
{
this.base = b;
}
public void setHeight(int h)
{
this.height = h;
}
public void setHypo(int hy)
{
this.hyp = hy;
}
public void getBase()
{
return base;
}
public void getHeight()
{
return height;
}
public void getHypo()
{
return hyp;
}
public float area()
{
return ((base*height)/2)
}
}
class MyClass
{
public static void main(String args[])
{
RightTriangle first = new RightTriangle();
first.setBase(10);
first.setHeight(15);
first.setHypo(20);
System.out.println("Base ="+getBase());
System.out.println("Height ="+getHeight());
System.out.println("Area ="+area());
RightTriangle second = new RightTriangle(10,11,13);
System.out.println("Base ="+getBase());
System.out.println("Height ="+getHeight());
System.out.println("Area ="+area());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.