Need help writing java program that follows Write an inherit
Need help writing java program that follows:
Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelogram, Rectangle, and Square. Use Quadrilateral as the super class of the hierarchy. Make the hierarchy as deep I.e., as many levels) as possible. Specify the instance variables and methods for each class. The private data of the Quadrilateral should be the x-y coordinate pairs for the four end-points of the Quadrilateral. Write a program that instantiate objects of you classes and outputs each object area (except Quadrilateral)
desired output should look something like this :
Coordinates of Quadrilateral are:
(1.1, 1.2) , (6.6, 2.8), (6.2, 9.9), (2.2, 7.4)
Coordinates of Trapezoid are:
(0.0,0.0) , (10.0, 0.0), (8.0, 5.0), (3.3, 5.0)
Height is : 5
Area is : 36.75
Coordinates of Parallelogram are:
(5.0,5.0) , (11.0, 5.0), (12.0, 20.0), (6.0, 20.0)
Width is: 6
Height is : 15
Area is : 90
Coordinates of Rectangle are:
(17.0,14.0) , (30.0, 14.0), (30.0, 28.0), (17.0, 28.0)
Width is: 13
Height is : 14
Area is : 182
Coordinates of Square are:
(4.0, 0.0), (8.0,0.0), (8.0, 4.0), (4.0, 4.0)
Side is: 4
Area is: 16
Solution
//Point.java
//Point class defintion
public class Point
{
private double x;
private double y;
//two arguement constructor
public Point (double xCoordinate, double yCoordinate)
{
x = xCoordinate;
y = yCoordinate;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
//Returns string of Point class object
@Override
public String toString()
{
return String.format( \"(%2.1f,%2.1f)\", getX(), getY());
}
}
------------------------------------------------------
//Quadrillateral.java
public class Quadrillateral
{
private Point point1;
private Point point2;
private Point point3;
private Point point4;
public Quadrillateral (
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;
}
//Returns string of the Quadrillateral class object
@Override
public String toString()
{
return String.format( \"%s %s,%s,%s,%s\",
\"Coordinates of Quadrilateral are :\ \",
point1,point2,point3,point4);
}
public String getCoordinatesAsString()
{
return String.format(\"\ %s,%s,%s,%s\", point1, point2, point3, point4);
}
}
------------------------------------------------------
//Quadrillateral.java
public class Trapezoid extends Quadrillateral
{
private double height;
public Trapezoid (double x1, double x2,
double x3, double x4,
double y1, double y2,
double y3, double y4)
{
super (x1,x2,x3,x4,y1,y2,y3,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());
}
@Override
public String toString()
{
return String.format(\"\ %s\ %s\ %s: %s\ %s : %s\"
,\"Coordiates of Trapezoid are\",
super.getCoordinatesAsString(),
\"Height is\", getHeight(),
\"Area is\", getArea());
}
}
------------------------------------------------------------------------------------------------------------
//Parallelogram.java
public class Parallelogram extends Trapezoid
{
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());
}
//Returns the string of the Parallelogram object
@Override
public String toString()
{
return String.format (\" \ %s %s\ %s%s\ %s%s \ %s%s \",
\"Coordinates of Parallelogram are\" ,
super.getCoordinatesAsString(),
\"Width is :\", getWidth(),
\"Height is :\", getHeight(),
\"Area is :\", getArea()
);
};
}
------------------------------------------------------
//Rectangle.java
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);
}
@Override
public String toString()
{
return String.format (\"\ %s%s\ %s%s\ %s%s\ %s%s \",
\"Coordinates of Rectangle are\",
super.getCoordinatesAsString(),
\"Width is : \", getWidth(),
\"Height is : \" ,getHeight(),
\"Area is : \" ,getArea());
}
}
------------------------------------------------------
//Square.java
//This class extends the Parallelogram class
public class Square extends Parallelogram
{
public Square (double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{
//calling base class Parallelogram
super(x1,y1,x2,y2,x3,y3,x4,y4);
}
//returns the string representation of Parallelogram class object
@Override
public String toString()
{
return String.format (\"\ %s%s\ %s%s\ %s%s \",
\"Coordinates of Square are\",
super.getCoordinatesAsString(),
\"side is : \", getWidth(),
\"Area is : \" ,getArea());
}
}
------------------------------------------------------
Output :
Coordinates of Quadrilateral are :
(1.1,1.2),(6.6,2.8),(6.2,9.9),(2.2,7.4)
Coordiates of Trapezoid are
(0.0,0.0),(10.0,0.0),(8.0,5.0),(3.3,5.0)
Height is: 5.0
Area is : 36.75
Coordinates of Parallelogram are
(5.0,5.0),(11.0,5.0),(12.0,20.0),(6.0,20.0)
Width is :6.0
Height is :15.0
Area is :90.0
Coordinates of Rectangle are
(17.0,14.0),(30.0,14.0),(30.0,28.0),(17.0,28.0)
Width is : 13.0
Height is : 14.0
Area is : 182.0
Coordinates of Square are
(4.0,0.0),(8.0,0.0),(8.0,4.0),(4.0,4.0)
side is : 4.0
Area is : 16.0




