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

I have provided a PolyShape class that represents polygons by their number of si

ID: 3676253 • Letter: I

Question

I have provided a PolyShape class that represents polygons by their number of sides and an array that contains their side lengths. You will write child classes and a driver program to use PolyShape.

Do not modify the PolyShape class.

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.

I have provided a sample program so you can see how the driver program works. To run the sample:

On a MAC, open a terminal window. On a PC, open a command window (type "cmd" in the search box).

Move to the folder where you downloaded the file (you can use "cd full/file/path").

Then use this command: java -jar ShapeCreator.jar

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.

You will be graded both on your syntax but also your program design. For full credit, consider all of the design elements listed below.

given code:

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;
   }
}

Explanation / Answer

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;
    }
}

class Quadrilateral extends PolyShape{
   public Quadrilateral(int sides[]) {
       super(sides.length, sides);
   }
   public String toString() {
        String s = "I am a shape with " + getNumSides()+ " sides of length: ";
        for(int length : getSideLengths())
            s += length + " ";
        s += " I am a Quadrilateral.";
        return s;
    }
}
////////////////////////////////////////////////////////////////////////////////
class Rectangle extends PolyShape{

   public Rectangle(int numSides, int[] sideLengths) {
       super(numSides, sideLengths);
   }
  
   public int getArea(){
       return getSideLengths()[0]*getSideLengths()[2];
   }
   public String toString() {
        String s = "I am a shape with " + getNumSides()+ " sides of length: ";
        for(int length : getSideLengths())
            s += length + " ";
        s += " I am a Rectangle.";
        return s;
    }
  
}
///////////////////////////////////////////////////////////////////////////////
class Square extends PolyShape{

   public Square(int numSides, int[] sideLengths) {
       super(numSides, sideLengths);
   }
  
   public int getArea(){
       return getSideLengths()[0]*getSideLengths()[2];
   }
   public String toString() {
        String s = "I am a shape with " + getNumSides()+ " sides of length: ";
        for(int length : getSideLengths())
            s += length + " ";
        s += " I am a Square.";
        return s;
    }
}
////////////////////////////////////////////////////////////////////////////////////
// Triangle class
class Triangle extends PolyShape{

   public Triangle(int numSides, int[] sideLengths) {
       super(numSides, sideLengths);
   }
  
   public boolean isIsoceles(){
       int sides[] = getSideLengths();
       if((sides[0]==sides[1]) || (sides[1]==sides[2]) || (sides[0]==sides[2]))
           return true;
       return false;
   }
  
   public boolean isEquilateral(){
       int sides[] = getSideLengths();
       if((sides[0]==sides[1]) && (sides[1]==sides[2]))
           return true;
       return false;
   }
   public String toString() {
        String s = "I am a shape with " + getNumSides()+ " sides of length: ";
        for(int length : getSideLengths())
            s += length + " ";
        s += " I am a Triangle.";
        return s;
    }
  
}

class Driver{
   public static void main(String[] args) {
       PolyShape[] shapes = {
           new Rectangle(4, new int[]{2,3,2,3}),
           new Square(4, new int[]{3,3,3,3}),
           new Triangle(3, new int[]{3,4,5}),
           new Triangle(3, new int[]{5,5,7})
       };
      
       for(PolyShape p : shapes){
           System.out.println(p.toString());
           System.out.println(p.getPerimeter());
           if(p instanceof Rectangle)
               System.out.println(((Rectangle)p).getArea());
           if(p instanceof Square)
               System.out.println(((Square)p).getArea());
          
       }
   }
}

/*

Output:

I am a shape with 4 sides of length: 2 3 2 3
I am a Rectangle.
10
4
I am a shape with 4 sides of length: 3 3 3 3
I am a Square.
12
9
I am a shape with 3 sides of length: 3 4 5
I am a Triangle.
12
I am a shape with 3 sides of length: 5 5 7
I am a Triangle.
17

*/