Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract class needs access to subclass attribute

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.

like image 213
franz9 Avatar asked Jun 12 '26 20:06

franz9


1 Answers

Think of:

  • your Polygon interface as java.util.List interface
  • your SimplePoygon abstract class as java.util.AbstractCollection
  • your ArrayPolygon concrete class as java.util.ArrayList

I 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:

/* 
 *   This class implements generic methods about polygon, like size, area and 
 *   so on, leveraging on the Vertex2D iterator.
 */
public abstract class SimplePolygon implements Polygon {

    // all concrete subclasses must implement iterator
    public abstract Iterator<Vertex2D> iterator();

    // this generic method prints the vertex list
    // using iterator hides the data structure used in implementation
    public String toString() {
       Iterator<Vertex2D> it = iterator();
       if (! it.hasNext())
           return "[]";

       StringBuilder sb = new StringBuilder();
       sb.append('Polygon: vertices = [');
       for (;;) {
           Vertex2D e = it.next();
           if (! it.hasNext())
               return sb.append("]").toString();
           sb.append(' ');
       }
    }
}

// for instance this concrete classes uses an ArrayList
public ArrayPolygon extends SimplePolygon {

    public Iterator<Vertex2D> iterator() {
       return new .....
    }
}
like image 183
guido Avatar answered Jun 14 '26 09:06

guido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!