Write an inheritance hierarchy for classes Quadrilateral,Trapezoid, parallelogra
ID: 3610469 • Letter: W
Question
Write an inheritance hierarchy for classes Quadrilateral,Trapezoid, parallelogram, Rectangel and Square. UseQuadrilateral as the base class of the hierarchy. Make thehieraracy as deep (i.e. as many levels) as possible. specifythe instance variables, properties and methods for eachclass. The Private instance variables of Quadrilateral shouldbe the x-y coordinate pairs for the four endpoints of theQuadrilateral. Write an application that instantiates objectsof your classes and outputs each object's area (exceptQuadrilateral).Explanation / Answer
JAVA program:
class Quadrilateral // Base class declaration
{
public:
int height, breadth;
quadrilateral( int x, int y)
{
height = x;
breadth = y;
}
}
class Trapezoid extends Quadrilateral // inherited classtrapezoid
{
int side;
trapezoid(int x, int y, int a)
{
super( x,y);
side = a;
}
float tr_area()
{
return ( 1/2 * height * (side + breadth) );
}
}
class Parallelogram extends Quadrilateral // inherited classparallelogram
{
parallelogram( int x, int y)
{
super( x,y )
}
float pr_area()
{
return ( height * breadth );
}
}
class Rectangle extends Quadrilateral // inherited classquadrilateral
{
rectangle( int x, int y)
{
super( x,y )
}
float rec_area()
{
return ( height * breadth );
}
}
class Square extends Quadrilateral // inherited class square
{
int side;
square(int a)
{
side = a;
}
float sqr_area()
{
return ( side * side );
}
}
class InherTest
{
public static void main (String args[])
{
Trapezoid t1 = new Trapezoid ( 14, 12, 10); // objectdeclaration
float tr_area1 = t1.tr_area();
System.out.println(“ Trapezoid area =” + tr_area1);
Parallelogram p1 = new Parallelogram ( 10,20);
float pr_area1 = p1.pr_area();
System.out.println(“Parallelogram area =” +pr_area1);
Rectangle r1 = new Rectangle( 5,10);
float rec_area1 = r1.rec_area();
System.out.println(“Rectangle area =” + rec_area1);
Square s1 = new Square(10);
float sqr_area1 = s1.sqr_area();
System.out.println(“Square area =” + sqr_area1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.