I have a problem with my code (edit: whole code of these classes)
public abstract class SimplePolygon implements Polygon {
//protected Vertex2D[] varray; //this is wrong in tests
public double getWidth(){
double min = varray[0].getX(), max = varray[0].getX();
for(int i = 0;i<varray.length;i++){
max = Math.max(max,varray[i].getX());
min = Math.min(min,varray[i].getX());
}
return max - min;
}
public double getHeight(){
double min = varray[0].getY(), max = varray[0].getY();
for(int i = 0;i<varray.length;i++){
max = Math.max(max,varray[i].getY());
min = Math.min(min,varray[i].getY());
}
return max - min;
}
public double getLength(){
double distance = 0;
for(int i = 0;i<varray.length;i++){
if((i+1)<varray.length){distance += varray[i].distance(varray[i+1]);}
else{distance += varray[i].distance(varray[0]);}
}
return distance;
}
public double getArea(){
double suma = 0;
for(int i = 0;i<varray.length-1;i++){
suma += varray[i].getX()*varray[i+1].getY() - varray[i+1].getX()*varray[i].getY();
}
return suma/2;
}
public String toString(){
String str = "Polygon: vertices =";
for(int i = 0;i<varray.length;i++){
str += " ";
str += varray[i];
}
return str;
}
}
public class ArrayPolygon extends SimplePolygon {
public ArrayPolygon(Vertex2D[] array){
varray = new Vertex2D[array.length];
if (array == null){}
for(int i = 0;i<array.length;i++){
if (array[i] == null){}
varray[i] = array[i];
}
}
public Vertex2D getVertex(int index) throws IllegalArgumentException{
return varray[index];
}
public int getNumVertices(){
return varray.length;
}
}
Problem is, that i’m not allowed to add any attribute or method to abstract class SimplePolygon, so i can’t properly initialize varray. It could simply be solved with protected attrib in that class, but for some (stupid) reason i can’t do that. Has anybody an idea how to solve it without that? Thanks for all help.
Think of:
Polygoninterface asjava.util.ListinterfaceSimplePoygonabstract class asjava.util.AbstractCollectionArrayPolygonconcrete class asjava.util.ArrayListI think the point of your assignment is implementing a solution with an iterator, in that way you could implement generic methods about polygons in the abstract class, while hiding the actual data structure containing the data point in the concrete classes; so: