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

There is a program about inheritance. Hope some smart ones can help me! I post t

ID: 3676404 • Letter: T

Question

There is a program about inheritance. Hope some smart ones can help me! I post this question before, but that answer is not for my code... I appreciate it if some one can have a correct answer!

Write four classes that extend PolyShape.

Quadrilateral

This class represents polygons with four sides

Rectangle

The class represents four-sided polygons where opposite sides are equal length.

This class should have a method getArea() that takes no parameters and returns an integer.

Square

This class represents four-sided polygons where all four sides are of equal length.

This class should have a method getArea() that takes no parameters and returns an integer.

Triangle

This class represents polygons with three sides

This class should have methods isIsoceles and isEquilateral that returns a boolean if two or three (respectively) sides are of equal length

In each class, include:

one or more constructors

getters and setters

toString method that returns the number of sides, the side lengths, and all possible names for a shape.

For example, a square might print "I have four sides of length 3, 3, 3, and 3. I am a polygon. I am a quadrilateral. I am a rectangle. I am a square."

Write an interactive driver program.The program allows the user to repeatedly create a shape by entering its dimensions.

Note: you do not need to handle negative or decimal values in the driver program. You can assume the user will enter positive integer input only.

The program prints the object created and the perimeter, area (if applicable), and isIsoceles/isEquilateral (if applicable) methods.

Your classes should work with your driver program, but they should be designed to work with other drivers, too (for example, a driver I might create).

Think carefully about what kind of checks should go in the driver program and what kind of checks should go in the individual classes.

Remember that object-oriented design states that each object should be in charge of its own information.

Your classes should work with your driver program, but they should be designed to work with other drivers, too (for example, a driver I might create).

Think carefully about what kind of checks should go in the driver program and what kind of checks should go in the individual classes.

Remember that object-oriented design states that each object should be in charge of its own information.

There is the PolyShape programm:

public class PolyShape {
   private int numSides;
   private int[] sideLengths;
  
   /*
   * note: PolyShape takes in an int[] because we don't know
   * in advance how many sides a polygon will have... but that
   * doesn't mean the child classes have to follow that same
   * setup- make sure the parameters of the child class
   * constructors make sense for that class!
   *
   * also note: there is no error checking here to make sure
   * that sideLengths has the appropriate number of values; for now,
   * we just assume the user will send in the proper information
   */
   public PolyShape(int numSides, int[] sideLengths) {
       this.numSides = numSides;
       this.sideLengths = sideLengths;
   }
  
   public void setNumSides(int numSides) {
       this.numSides = numSides;
   }
   public int getNumSides() {
       return numSides;
   }
   public void setSideLengths(int[] sideLengths) {
       this.sideLengths = sideLengths;
   }
   public int[] getSideLengths() {
       return sideLengths;
   }
   public int getPerimeter() {
       int perim = 0;
       for(int n : sideLengths)
           perim += n;
       return perim;
   }
   public String toString() {
       String s = "I am a shape with " + numSides + " sides of length: ";
       for(int length : sideLengths)
           s += length + " ";
       s += " I am a polygon.";
       return s;
   }
}

Thank you for your work!

Explanation / Answer

Polygon.java

package org.students;

public abstract class Polygon {
   public abstract double calculateArea();
}

-----------------------------------------------------------------------------------

Quadrilateral.java

package org.students;

import java.awt.geom.*;

public class Quadrilateral extends Polygon {
   protected double base = 0.0;
   protected double height = 0.0;

   public Quadrilateral() {
       super();
   }// end of constructor

   public Quadrilateral(double width, double height) {
       super();
       this.base = width;
       this.height = height;

   }// end of constructor

   public double calculateArea() {
       /*
       * The area of a Parallelogram is calculated as follows: (base * height)
       */
       double area = base * height;
       return area;
   }// end of method calculateArea()

   public String toString() {
       System.out.println("Name of class : " + getClass());

       System.out.println("Base : " + base);
       System.out.println("Height : " + height);
       System.out.println("Area : " + calculateArea());
       System.out.println("Perimeter : " + calculatePerimeter());
       System.out
               .println("=====================================================");
       return "";
   }// end of method toString()

   public double calculatePerimeter() {
       double perimeter = 2 * (base + height);
       return perimeter;
   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------

Square.java

package org.students;

public class Square extends Polygon {
   protected double side = 0.0;

   public Square() {
       super();
   }// end of constructor

   public Square(double side) {
       super();
       this.side = side;
   }// end of constructor

   @Override
   public double calculateArea() {
       double area = side * side;
       return area;
   }

   public String toString() {
       System.out.println("Name of class : " + getClass());

       System.out.println("Side : " + side);

       System.out.println("Area : " + calculateArea());
       System.out.println("Perimeter : " + calculatePerimeter());
       System.out
               .println("=====================================================");
       return "";
   }// end of method toString()

   @Override
   public double calculatePerimeter() {
       double perimeter = 4 * side;
       return perimeter;
   }
}// end of class Square

---------------------------------------------------------------------------------------------------------------

Rectangle.java

package org.students;

public class Rectangle extends Polygon {
   protected double base = 0.0;
   protected double height = 0.0;

   public Rectangle() {
       super();
   }// end of constructor

   public Rectangle(double width, double height) {
       super();
       this.base = width;
       this.height = height;
   }// end of constructor

   public double calculateArea() {
       /*
       * The area of a Parallelogram is calculated as follows: (base * height)
       */
       double area = base * height;
       return area;
   }// end of method calculateArea()

   public String toString() {
       System.out.println("Name of class : " + getClass());

       System.out.println("Base : " + base);
       System.out.println("Height : " + height);
       System.out.println("Area : " + calculateArea());
       System.out.println("Perimeter : " + calculatePerimeter());
       System.out
               .println("=====================================================");
       return "";
   }// end of method toString()

   public double calculatePerimeter() {
       double perimeter = 2 * (base + height);
       return perimeter;
   }
}// end of class Rectangle

---------------------------------------------------------------------------------------------------------------------------------------------

Triangle.java

package org.students;

public class Triangle extends Polygon{
protected double base=0.0;
protected double height=0.0;
   public Triangle() {
      
   }

   public Triangle(double base,double height) {
       this.base=base;
       this.height=height;
   }

  
   public double calculateArea() {
       double area=0.5*base*height;
       return area;
   }

  
   public String toString() {
       System.out.println("Name of class : " + getClass());

       System.out.println("Base : " + base);
       System.out.println("Height : " + height);
       System.out.println("Area :" + calculateArea());
       System.out
               .println("=====================================================");
       return "";
   }// end of method toString()

}

--------------------------------------------------------------------------------------------------------------------------------------------------------

ShapeGenerator.java (This class contains main method)

package org.students;


       public class ShapeGenerator
       {
       public static void main(String args[])
       {
           Polygon shapes[] = { new Rectangle(10, 20), new Quadrilateral(10, 20),
                   new Square(10),new Triangle(10,5) };
           double area = 0;
           for (int i = 0; i < shapes.length; i++) {
               shapes[i].toString();
           }// end of for loop
  
      
       }//end of class ShapeGenerator
       }
-------------------------------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote