2016-03-30 4 views
3

私は自分のクラスとメソッドをJavaで作成し始めましたので、おそらく混雑している不要なコードを赦してください。私は、次のメソッドを利用してOrderedPairというクラスを書くことになっています:reflectX、reflectY、translate、rotate90、getQuadrant、getOrigPt、dilate、およびtoString私のジオメトリプログラムに問題があります

私の先生がこのテクニックをプログラムに実装したかったので、reflectXメソッドとreflectYメソッドは意図的にオーバーロードされていました。

私の問題は、私のrotate90メソッドが機能していないことです。たとえば、ポイント(3、-8)を4回回転させると(元のポイントを返すはずです)、代わりに(-8、-8)を取得します。また、私はtoStringメソッドを印刷するとき、新しいポイントの新しい象限が間違っています。ここに私のコードです:

public class OrderedPair{ 
    private double original_x; 
    private double original_y; 
    private double x_value; 
    private double y_value; 
    private int original_quadrant; 
    private int quadrant; 

    public OrderedPair (double x, double y, int q){ 
     original_x = x; 
     original_y = y; 
     x_value = x; 
     y_value = y; 
     original_quadrant = q; 
     quadrant = q; 
    } 

    public void reflectX(){ 
     y_value = -y_value; 
    } 

    public void reflectX (double value){ 
     double reflect = value - x_value; 
     if (reflect > 0) 
      x_value += 2*reflect; 
     else 
      x_value -= 2*reflect; 
    } 

    public void reflectY(){ 
     x_value = -x_value; 
    } 

    public void reflectY (double value){ 
     double reflect = value - y_value; 
     if (reflect > 0) 
      y_value += 2*reflect; 
     else 
      x_value -= 2*reflect; 
    } 

    public void translate (double translateX, double translateY){ 
     x_value += translateX; 
     y_value += translateY; 
    } 

    public void rotate90 (int numOfRotations){ 
     for (int rotate = 1; rotate <= numOfRotations; rotate++){ 
     x_value = -y_value; 
     y_value = x_value; 
     } 
    } 

    public void dilate (double dilate_value){ 
     x_value *= dilate_value; 
     y_value *= dilate_value; 
    } 

    public int getQuadrant(){ 
     if (x_value>=0) 
     { 
      if (y_value >= 0) 
      { 
       quadrant = 1; 
       return quadrant; 
      } 
      else 
      { 
       quadrant = 4; 
       return quadrant; 
      } 
     } 
     else 
     { 
      if (y_value >= 0) 
      { 
       quadrant = 2; 
       return quadrant; 
      } 
      else 
      { 
       quadrant = 3; 
       return quadrant; 
      } 
     } 
    } 

    public String getOrigPt(){ 
     return "(" + original_x + ", " + original_y + ")"; 
    } 

    public String toString(){ 
     return "(" + original_x + ", " + original_y + "); " + original_quadrant + "; " + "(" + x_value + ", " + y_value + "); " + quadrant; 
    } 
} 

誰かが助けることができたら、それは素晴らしいでしょう! rotate90

+0

:だからあなたの結果はx_valueをキャッシュするために、ローカル変数を使用しx_value = -y_valueとy_value = -y_value

です。 'y_value'はどのような値を割り当てますか? – KevinO

+0

'public void reflectiveY(double value)'メソッドでは、else文にエラーがある可能性があります。エラーではなく、エラーです。論理エラーです。 – 3kings

+0

私にそれをつかまえてくれてありがとう! –

答えて

2

forループ本体

x_value = -y_value; 

の最初の行は、x_valueを上書きしないので、2行目にはもはや利用できます。

y_value = x_value; // x_value has already been changed 

これは効果的にy_valueをそれ自身の反対側に設定します。古い値を失わないように一時変数を使用してください。

double old_x = x_value; 
x_value = -y_value; 
y_value = old_x; 

象限の決定は正しいように見えますが、正しいx値とy値に依存しています。 rotate90メソッドを修正する場合は、getQuadrantメソッドの出力も修正する必要があります。

2
public void rotate90 (int numOfRotations){ 
    for (int rotate = 1; rotate <= numOfRotations; rotate++){ 
    x_value = -y_value; 
    y_value = x_value; 
    } 
} 

x_valueを-y_valueで上書きし、その後でy_value = x_valueを設定します。割り当ての順序を注意深く見、rotate90` `で

double x_value_cache = x_value; 
x_value = -y_value; 
y_value = x_value_cache; 
+0

ありがとう、あなたたちは正しいです。私は完全にそれを認識しませんでした。 –

関連する問題