Question

JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

JAVA: Quadrilateral Inheritance Hierarchy

Write an inheritance hierarchy for classes Quadrilateral, 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 as possible, i.e. more than two levels. 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. A program that instantiates objects of your classes and outputs each object's area (except Quadrilateral) has been provided.  

CODE:

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 parallelogram is a quadrilateral with opposite sides 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);

System.out.printf(
"%s %s %s %s\n", quadrilateral, parallelogram,
rectangle, square);
}
}

RESULT:

Coordinates of Quadrilateral are:
2 (1.1, 1.2), (6.6, 2.8), (6.2, 9.9), (2.2, 7.4)
3
4 Coordinates of Parallelogram are:
5 (5.0, 5.0), (11.0, 5.0), (12.0, 20.0), (6.0, 20.0)
6 Width is: 6.0
7 Height is: 15.0
8 Area is: 90.0
9
10 Coordinates of Rectangle are:
11 (17.0, 14.0), (30.0, 14.0), (30.0, 28.0), (17.0, 28.0)
12 Width is: 13.0
13 Height is: 14.0
14 Area is: 182.0
15
16 Coordinates of Square are:
17 (4.0, 0.0), (8.0, 0.0), (8.0, 4.0), (4.0, 4.0)
18 Side is: 4.0
19 Area is: 16.0
0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Point.java

public class Point {
   private double x;
   private double y;

   public Point(double x, double y) {
       this.x = x;
       this.y = 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;
   }


   @Override
   public String toString() {
       return "(" + x + "," + y + ")";
   }

}
________________________

// Quadrilateral.java

public class Quadrilateral {
   private Point p1;
   private Point p2;
   private Point p3;
   private Point p4;

   public Quadrilateral(double x1, double y1, double x2, double y2, double x3, double y3,
           double x4, double y4) {
       this.p1 = new Point(x1, y1);
       this.p2 = new Point(x2, y2);
       this.p3 = new Point(x3, y3);
       this.p4 = new Point(x4, y4);
   }
  
   public Point getP1() {
       return p1;
   }

   public void setP1(Point p1) {
       this.p1 = p1;
   }

   public Point getP2() {
       return p2;
   }

   public void setP2(Point p2) {
       this.p2 = p2;
   }

   public Point getP3() {
       return p3;
   }

   public void setP3(Point p3) {
       this.p3 = p3;
   }

   public Point getP4() {
       return p4;
   }

   public void setP4(Point p4) {
       this.p4 = p4;
   }

   @Override
   public String toString() {
       return p1 + ", " + p2 + ", " + p3 + ", " + p4;
   }

}
__________________________

// Rectangle.java

import java.text.DecimalFormat;

public class Rectangle extends Quadrilateral {
private double width;
private double height;
  
   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);
       width = Math.sqrt(Math.pow((getP2().getX() - getP1().getX()), 2)
               + Math.pow((getP2().getY() - getP1().getY()), 2));
       height = Math.sqrt(Math.pow((getP4().getX() - getP1().getX()), 2)
               + Math.pow((getP4().getY() - getP1().getY()), 2));
   }


   public double area() {
      
       return width*height;
   }
   @Override
   public String toString() {
       // DecimalFormat class is used to format the output
               DecimalFormat df = new DecimalFormat(".0");
              
       return "\nCoordinates of Rectangle are:\n" + super.toString()
               + "\nWidth is :" + df.format(width) + "\nHeight is :" + df.format(height)
               + "\nArea is :" + df.format(area());
   }


}
____________________________

// Square.java

import java.text.DecimalFormat;

public class Square extends Quadrilateral {

   private double side;

   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);
       side = Math.sqrt(Math.pow((getP2().getX() - getP1().getX()), 2)
               + Math.pow((getP2().getY() - getP1().getY()), 2));
   }


   public double area() {

       return 4 * side;
   }

   @Override
   public String toString() {
       // DecimalFormat class is used to format the output
       DecimalFormat df = new DecimalFormat(".0");
       return "\nCoordinates of Square are:\n" + super.toString()
               + "\nSide is :" + df.format(side)+"\nArea is :" + df.format(area());
   }

}
___________________________

// Parallelogram.java

import java.text.DecimalFormat;

