2016-04-28 35 views
-2

我對如何從自定義類中提取數據感到困惑。該代碼將笛卡爾座標組織在一個名爲linesegment的類中,其中幾個類CartesianCoordinate作爲其成員。我被困在試圖找到兩組笛卡爾座標之間的距離。在java中操作類

我該如何解碼linesegment類,進入cartesiancoordinate類,然後訪問單個的double值從主類打印到屏幕?

下面是我的程序中使用的三類:

主類:

public class lab3 
{ 

    public static void main(String [] args) 
    { 
     cartesiancoordinate one, two; //instantsiating one and two as type cartesiancoordiante 
     one = new cartesiancoordinate(5, 6); //putting the information for one and two into type cartesiancoordinate 
     two = new cartesiancoordinate(4.5, -6.5); 

     linesegment oneandtwo; 
     oneandtwo = new linesegment(one, two);  

     System.out.println(one.toString()); //dual X/Y statements using a toString method 
     System.out.println(two.toString());  

     System.out.println(oneandtwo.tostring()); 

     System.out.println("X for one is: " + one.getx()); //individual X/Y statements using getter methods 
     System.out.println("Y for one is: " + one.gety()); 
     System.out.println("X for two is: " + two.getx()); //individual X/Y statements using getter methods 
     System.out.println("Y for two is: " + two.gety()); 

     double tester; 
     oneandtwo.test();  
     System.out.println("The test method returned the distance between the two cartesian coordinates to be: " + tester); 
    } 
} 

的cartesiancoordinate類:

class cartesiancoordinate 
{ 
    private double xposition; 
    private double yposition; 

    public cartesiancoordinate(double x, double y) 
    { 
     this.xposition = x; 
     this.yposition = y; 
    } 

    public double getx() 
    { 
     return this.xposition; 
    } 

    public double gety() 
    { 
     return this.yposition; 
    } 

    public String toString() 
    { 
     return "(" + this.xposition + "/" + this.yposition + ")"; 
    } 
} 

的麻煩線段類:

class linesegment 
{ 
    private cartesiancoordinate startpoint, endpoint, s1, e1;  
    public cartesiancoordinate one, two; 


    public linesegment(cartesiancoordinate x, cartesiancoordinate y) 
    { 
     this.startpoint = x; 
     this.endpoint = y; 
    } 

    public cartesiancoordinate getstartpoint() 
    { 
     return this.startpoint; 
    } 

    public cartesiancoordinate getendpoint() 
    { 
     return this.endpoint; 
    } 

    public String tostring() 
    { 
     return ("The start point is " + this.startpoint + " and the end point is " + this.endpoint); 
    } 


    public double test() 
    { 

     double x1,x2,y1,y2; 
     cartesiancoordinate s1,e1; 

     getstartpoint() = s1; 
     getendpoint() = e1 ; 

     s1.getx() = x1; 
     s1.gety() = y1; 
     e1.getx() = x2; 
     e1.gety() = y2; 

     double tester; 
     tester = x1 + x2 + y1 + y2; 
     return tester; 
    } 
} 
+8

關於代碼質量的備註:閱讀關於java代碼風格指南;你的方法和類的名字很簡單......完全「錯誤」。除此之外,你的命名也是「不好的」(從無用的意義上說):一種方法應該說明它在做什麼。那麼,什麼是**測試**測試?爲什麼它會返回一個double?最後:考慮轉向JUnit單元測試;而不是從靜態主要方法手動「測試」。 – GhostCat

+1

沒有什麼特別區分你寫的類(「自定義類」)與標準庫中提供的類有關如何操作它們並從中提取數據。你的'cartesiancoordinate'類提供了提取單個座標的方法 - 使用它們。你的'linesegment'類提供了獲得代表起點和終點的'cartesiancoordinate'對象的方法 - 使用它們。 –

+0

感謝您的反饋,我剛剛閱讀了駱駝案例爲什麼在編碼方面很重要 - 我對這種風格很陌生,可以看到這方面的好處。至於當你說「使用它們」時 - 這正是我試圖解決的問題,現在它已經解決了。再次感謝你的時間。 –

回答

0

相反的:

double tester; 
oneandtwo.test(); 

你想:

double tester = oneandtwo.test(); 
+0

感謝您的幫助!我現在已經啓動並運行了代碼! –

0

這是一個問題。

getstartpoint() = s1; 
getendpoint() = e1 ; 

你的方法是return -ing值,所以如果你想將它們分配到s1e1,那麼你就可以做到這一點,像這樣

s1 = getstartpoint(); 
e1 = getendpoint(); 

這不會解決的邏輯你代碼,但它至少應該編譯。

0

linesegment類被打破:

class linesegment { 
    // a linesegment is nothing more than defined by a starting and ending point 
    private cartesiancoordinate startpoint, endpoint;  

    public linesegment(cartesiancoordinate x, cartesiancoordinate y) { 
     this.startpoint = x; 
     this.endpoint = y; 
    } 

    public cartesiancoordinate getstartpoint() { 
     return this.startpoint; 
    } 

    public cartesiancoordinate getendpoint() { 
     return this.endpoint; 
    } 

    public String toString() { 
     return ("The start point is " + this.startpoint + " and the end point is " + this.endpoint); 
    }  

    public double test() 
    { 
     double dx = endpoint.getx()-startpoint.getx(); 
     double dy = endpoint.gety()-startpoint.gety(); 
     return Math.sqrt(dx*dx+dy*dy); 
    } 
} 

現在主:

double tester = oneandtwo.test(); // get the distance 
System.out.println("Distance is "+tester); 

請使用標準的命名約定。您的課程必須拼寫CartesianCoordinates,LineSegment,Lab3。您的變量:oneAndTwo,startPoint,endPoint ...

+0

謝謝你的幫助 - 這是非常感謝,現在我可以前進!我剛剛將類/變量的所有名稱更改爲駝峯式樣式,再次感謝。 –