1

したがって、このプロジェクトでは、多数のポイントを含む配列を入力したいと考えています。(特定の番号はありません)。X または Y 値の平均を取得するなどの変更を必要とするメソッドがあるため、メソッドとコンストラクターにどのようにアプローチするべきかについて混乱しています。このプロジェクトに正しい方法で取り組んでいますか? ポイントリストのクローン、または配列リストなどを使用する必要がありますか... (例として PolygonImpl クラスの一部のみを示していることに注意してください。それらはすべて同様に機能します) クラスポイントには次が含まれます:

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));
}

}

public class PolygonImpl implements Polygon {

private double xSum=0;
private double ySum=0;
private ArrayList<Point> points;
private Point[] pList;
private Point a;

PolygonImpl(Point[] pList) {

    this.pList = pList.clone();
    points = new ArrayList<Point>();

    for (int index = 0; index < pList.length; index++) {
           points.add(pList[index]);
       xSum += pList[index].getX();
       ySum += pList[index].getY();


    }
}



public Point getVertexAverage() {
     double xSum = 0;
     ArrayList<Point> vlist = new ArrayList<Point> ();
     double ySum = 0;
    for (int index = 0; index < vlist.size(); index++) {
         xSum = xSum + vlist.get(index).getX();
         ySum = ySum + vlist.get(index).getY();
        }

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

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

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


}

public void scale(double factor) {

 ArrayList<Point> points = new ArrayList<Point> ();

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

        }
    }
     }
4

2 に答える 2

0

ここで指摘しなければならないことが少なくとも 3 つあります。

1つ目は「クローン」と「コピー」の違いです。.clone は浅いコピーです。つまり、クローンを変更すると、オリジナルも変更されます。

2 つ目は、コレクションの使用に関するものです。ポリゴンは点の集合なので、配列でもArrayListでもどちらでもいいのですが、わざわざ使っても意味がないし、あるのならそれでいいのかじっくり考えて説明で説明してください。なぜそれが重要なのかをインライン ドキュメントで説明します。

3 つ目はスコープです。ポリゴン クラスにインスタンス変数 (xSum および ySum) があり、getVertexAverage で同じ名前の変数によって隠されています。getVertexAverage での使用方法自体は適切です。インスタンス変数は、ポイントを変更するすべての操作がインスタンス値を無効にするため、合計をキャッシュする場合にのみ役立ちます。これはより疑わしいものになります。

インスタンス値は、オブジェクトに関するデータを格納するためのものです (この場合、ポイントは妥当です)。インスタンス メソッドは、特定の状態 (この場合は平均) でそのデータを操作するためのものです。

これを念頭に置いて、移動メソッドがどのように終了していないかを理解できるようになりました:)

于 2013-04-17T17:29:34.323 に答える
0

この場合、追加の List オブジェクトは必要ないと思います。それらは冗長です。pList 配列を使用したコードを次に示します。

public class PolygonImpl implements Polygon {

private double xSum=0;
private double ySum=0;
private Point[] pList;
private Point a;

    PolygonImpl(Point[] pList) {

        this.pList = pList.clone();

        for (int index = 0; index < pList.length; index++) {
           xSum += pList[index].getX();
           ySum += pList[index].getY();
        }
    }



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

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

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

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


    }

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

        }
    }
于 2013-04-17T17:12:47.883 に答える