2013-11-14 202 views
0

我想要做的是創建一個方法,可以找到兩點之間的中點。此外,我正在嘗試顯示這些中點的象限。問題在於,在將座標更改爲中點並顯示點之前,它不能正確設置象限。這裏是我的代碼:類實例調用靜態方法

public class OrderedPairTest { 


    public static void main(String[] args) { 
     OrderedPair op1 = new OrderedPair(0,0); 
     OrderedPair op2 = new OrderedPair(0,0); 
     OrderedPair op3 = new OrderedPair(0,0); 
     System.out.println(op1.a); 
     System.out.println(op1.b); 
     System.out.println(op1.setX(2)); 
     System.out.println(op1.setY(3)); 
     System.out.println(op2.moveX(-1)); 
     System.out.println(op2.moveY(-3)); 
     System.out.println(op3.moveXY(-4,4)); 
     System.out.println(op1.printOP()); 
     System.out.println(op2.printOP()); 
     System.out.println(op3.printOP()); 
     System.out.println(op1.distance(op2)); 
     System.out.println(op1.distance(op3)); 
     System.out.println("The distance between the two points is: "+op1.distancestat(op2,op3)); 
     System.out.println("The midpoint is at: "+op1.midpoint(op2)+op2.q); 
     System.out.println("The midpoint is at: "+op2.midpointstat(op1,op3)+op2.q);  
    } 
} 

public class OrderedPair { 
    int a,b; 
    public OrderedPair(int x,int y){ 
     a = x; 
     b = y; 
    } 
    String q = ""; 
    String msg = ""; 
    String error = ""; 
    String result = ""; 
    public int getX() {                     //Gets the value of X 
     result = "X is "+a+"."; 
     return a; 
    } 
    public int getY() {                     //Gets the value of Y 
     result = "Y is "+b+"."; 
     return b; 
    } 
    public String setX(int x) {                   //Sets the value of X 
     a = x; 
     result = "X has been set to "+a;   
     if (b != 0) setQ(); 
     else; 
     return result; 
    } 
    public String setY(int y) {                   //Sets the value of Y 
     b = y; 
     result = "Y has been set to "+b;   
     if (a != 0) setQ(); 
     else; 
     return result; 
    }   
    public String toString() {                   //Turns the variables into a string 
     msg = ("("+a+","+b+") Q"+q); 
     result = "Values have been converted into string."; 
     return msg;  
    } 
    public String moveX(int amt) {                   //Moves X a predefined amount of units 
     a+=amt; 
     result = "X has been moved "+amt+" units.";  
     if ((b != 0) && (a != 0)) setQ(); 
     else; 
     return result; 
    } 
    public String moveY(int amt) {                   //Moves Y a predefined amount of units 
     b+=amt; 
     result = "Y has been moved "+amt+" units."; 
     if ((b != 0) && (a != 0)) setQ(); 
     else; 
     return result;   
    } 
    public String moveXY(int amt1, int amt2){                //Moves X and Y a predefined amount of units 
     a+=amt1; 
     b+=amt2; 
     result = "X has been moved "+amt1+" units.Y has been moved "+amt2+" units."; 
     if ((b != 0) && (a != 0)) setQ(); 
     else; 
     return result; 
    } 
    public String distance(OrderedPair other) {               //Gets the distance between two pairs of choordinates 
     double d = Math.sqrt(Math.pow(other.b-this.b,2)+Math.pow(other.a-this.a,2)); 
     result = "The distance between the two points is: "+d; 
     return result; 
    } 
    public static double distancestat(OrderedPair other1, OrderedPair other2) {         //Gets the distance between two pairs of static choordinates 
     double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2)); 
     return d; 
    } 
    public OrderedPair midpoint(OrderedPair other) {               //Gets the midpoint between two pairs of choordinates 
     return new OrderedPair((this.getX()+other.getX())/2,(this.getY()+other.getY())/2);   
    } 
    public static OrderedPair midpointstat(OrderedPair other1, OrderedPair other2) {                        //Gets the midpoint between two pairs of static choordinates 
     return new OrderedPair((other1.getX()+other2.getX())/2,(other1.getY()+other2.getY())/2);   
    }  
    public String printOP() {                    //Calls toString() then prints the string set by toString() 
     toString(); 
     if (q!= "") return msg; 
     else error = "Q has not been set."; 
     return error; 
    } 
    private String setQ() {                    //Sets the quadrant based on the signs of the choordinates 
     if ((a!=0)&&(b!=0)){ 
      if (a > 0){ 
       if (b > 0) q = "I";  
       else q = "IV"; 
      } 
      else { 
       if (b > 0) q = "II"; 
       else q = "III"; 
      }  
      result = "The quadrant has been set."; 
      return result; 
     } 
     else error = "X and/or Y are zero, please assign the variables a nonzero value."; 
     return error; 
    }    
} 
+3

這聽起來像是學習使用調試器的好機會。 – NPE

+0

請僅顯示相關代碼,這太多了。你能否澄清你的標題是什麼意思? –

+0

@JeroenVannevel所有的代碼都是相關的,我包括測試類和其他類。 – Showman

回答

1
  1. 除以2,而不是2.0在中點計算是有問題的。

  2. 無論是在setX的(),SETY()或setQ(),正確地處理其中a或b等於條件爲零if a==0 or b==0 then q = ""

  3. distancestat()應該是

double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other2.a,2)); //變化是在結束 - other2.a

double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2));

相關問題