define a class called “Triangle” which has two private properties: base (float),
ID: 3794140 • Letter: D
Question
define a class called “Triangle” which has two private properties: base (float), and height (float). Define proper functions (including constructors, get/set etc., destructor) to utilize the properties. define a class called “Area_Calculator” that will calculate the area of an object. We assume that the Area_Calculator class will inherit from “Triangle” class, and should be able to print the area of a triangle object. (Clue: area of a triangle = .5 * base * height In main function, create two Triangle class, and print their area using the help of an Area_Calculator object
Explanation / Answer
class Triangle
{
private float base;
private float height;
float area;
public Triangle()
{
System.out.println("Constructor");
}
public float getBase()
{
return base;
}
public float getHeight()
{
return height;
}
public void setBase( float newBase)
{
base = newBase;
}
public void setHeight(float newHeight)
{
height = newHeight;
}
}
public class Area_Calculator
{
public static void main(String args[])
{
Triangle obj = new Triangle();
obj.setBase(5);
obj.setHeight(10);
float area = 0.5f * obj.getBase() * obj.getHeight();
System.out.print("The Area of Triangle is:"+area);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.