2016-04-28 13 views
-2

カスタムクラスからデータを抽出する方法が混乱しています。コードグループデカルト座標はlinesegmentというクラス内で座標され、クラスのいくつかのインスタンスはメンバーとしてCartesianCoordinateです。私はデカルト座標の2つのセットの間の距離を見出そうとしています。javaでクラスを操作する

cartesiancoordinateクラスにlinesegmentクラスをデコードして、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コーディングスタイルガイドラインについて読む;メソッドとクラスのあなたの名前は単に...完全に「間違っている」。それを超えて、あなたの命名は「悪い」(役に立たない意味で)です。方法は、それが何をしているのかを述べるべきです。だから、**テスト**テストは何ですか?なぜ二重に戻っているのですか?最後に、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); 

標準の命名規則を使用してください。あなたのクラスは、CartesianCoordinatesLineSegmentLab3の綴りが必要です。あなたの変数:oneAndTwostartPointendPoint ...

+0

ご協力いただきありがとうございます - それは大変感謝しており、今私は前進することができます!クラス/変数のすべての名前をラクダのスタイルに変更しました。もう一度感謝します。 –