Skip to content

Instantly share code, notes, and snippets.

@ellbur
Created December 15, 2011 20:58
Show Gist options
  • Select an option

  • Save ellbur/1482843 to your computer and use it in GitHub Desktop.

Select an option

Save ellbur/1482843 to your computer and use it in GitHub Desktop.
Polygonz
public class Polygon
{
// define fields here
private int numvertices;
Point[] vertices;
public Polygon(int numvertices)
{
this.numvertices = numvertices;
this.vertices = new Point[numvertices];
}
public boolean setVertex(int vertexnumber, Point p)
{
System.out.println(numvertices);
if (vertexnumber >= numvertices) {
return false;
}
else if (vertexnumber < 0) {
return false;
}
else {
vertices[vertexnumber] = p;
System.out.println(java.util.Arrays.toString(vertices));
return true;
}
}
public double getPerimeter()
{
double total = 0.0;
for (int i=0; i<vertices.length; i++) {
int j = i + 1;
if (j == vertices.length) {
j = 0;
}
total = total + vertices[i].distanceTo(vertices[j]);
}
return total;
}
public double getArea()
{
return 0.0; // replace this line with your code
}
}
public class PolygonTest {
public static void main(String[] args) {
Polygon foo = new Polygon(3);
System.out.println(foo.getPerimeter());
foo.setVertex(0, new Point(3, 1));
foo.setVertex(1, new Point(3, 4));
foo.setVertex(2, new Point(5, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment