/** * A class to implement a "regular polygon" This is an N-gon * in which every side is of equal length and every interior angle is the * same. * @author gtowell * Created: 10/12/2020 */ public class RegularPolygon extends GeometricObject { /** The number of sides in the regular N-gon */ int numSides; /** The length of every side */ int sideLength; /** * Make a regular polygon. Note that this is a static method. It needs to be as * we will use this method to create instances rather than directly using the constructor. * Not directly using constructor so that we can return different instances depending on * the number of sides. * @param n the number of sides * @param l the length of the side * @return an instance of either regular polygon OR something that extends RegularPolygon */ public static RegularPolygon regularPolygonBuilder(int n, int l) { if (n==4) return new SquareRP(l); else return new RegularPolygon(n, l); } /** * NOTE PROTECTED. We do not want this to be used by anyone outside of this * class or descendants. Rather use regularPolygonBuilder * @param n the number of sides * @param l the length of sides */ protected RegularPolygon(int n, int l) { sideLength=l; numSides=n; } /** The perimiter * @return the perimiter */ public int getPerimiter() { return numSides*sideLength; } /** * A complex formula but it works for all regular N-gons * @return the area */ public int getArea() { return (int)((sideLength*sideLength*numSides) / (4*Math.tan(Math.PI/numSides))); } }