public class Parallelogram extends Quadrilateral {
   private double width;
   private double height;

   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);
       width = Math.sqrt(Math.pow((getP2().getX() - getP1().getX()), 2)
               + Math.pow((getP2().getY() - getP1().getY()), 2));
       height = Math.sqrt(Math.pow((getP4().getX() - getP1().getX()), 2)
               + Math.pow((getP4().getY() - getP1().getY()), 2));
   }

   public double area() {

       return width * height;
   }

   @Override
   public String toString() {
       // DecimalFormat class is used to format the output
       DecimalFormat df = new DecimalFormat(".0");
       return "\nCoordinates of Parallelogram are:\n" + super.toString()
               + "\nWidth is :" + df.format(width) + "\nHeight is :" + df.format(height)
               + "\nArea is :" + df.format(area());
   }

}
__________________________

// QuadrilateralTest.java

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 parallelogram is a quadrilateral with opposite sides 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);

System.out.printf(
"%s %s %s %s\n", quadrilateral, parallelogram,
rectangle, square);
}
}
____________________________

Output:

(1.1,1.2), (6.6,2.8), (6.2,9.9), (2.2,7.4)
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.2
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

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • USE EXCEL TO CALCULATE THE FREQUENCIES AS SHOWN BELOW. PLEASE PROVIDE EXCEL FORMULA USED. Frequency Distribution...

    USE EXCEL TO CALCULATE THE FREQUENCIES AS SHOWN BELOW. PLEASE PROVIDE EXCEL FORMULA USED. Frequency Distribution Low High Bins Frequency -67.0 -56.6 (-67, -56.6] -56.6 -46.2 (-56.6, -46.2] -46.2 -35.8 (-46.2, -35.8] -35.8 -25.4 (-35.8, -25.4] -25.4 -15.0 (-25.4, -15] -15.0 -4.6 (-15, -4.6] -4.6 5.8 (-4.6, 5.8] 5.8 16.2 (5.8, 16.2] 16.2 26.6 (16.2, 26.6] 26.6 37.0 (26.6, 37] 37.0 47.4 (37, 47.4] 47.4 57.8 (47.4, 57.8] 57.8 68.2 (57.8, 68.2] 68.2 78.6 (68.2, 78.6] 78.6 89.0 (78.6, 89]...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

  • sunspot.year is a built-in R time series dataset which gives the mean yearly numbers of sunspots...

    sunspot.year is a built-in R time series dataset which gives the mean yearly numbers of sunspots from 1700 to 1988 rounded to one digit. This data is maintained up until the current date at WDC-SILSO, Royal Observatory of Belgium. We are interested in some descriptive statistics related to the sunspot.year time series. We can access this data directly and convert the time series into a vector by using the assignment x <- as.vector(sunspot.year). (In R use ?sunspot.year for info on...

  • The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and incl...

    The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and include them in your project. IheじML diagram below details the class Point くくfriend>> ostream& operator.((ostream&, point&) <ごfriend::. İstream& operator:..イ1stream&-point& - : double - v doublc getX) double getYO double - sctX( double): void - set Y(double) : void - point(double-0.0, double-0.0 operator-(const point& bool perator< const...

  • a) Insert here a copy of the plot for the heating curve you created in laboratory....

    a) Insert here a copy of the plot for the heating curve you created in laboratory. Remember that an acceptable plot has labels and units on the axes, a legend, and a title. PLOT GRAPH Run 1 Time   Temperature   T   T   min   °C   0.0   22.0   0.2   22.0   0.5   22.1   0.8   21.9   1.0   22.2   1.2   22.1   1.5   22.1   1.8   22.3   2.0   22.0   2.2   21.9   2.5   22.0   2.8   22.2   3.0   22.7   3.2   23.6   3.5   24.7   3.8   26.5   4.0   28.0   4.2   29.6   4.5  ...

  • find v belt drive design power select belt type determine shive size (belt speed 4000 ft/min)...

    find v belt drive design power select belt type determine shive size (belt speed 4000 ft/min) find shive size from power rating figure find rated power find estimated centre distance find belt length (by selecting standard belt length) calculate actual centre distance find contact angle for small shieve determine correct factors calculate correct power per belt no. of belt needed V-Belt Designing Sample Problem . Given: A 4 cylinder diesel engine runs at 80 hp, 1800 rpm, to drive a...

  • CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in...

    CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in Appendix A. Required: Compute the following ratios for Year 11. Liquidity ratios: Asset utilization ratios:* a. Current ratio n. Cash turnover b. Acid-test ratio 0. Accounts receivable turnover c. Days to sell inventory p. Inventory turnover d. Collection period 4. Working capital turnover Capital structure and solvency ratios: 1. Fixed assets turnover e. Total debt to total equity s. Total assets turnover f. Long-term...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT