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

polygon public int getNumberOfSides(); public double getPerimeter(); public doub

ID: 3744941 • Letter: P

Question

polygon

public int getNumberOfSides();
  
public double getPerimeter();
  
public double getArea();

public abstract class Simple_Polygon implements Polygon

{
all array points must be organized in clockwise motion
  
public Simple_Polygon(int n)
{
Define attribute 'vertices' with a new array of 'Point's

size depends on parameter value.
  
If an argument is smaller than 3 throw an 'IllegalArgumentException' (beforehand).
}
  
  
Create numberofsides function and return the result which is the same as the number of vertices
  
  
  
This function determines whether or not this polygon has sides that are


all of equal length. If so, it returns true, otherwise it'll return false.


Make sure this function works no matter how many sides / vertices there are.


public boolean isEquilateral()
{
create and invoke this method
}
}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Polygon.java interface

public interface Polygon {

      public int getNumberOfSides();

      public double getPerimeter();

      public double getArea();

}

// Simple_Polygon.java

import java.awt.Point;

public abstract class Simple_Polygon implements Polygon {

      // array of Points to denote vertices

      private Point vertices[];

      public Simple_Polygon(int n) {

            // checking if number of vertices are less than 3

            if (n < 3) {

                  // invalid

                  throw new IllegalArgumentException();

            }

            // initializing the vertices array

            vertices = new Point[n];

      }

      @Override

      public int getNumberOfSides() {

            // number of sides = number of vertices

            return vertices.length;

      }

      /**

      * This function determines whether or not this polygon has sides that are

      * all of equal length. If so, it returns true, otherwise it'll return

      * false.This function works no matter how many sides vertices there are.

      */

      public boolean isEquilateral() {

            // for this, we need to find all side lengths

            double sideLength = -1; // to denote a side length

            // looping through all pair of vertices (adjacent)

            for (int i = 1; i < vertices.length; i++) {

                  // finding the distance between this vertex and the previous vertex

                  double side = vertices[i - 1].distance(vertices[i]);

                  if (sideLength == -1) {

                        // first side, updating sideLength variable

                        sideLength = side;

                  } else if (side != sideLength) {

                        // mismatch found, returning false

                        return false;

                  }

            }

            // checking the distance between last vertex and the first vertex

            if (vertices[vertices.length - 1].distance(vertices[0]) != sideLength) {

                  // mismatch found

                  return false;

            }

            // all checks are done, the polygon is equilateral

            return true;

      }

}

/* As Simple_Polygon is an abstract class, I cannot create objects and demonstrate the output without creating a sub class */