Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA CODING SUPPLY CODE AND COMMENTS WITH OUTPUT USING NETBEANS Write an inherit

ID: 3665460 • Letter: J

Question

JAVA CODING SUPPLY CODE AND COMMENTS WITH OUTPUT USING NETBEANS

Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass 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 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's area (except Quadrilateral). Each class should use an area calculation appropriate for that class. The four coordinate points of each figure should be specified in the following order, top left, top right, bottom right, and bottom left. To simplify the calculations, you can assume that the figures have tops and bottoms that are parallel to the x axis.

The following code specifies the coordinates for the four figure types:
        
        //Trapezoid coordinate points
        Point trapezoidTL = new Point(2.0, 4.0);
        Point trapezoidTR = new Point(5.0, 4.0);
        Point trapezoidBR = new Point(4.0, 2.0);
        Point trapezoidBL = new Point(3.0, 2.0);
        
        //Parallelogram coordinate points
        Point parallelogramTL = new Point(2.0, 4.0);
        Point parallelogramTR = new Point(5.0, 4.0);
        Point parallelogramBR = new Point(6.0, 2.0);
        Point parallelogramBL = new Point(3.0, 2.0);
        
        //Rectangle coordinate points
        Point rectangleTL = new Point(2.0, 4.0);
        Point rectangleTR = new Point(5.0, 4.0);
        Point rectangleBR = new Point(5.0, 2.0);
        Point rectangleBL = new Point(2.0, 2.0);
        
        //Square coordinate points
        Point squareTL = new Point(2.0, 4.0);
        Point squareTR = new Point(4.0, 4.0);
        Point squareBR = new Point(4.0, 2.0);
        Point squareBL = new Point(2.0, 2.0);

For those figures the results would be:

run:
The area of the trapezoid is: 4.000000
The area of the parallelogram is: 6.000000
The area of the rectangle is: 6.000000
The area of the square is: 4.000000
BUILD SUCCESSFUL (total time: 1 second)

Explanation / Answer

point.java

public class point {

   private double x;
   private double y;

   public double getX() {
       return x;
   }

   public void setX(double x) {
       this.x = x;
   }

   public double getY() {
       return y;
   }

   public void setY(double y) {
       this.y = y;
   }

   public point(double xcor, double ycord) {
       this.x = xcor;
       this.y = ycord;
   }

}

quadrilateral.java


public class quadrilateral {

   public quadrilateral(point TL,point TR,point BR,point BL)
   {
       this.pointTL=TL;
       this.pointTR=TR;
       this.pointBL=BL;
       this.pointBR=BR;
   }
   //Declaring the private variables for representing points for all quadrilaterals.
   private point pointTL;
   private point pointTR;
   private point pointBR;
   private point pointBL;
  
   public point getPointTL() {
       return pointTL;
   }
   public void setPointTL(point pointTL) {
       this.pointTL = pointTL;
   }
   public point getPointTR() {
       return pointTR;
   }
   public void setPointTR(point pointTR) {
       this.pointTR = pointTR;
   }
   public point getPointBR() {
       return pointBR;
   }
   public void setPointBR(point pointBR) {
       this.pointBR = pointBR;
   }
   public point getPointBL() {
       return pointBL;
   }
   public void setPointBL(point pointBL) {
       this.pointBL = pointBL;
   }

}

trapezoid.java


public class trapezoid extends quadrilateral {

   //Derived directly from quadrilateral
   public trapezoid(point TL,point TR,point BR,point BL) {
      
       super(TL,TR,BR,BL);
   }
  
   //Area calculation for trapezoid, area=((top+bottom)/2)*height
   public double area()
   {
       point topLeft=this.getPointTL();
       point topRight=this.getPointTR();
       point bottomRight=this.getPointBR();
       point bottomLeft=this.getPointBL();
      
       //breadth of top of trapezoid
       double breadthTop=topRight.getX()-topLeft.getX();
       //breadth of bottom of trapezoid
       double breadthBottom=bottomRight.getX()-bottomLeft.getX();
       //height of trapezoif from base
       double height=topLeft.getY()-bottomLeft.getY();
      
       double area=((breadthTop+breadthBottom)*height)/2;
      
       return area;
   }

}

parallelogram.java


public class parallelogram extends quadrilateral {

   public parallelogram(point TL,point TR,point BR,point BL) {
       //Calling the constructor of superclass which is quadrilateral for setting up the four corner points
       super(TL,TR,BR,BL);
   }
  
   //Calculating area which is equal to base*height,rectangle is also a parallelogram.
   public double area()
   {
       point topLeft=this.getPointTL();
       point topRight=this.getPointTR();
       point bottomRight=this.getPointBR();
       point bottomLeft=this.getPointBL();
      
       double breadth=topRight.getX()-topLeft.getX();
       double length=topLeft.getY()-bottomLeft.getY();
  
       return breadth*length;
   }

}

rectangle.java


public class rectangle extends parallelogram {

   public rectangle(point TL,point TR,point BR,point BL) {
      
       super(TL,TR,BR,BL);
   }

   //No need for an area function,as a rectangle is a specific case of parallerogram
   //We will use the area function of parallelogram to do the area calculation
}

square.java

public class square extends rectangle {

   public square(point TL, point TR, point BR, point BL) {

       super(TL, TR, BR, BL);
   }

   //No need for an area function,as a square is a specific case of rectangle and parallelogram
   //We will use the area function of parallelogram to do the area calculation
}

Test.java(for creating objects of these classes and calculating area)

public class Test {

   public static void main(String[] args) {

       //Trapezoid coordinate points
point trapezoidTL = new point(2.0, 4.0);
point trapezoidTR = new point(5.0, 4.0);
point trapezoidBR = new point(4.0, 2.0);
point trapezoidBL = new point(3.0, 2.0);
  
//Parallelogram coordinate points
point parallelogramTL = new point(2.0, 4.0);
point parallelogramTR = new point(5.0, 4.0);
point parallelogramBR = new point(6.0, 2.0);
point parallelogramBL = new point(3.0, 2.0);
  
//Rectangle coordinate points
point rectangleTL = new point(2.0, 4.0);
point rectangleTR = new point(5.0, 4.0);
point rectangleBR = new point(5.0, 2.0);
point rectangleBL = new point(2.0, 2.0);
  
//Square coordinate points
point squareTL = new point(2.0, 4.0);
point squareTR = new point(4.0, 4.0);
point squareBR = new point(4.0, 2.0);
point squareBL = new point(2.0, 2.0);
      
       trapezoid t = new trapezoid(trapezoidTL,trapezoidTR,trapezoidBR,trapezoidBL);
      
       parallelogram p=new parallelogram(parallelogramTL,parallelogramTR,parallelogramBR,parallelogramBL);
      
       rectangle r=new rectangle(rectangleTL,rectangleTR,rectangleBL,rectangleBR);
      
       square s=new square(squareTL,squareTR,squareBR,squareBL);
      
       System.out.println("The area of the trapezoid is: "+t.area());
       System.out.println("The area of the parallelogram is: "+p.area());
       System.out.println("The area of the rectangle is: "+r.area());
       System.out.println("The area of the square is: "+s.area());

   }

}

sample output:

The area of the trapezoid is: 4.0
The area of the parallelogram is: 6.0
The area of the rectangle is: 6.0
The area of the square is: 4.0