カスタムクラスからデータを抽出する方法が混乱しています。コードグループデカルト座標は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;
}
}
以下
は私のプログラム内で使用される三つのクラスです
サイドノートオンコード品質:Javaコーディングスタイルガイドラインについて読む;メソッドとクラスのあなたの名前は単に...完全に「間違っている」。それを超えて、あなたの命名は「悪い」(役に立たない意味で)です。方法は、それが何をしているのかを述べるべきです。だから、**テスト**テストは何ですか?なぜ二重に戻っているのですか?最後に、JUnit単体テストに目を向けることを検討してください。静的なメインメソッドから手動の "テスト"の代わりに。 – GhostCat
特に、あなたが書き込むクラス(「カスタムクラス」)と、それらを操作してデータを抽出する方法に関して標準ライブラリで提供されるものとを区別するものは何もありません。あなたの 'cartesiancoordinate'クラスは個々の座標を抽出するメソッドを提供します。 'linesegment'クラスは、開始点と終了点を表す' cartesiancoordinate'オブジェクトを取得するためのメソッドを提供します。 –
あなたのフィードバックをありがとう、私はちょうどなぜラクダの大文字小文字がコーディングで重要なのかを読んでいます - 私はこのスタイルに新しいです、そして、これの利点を見ることができます。そして、あなたが「使用する」と言ったときには、これはまさに私が取り組もうとしていた問題であり、今解決されています。あなたの時間をもう一度ありがとう。 –