[英]program allows you specify points in N-dimensional space
所以這是我遇到困難的任務的要點:
編寫一個名為Point的java類來表示一個N維點(坐標為double)
跟蹤每個創建的點
編寫一個名為Line的java類來表示一行(帶有起點和終點)
所以我創建了我的線和點類,但我不確定我是否正確創建了我的線或點類。 我已經將主要方法設計為從控制台獲取輸入並將該輸入存儲到兩個不同的坐標數組中。 當我嘗試在我的主要調用此方法來打印坐標和距離時,我得到了輸出:
(4.0,5.0)(2.0,3.0)(0.0,0.0)之間的距離是>
所以我希望我可以幫助澄清我是否正在創建我的構造函數和其他所有內容。 到目前為止,這是我的兩個類的代碼:
public class Point{
private double[] coordinate1;
private double[] coordinate2;
public Point(double[] array1, double[] array2){
this.coordinate1 = array1;
this.coordinate2 = array2;
}
//method to get array inputs and copy them
public void copy(double[] points1, double[] points2){
double[] coordinate1 = new double[points1.length +1];
for(int copyIndex = 0; copyIndex < points1.length; copyIndex++){
coordinate1[copyIndex] = points1[copyIndex];
}
points1 = coordinate1;
double[] coordinate2 = new double[points2.length +1];
for(int copyIndex = 0; copyIndex < points2.length; copyIndex++){
coordinate2[copyIndex] = points2[copyIndex];
}
points2 = coordinate2;
}
//method to print the coordinates
public void printArray(){
double array1 = coordinate1.length;
double array2 = coordinate2.length;
for (int i = 0; i < array1 && i < array2; i++) {
System.out.println("( " + coordinate1[i] + " , " + coordinate2[i] + " ) ");
}
}
}
public class Line{
private double[] coordinate1;
private double[] coordinate2;
private double distance;
public Line(double[] array1, double[] array2){
this.coordinate1 = array1;
this.coordinate2 = array2;
}
public double getLine(double[] coordinate1, double[] coordinate2){
double diffSquareSum = 0.0;
for(int i=0;i<coordinate1.length;i++) {
diffSquareSum += (coordinate1[i] - coordinate2[i]) * (coordinate1[i] - coordinate2[i]);
}
distance = Math.sqrt(diffSquareSum);
return distance;
}
public String toString(){
return "The distance is " + distance;
}
}
如果我需要澄清或擴展我的問題,請告訴我。 感謝任何幫助。 謝謝。 以下是程序運行時預期的輸出:
例:
輸入點#1尺寸#1或“隨機”或“退出”或“幫助”或空白行繼續:3.14輸入點#1尺寸#2或“隨機”或“退出”或“幫助”或空行以繼續:0輸入點#1尺寸#3或“隨機”或“退出”或“幫助”或空行以繼續:
輸入點#2尺寸#1或“隨機”或“退出”或“幫助”或空行繼續:0輸入點#2尺寸#2或“隨機”或“退出”或“幫助”或空行以繼續:hEl
輸入點#2尺寸#2或“隨機”或“退出”或“幫助”或空白行繼續:2.71輸入點#2尺寸#3或“隨機”或“退出”或“幫助”或空白行以繼續:(3.14,0.0)和(0.0,2.71)之間的距離為4.147734321289154
輸入點#1尺寸#4或“隨機”或“退出”或“幫助”或空行繼續:隨機 - > -75.1234輸入點#1尺寸#5或“隨機”或“退出”或“幫助”或空行繼續:輸入#2維#1或“隨機”或“幫助”或“退出”或空白行繼續:RA - > 38.12851983534693輸入#2維#2或“隨機”或“幫助” “或”退出“或空白行繼續:rAnD - > 74.31366638262983
輸入點#2尺寸#3或“隨機”或“幫助”或“退出”或空白行繼續:哎喲 - 您試圖創建一條具有不同尺寸點的線!
輸入#1維度#1或“隨機”或“退出”或“幫助”或空行以繼續:退出
你創建了4個點:(3.14,0.0)(0.0,2.71)( - 75.1234)(38.12851983534693,74.31366638262983)你創建了1行:(3.14,0.0)到(0.0,2.71),長度為4.147734321289154
所以它不斷要求坐標雙打並創建一條線,然后打印出程序關閉后生成的所有線條。 希望這有助於澄清作業所需的內容
目前尚不清楚為什么你的構造函數會有2個數組 - 似乎應該只有1個數組來表示所有N個可能的軸。
此外,還不清楚如何將坐標傳遞給構造函數。
像這樣使用多個參數?
public class Point {
private double[] coordinates;
public Point(double...coordinates) {
this.coordinates = coordinates;
}
}
或者像這樣使用陣列?
public class Point {
private double[] coordinates;
public Point(double[] coordinates) {
this.coordinates = coordinates;
}
}
復制方法應該有這個簽名:
Point copy();
以便您返回當前實例的副本。
Line類的要求表明構造函數需要兩個Point
所以它看起來應該類似於:
public Line(Point p1, Point p2);
以及使用p1
和p2
的方法。
您只需要一個數組來表示每個點。 數組的長度將告訴您該點的維度。 您的代碼基本上是一個硬編碼的2D點。 正確的用法應該是這樣的:
double[] coordinate1 = {0.0};// 1d coordinate
double[] coordinate2 = {0.0,1.0};// 2d coordinate
double[] coordinate3 = {0.0,1.0,2.0};// 3d coordinate
Point point1 = new Point(coordinate1);// 1d point
Point point2 = new Point(coordinate2);// 2d point
Point point3 = new Point(coordinate3);// 3d point
int dimension = point1.getCoordinates().length;
你的線類將有兩個Point類型的變量通過構造函數傳入。 你可以制作兩種快速方法
public boolean isSameDimension(Point p1, Point p2)
{
if(p1.getDimension() == p2.getDimension())
return true;
return false;
}
public int getDimension()//In point class
{
return getCoordinates().length;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.