2017-01-21 4 views
0

私は、2つのデカルト点の間の距離を求める "CartesianPoint"クラスのメソッドを書いています。私がこれを呼び出すたびに、私が使用するポイントに関係なく、印刷される距離は常にゼロです。私は、距離を見つけるために作成する新しいポイントは、何とか私のインスタンス変数をポイントでオーバーライドしていると信じていますが、これを正しくコーディングする方法はわかりません。ここでJava:2点間の距離が常にゼロを返す

はCartesianPointクラスである:ここでは

public class CartesianPoint implements Point { 
    private static double x; 
    private static double y; 

    public CartesianPoint(double xCoord, double yCoord){ 
     x = xCoord; 
     y = yCoord; 
    } 

    public double xCoordinate(){ 
     return x; 
    } 

    public double yCoordinate(){ 
     return y; 
    } 

    public double radius(){ 
     double radius = Math.sqrt(Math.pow(xCoordinate(), 2)+Math.pow(yCoordinate(), 2)); 
     return radius; 
    } 

    public double angle(){ 
     double angle = Math.acos(xCoordinate()/radius()); 
     return angle; 
    } 

    public double distanceFrom(Point other){ 
     //System.out.println("x coordinate of this: " + xCoordinate()); 
     //System.out.println("x coordinate of other: " + other.xCoordinate()); 
     double xDistance = x - other.xCoordinate(); 
     double yDistance = y - other.yCoordinate(); 
     double distance = Math.sqrt(Math.pow(xDistance, 2) -  Math.pow(yDistance, 2)); 
     return distance; 
    } 

//not currently being used 
    public Point rotate90(){ 
     Point rotatedPoint = new CartesianPoint(0, 0); 
     return rotatedPoint; 
    } 
} 

は、私のテスタークラスのメソッド呼び出しです:

public class tester{ 
    public static void main(String[] args){ 
    Point p = new CartesianPoint(3, 4); 
    Point a = new CartesianPoint(6, 7); 
    System.out.println("Cartesian: (" + p.xCoordinate() + ", " + p.yCoordinate() + ")"); 
    System.out.println("Polar: (" + p.radius() + ", " + p.angle() + ")"); 
    System.out.println("Distance: " + p.distanceFrom(a)); 
    } 
} 

そして、これは私が取得しています出力されます:

Cartesian: (6.0, 7.0) 
Polar: (9.219544457292887, 0.8621700546672264) 
Distance: 0.0 

明確にするために、直交座標系と極座標は、現在行っているように、 'a'ではなく 'p'の座標を出力する必要があります。新しいポイントが作成されるたびに、最後のポイントの座標が上書きされるようです。

これに関するお手伝いをしております。

+0

xとyからstaticキーワードを削除します。 –

答えて

2

をCartesianPointのプロパティを宣言する前にstaticキーワードを削除します。

private double x; 
private double y; 

その後、あなたは右のプロパティにアクセスしていることを確認しますクラスの各インスタンス(プロパティをカプセル化する)に渡します。 X A -

また、あなたは2点間の距離を取得するために使用している数式が間違っている式はのsqrt((X Bであるとして、それは

double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2)); 

されている必要があります) +(Y B - Y ))、正しい方法は次のようになります

public double distanceFrom(Point other){ 
    //System.out.println("x coordinate of this: " + xCoordinate()); 
    //System.out.println("x coordinate of other: " + other.xCoordinate()); 
    double xDistance = x - other.xCoordinate(); 
    double yDistance = y - other.yCoordinate(); 
    double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2)); 
    return distance; 
} 
0

ヒント:距離の計算式を確認してください(例えばhere参照)、あなたはここに書かれたものと比較:

Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2)); 

あなたは違いを参照していますか?

ヒント#2:マイナス?

  • は、あなたが慎重に
  • 書かれた要件を確認しているものを読む:あなたが正しく動作しないいくつかのコードを書くときに


    は、それが支払います。

  • ドメイン知識を確認します。この場合の「数学」を