java - Duplicating Array and Acting Upon it -


i asked question earlier cloning array when change area/scale polygon etc... not change values of array (basics of encapsulation). after trying duplicating array another, using clone array, or using array list, still having problems. have suggestions how how duplicate array can make changes it, return value in method, still maintain integrity of original array passed constructor? point class defines values passed in. post well. polygon created 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];  (int = 0; < points.length; i++)  this.points[i] = points[i]; }    public point getvertexaverage() {      double xsum = 0;      double ysum = 0;     (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 = getcentroid();     point newcentroid = new point(a.getx()+ c.getx(), a.gety() +c.gety());   }  public void scale(double factor) {     (int index = 0; index < points.length; index++)     {         double x = points[index].getx() *factor;         double y = points[index].gety() * factor;         point = new point(x,y);         points[index] = a;      } }     public point[] getpoints() {    //  point[] newpoints = new point[0];     //for (int = 0; <newpoints.length; i++)     //{     // newpoints = points.get(i);     //}     return points;  }     public rectangle getboundingbox() {     double minx= 99999;     double maxx= -999999;     double miny= 999999;     double maxy= -999999;      (int = 1; i<points.length; ++){         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 = new point(minx, miny);         point b = new point(maxx, maxy);         //need next 3 cases     }     return null;             //new rectangle(point a, point b); }     public point getcentroid() {     double x = 0;     double y =0;     double = 1/(6*getarea());     (int = 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 = new point(x,y);     return a; }     public double getarea() {     double area = 0; (int = 0; <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> ();      (int index = 0; index < points.length; index++) {         { double x = points[index].getx()+dx;           double y = points[index].gety()+dy;           point = new point(x,y);          } }           } } 

i not sure want clone array; believe tried like:

// won't work because keep same reference old objects public point[] clonearrayofpoints(point[] points) {     point[] result = new point[points.length];     (int = 0; < points.length; i++){         result.points[i] = points[i];     }     return result; } 

but, should try this:

// work because create new objects same old values public point[] clonearrayofpoints(point[] points) {     point[] result = new point[points.length];     (int = 0; < points.length; i++){         result.points[i] = new point(points[i].getx(), points[i].gety());     }     return result; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -