Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogr
ID: 3534264 • Letter: W
Question
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the base class of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance variables, properties, and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object
Explanation / Answer
//This is the program to use as a testing program.
import javax.swing.*;
public class QuadrilateralTest {
public static void main( String[] args )
{
// NOTE: All coordinates are assumed to form the proper shapes
// A quadrilateral is a four-sided polygon
Quadrilateral quadrilateral = new Quadrilateral(
1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4 );
// A trapazoid is a quadrilateral having two and only two
// parallel sides
Trapazoid trapazoid = new Trapazoid(
0.0, 0.0, 10.0, 0.0, 8.0, 5.0, 3.3, 5.0 );
// A parallelogram is a quadrilateral whose opposite sides are
// parallel
Parallelogram parallelogram = new Parallelogram(
5.0, 5.0, 11.0, 5.0, 12.0, 20.0, 6.0, 20.0 );
// A rectangle is an equiangular parallelogram
Rectangle rectangle = new Rectangle(
17.0, 14.0, 30.0, 14.0, 30.0, 28.0, 17.0, 28.0 );
// A square is an equiangular and equilateral parallelogram
Square square = new Square(
4.0, 0.0, 8.0, 0.0, 8.0, 4.0, 4.0, 4.0 );
String result = quadrilateral.toString() + " " +
trapazoid.toString() + " " + parallelogram.toString() +
" " + rectangle.toString() + " " + square.toString();
JOptionPane.showMessageDialog( null, result );
System.exit( 0 );
} // end method main
} // end class QuadrilateralTest
//**************************************************************************
Here is the code for the program:
// Class Point definition
public class Point {
private double x, y;
public Point( double xCoordinate, double yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public String toString()
{
return "( " + getX() + ", " + getY() + " )";
}
} // end class Point
//***************************************************************************
//
// Class Quadrilateral definition
public class Quadrilateral {
Point point1, point2, point3, point4;
public Quadrilateral( double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4 )
{
point1 = new Point( x1, y1 );
point2 = new Point( x2, y2 );
point3 = new Point( x3, y3 );
point4 = new Point( x4, y4 );
}
public Point getPoint1()
{
return point1;
}
public Point getPoint2()
{
return point2;
}
public Point getPoint3()
{
return point3;
}
public Point getPoint4()
{
return point4;
}
public String toString()
{
return "Coordinates of Quadrilateral are: " + printCoordinates();
}
public String printCoordinates()
{
return point1.toString() + ", " + point2.toString() + ", " +
point3.toString() + ", " + point4.toString();
}
} // end class Quadrilateral
//****************************************************************
//
// Class Trapazoid definition
public class Trapazoid extends Quadrilateral {
double height;
public Trapazoid( double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4 )
{
super( x1, y1, x2, y2, x3, y3, x4, y4 );
}
public double getHeight()
{
if ( getPoint1().getY() == getPoint2().getY() )
return Math.abs( getPoint2().getY() - getPoint3().getY() );
else
return Math.abs( getPoint1().getY() - getPoint2().getY() );
}
public double getArea()
{
return getSumOfTwoSides() * getHeight() / 2.0;
}
public double getSumOfTwoSides()
{
if ( getPoint1().getY() == getPoint2().getY() )
return Math.abs( getPoint1().getX() - getPoint2().getX() ) +
Math.abs( getPoint3().getX() - getPoint4().getX() );
else
return Math.abs( getPoint2().getX() - getPoint3().getX() ) +
Math.abs( getPoint4().getX() - getPoint1().getX() );
}
public String toString()
{
return " Coordinates of Trapazoid are: " + printCoordinates() +
" Height is: " + getHeight() + " Area is: " + getArea();
}
} // end class Trapezoid
//******************************************************************
////
// Class Parallelogram definition
public class Parallelogram extends Trapazoid {
public Parallelogram( double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4 )
{
super( x1, y1, x2, y2, x3, y3, x4, y4 );
}
public double getWidth()
{
if ( getPoint1().getY() == getPoint2().getY() )
return Math.abs( getPoint1().getX() - getPoint2().getX() );
else
return Math.abs( getPoint2().getX() - getPoint3().getX() );
}
public String toString()
{
return " Coordinates of Parallelogram are: " +
printCoordinates() + " Width is: " + getWidth() +
" Height is: " + getHeight() + " Area is: " + getArea();
}
} // end class Parallelogram
//********************************************************************************************8
//
// Class Rectangle definition
public class Rectangle extends Parallelogram {
public Rectangle( double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4 )
{
super( x1, y1, x2, y2, x3, y3, x4, y4 );
}
public String toString()
{
return " Coordinates of Rectangle are: " + printCoordinates() +
" Width is: " + getWidth() + " Height is: " + getHeight() +
" Area is: " + getArea();
}
} // end class Rectangle
****************************************************************
////
// Class Square definition
public class Square extends Parallelogram {
public Square( double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4 )
{
super( x1, y1, x2, y2, x3, y3, x4, y4 );
}
public String toString()
{
return " Coordinates of Square are: " + printCoordinates() +
" Side is: " + getHeight() + " Area is: " + getArea();
}
} //
//************************************************************
Dont forget to rate :)
Cheers!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.