Search code examples
javaencapsulation

Duplicating Array and Acting Upon it


I asked a question earlier about cloning an array so that when you change the area/scale the polygon etc... it will not change the values of the array (basics of Encapsulation). After trying duplicating the array into another, using a clone array, or using an array list, I am still having problems. Anyone have any suggestions how how to duplicate my array so that I can make changes to it, return the value in the method, but still maintain the integrity of the original array passed into the constructor? The Point class defines the values passed in. I will post that as well. Polygon created most of these methods. Frustrated...

 public class Point {

private double x;
private double y;

public Point(double x, double y) {
    this.x = x;
    this.y = y;
}

public double getX() {
    return x;
}

public double getY() {
    return y;
}

public Point translate(double dx, double dy) {
    return new Point(x+dx, y+dy);
}

public double distanceTo(Point p) {
    return Math.sqrt((p.x - x)*(p.x -x) + (p.y-y)*(p.y-y));
}

}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

 public class PolygonImpl implements Polygon {

   private double xSum=0;
    private double ySum=0;
    private Point[] points;
   private Point[] cloneList;
   private Point a;

  public PolygonImpl(Point[] points) {
 this.points = new Point[points.length];
 for (int i = 0; i < points.length; i++)
 this.points[i] = points[i];
}



public Point getVertexAverage() {
     double xSum = 0;
     double ySum = 0;
    for (int index = 0; index < points.length; index++) {
         xSum = xSum + points[index].getX();
         ySum = ySum + points[index].getY();
        }

    return new Point(xSum/getNumSides(), ySum/getNumSides());
}

public int getNumSides() {
    return points.length;
}

public void move(Point c) {
    Point a = getCentroid();
    Point newCentroid = new Point(a.getX()+ c.getX(), a.getY() +c.getY());


}

public void scale(double factor) {
    for (int index = 0; index < points.length; index++)
    {
        double x = points[index].getX() *factor;
        double y = points[index].getY() * factor;
        Point a = new Point(x,y);
        points[index] = a;

    }
}




public Point[] getPoints() {
   //  Point[] newPoints = new Point[0];
    //for (int i = 0; i <newPoints.length; i++)
    //{
    // newPoints = points.get(i);
    //}
    return points;

}




public Rectangle getBoundingBox() {
    double minX= 99999;
    double maxX= -999999;
    double minY= 999999;
    double maxY= -999999;

    for (int i = 1; i<points.length; i ++){
        if (points[i].getX()< minX )
        {
            minX= points[i].getX();
        }
        if (points[i].getX() > maxX)
        {
            maxX= points[i].getX();
        }
        if (points[i].getY()< minY )
        {
            minY= points[i].getY();
        }
        if (points[i].getY() > maxY)
        {
            maxY= points[i].getY();
        }

        Point a = new Point(minX, minY);
        Point b = new Point(maxX, maxY);
        //need for next three cases
    }
    return null;
            //new Rectangle(Point a, Point b);
}




public Point getCentroid() {
    double x = 0;
    double y =0;
    double something = 1/(6*getArea());
    for (int i = 0; i< points.length-1; i++)
    {
        x+= (points[i].getX() + points[i+1].getX())*(points[i].getX()*points[i+1].getY()-points[i+1].getX()*points[i].getY());
        y+= (points[i].getY() + points[i+1].getY())*(points[i].getX()*points[i+1].getY()-points[i+1].getX()*points[i].getY());
    }
        x = x*something;
        y= y*something;

    Point a = new Point(x,y);
    return a;
}




public double getArea() {
    double area = 0;
for (int i = 0; i <points.length-1; i++){
    area += points[i].getX()*points[i+1].getY() - points[i+1].getX()*points[i].getY();
}
    area = area/2;
return area;
}




public void move(double dx, double dy) {
    //ArrayList<Point> points = new ArrayList<Point> ();

    for (int index = 0; index < points.length; index++) {
        { double x = points[index].getX()+dx;
          double y = points[index].getY()+dy;
          Point a = new Point(x,y);

        }
}










}
}

Solution

  • I am not sure where you want to clone your array; however I believe that you tried something like:

    // This won't work because you keep the same reference to the old objects
    public Point[] cloneArrayOfPoints(Point[] points) {
        Point[] result = new Point[points.length];
        for (int i = 0; i < points.length; i++){
            result.points[i] = points[i];
        }
        return result;
    }
    

    But, you should try this:

    // This will work because you create new objects with same old values
    public Point[] cloneArrayOfPoints(Point[] points) {
        Point[] result = new Point[points.length];
        for (int i = 0; i < points.length; i++){
            result.points[i] = new Point(points[i].getX(), points[i].getY());
        }
        return result;
    }