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
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.
import java.util.Arrays;
public class PolyShape {
private int numSides;
private int[] sideLengths;
/*
* note: PolyShape constructor takes a variable-length parameter list;
* this means it can be invoked with any number of integers;
* inside the constructor, sideLengths is treated as an int[]
*
*/
public PolyShape(int ... sideLengths) {
this.sideLengths = sideLengths;
this.numSides = sideLengths.length;
}
public int getNumSides() {
return numSides;
}
public int[] getSideLengths() {
return Arrays.copyOf(sideLengths, sideLengths.length);
}
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;
}
}
Write four classes that extend PolyShape (either directly or indirectly).
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
Note you do not have to make sure the three side lengths represent a mathematically valid triangle!
In each class, include:
one or more constructors
getters and setters (if appropriate/necessary)
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
