[英]program allows you specify points in N-dimensional space
So here is the gist of the assignment I am having difficulty with: 所以这是我遇到困难的任务的要点:
Write a java class called Point to represent a N-Dimensional point (With coordinates that are double) 编写一个名为Point的java类来表示一个N维点(坐标为double)
Keep track of every created poin 跟踪每个创建的点
Write a java class called Line to represent a line (with a starting point and an ending point) 编写一个名为Line的java类来表示一行(带有起点和终点)
So i have created my line and point class but I am not sure if I am creating my line or point class correctly. 所以我创建了我的线和点类,但我不确定我是否正确创建了我的线或点类。 I already have my main method designed to take input from the console and store that input into two different arrays for the coordinates.
我已经将主要方法设计为从控制台获取输入并将该输入存储到两个不同的坐标数组中。 When i try to call this method in my main to print the coordinates and distance i get back the output:
当我尝试在我的主要调用此方法来打印坐标和距离时,我得到了输出:
The distance between ( 4.0 , 5.0 ) ( 2.0 , 3.0 ) ( 0.0 , 0.0 ) is> (4.0,5.0)(2.0,3.0)(0.0,0.0)之间的距离是>
So I was hoping i could get help in clarifying if I am creating my constructor and everything else correctly. 所以我希望我可以帮助澄清我是否正在创建我的构造函数和其他所有内容。 Here is my code for both classes thus far:
到目前为止,这是我的两个类的代码:
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;
}
}
Please let me know if I need to clarify or expand on my question. 如果我需要澄清或扩展我的问题,请告诉我。 Appreciate any help.
感谢任何帮助。 Thank you.
谢谢。 Here is the output expected from the program once ran:
以下是程序运行时预期的输出:
Example: 例:
Enter point # 1 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: 3.14 Enter point # 1 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: 0 Enter point # 1 dimension # 3 or "Random" or "Exit" or "Help" or blank line to proceed: 输入点#1尺寸#1或“随机”或“退出”或“帮助”或空白行继续:3.14输入点#1尺寸#2或“随机”或“退出”或“帮助”或空行以继续:0输入点#1尺寸#3或“随机”或“退出”或“帮助”或空行以继续:
Enter point # 2 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: 0 Enter point # 2 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: hEl 输入点#2尺寸#1或“随机”或“退出”或“帮助”或空行继续:0输入点#2尺寸#2或“随机”或“退出”或“帮助”或空行以继续:hEl
Enter point # 2 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: 2.71 Enter point # 2 dimension # 3 or "Random" or "Exit" or "Help" or blank line to proceed: The distance between ( 3.14 , 0.0) and ( 0.0 , 2.71) is 4.147734321289154 输入点#2尺寸#2或“随机”或“退出”或“帮助”或空白行继续:2.71输入点#2尺寸#3或“随机”或“退出”或“帮助”或空白行以继续:(3.14,0.0)和(0.0,2.71)之间的距离为4.147734321289154
Enter point # 1 dimension # 4 or "Random" or "Exit" or "Help" or blank line to proceed: Random --> -75.1234 Enter point # 1 dimension # 5 or "Random" or "Exit" or "Help" or blank line to proceed: Enter point # 2 dimension #1 or "Random" or "Help" or "Exit" or blank line to proceed: RA --> 38.12851983534693 Enter point # 2 dimension #2 or "Random" or "Help" or "Exit" or blank line to proceed: rAnD --> 74.31366638262983 输入点#1尺寸#4或“随机”或“退出”或“帮助”或空行继续:随机 - > -75.1234输入点#1尺寸#5或“随机”或“退出”或“帮助”或空行继续:输入#2维#1或“随机”或“帮助”或“退出”或空白行继续:RA - > 38.12851983534693输入#2维#2或“随机”或“帮助” “或”退出“或空白行继续:rAnD - > 74.31366638262983
Enter point # 2 dimension #3 or "Random" or "Help" or "Exit" or blank line to proceed: Ouch - You tried to create a line with points of disimilar dimension! 输入点#2尺寸#3或“随机”或“帮助”或“退出”或空白行继续:哎哟 - 您试图创建一条具有不同尺寸点的线!
Enter point # 1 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: EXIT 输入#1维度#1或“随机”或“退出”或“帮助”或空行以继续:退出
You created 4 points: ( 3.14 , 0.0 ) ( 0.0 , 2.71 ) ( -75.1234 ) ( 38.12851983534693 , 74.31366638262983 ) You created 1 lines: ( 3.14 , 0.0 ) to ( 0.0 , 2.71 ) with length 4.147734321289154 你创建了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
So it keeps asking for coordinate doubles and creates a line and then prints out all lines made once program closes. 所以它不断要求坐标双打并创建一条线,然后打印出程序关闭后生成的所有线条。 Hope that helps clarify what the assignment requires
希望这有助于澄清作业所需的内容
It's not clear why your constructor would have 2 arrays - it seems there should be only 1 array to represent all of the N possible axes. 目前尚不清楚为什么你的构造函数会有2个数组 - 似乎应该只有1个数组来表示所有N个可能的轴。
Also, it's not clear how the coordinates should be passed to the constructor. 此外,还不清楚如何将坐标传递给构造函数。
Like this using multiple arguments? 像这样使用多个参数?
public class Point {
private double[] coordinates;
public Point(double...coordinates) {
this.coordinates = coordinates;
}
}
Or like this using an array? 或者像这样使用阵列?
public class Point {
private double[] coordinates;
public Point(double[] coordinates) {
this.coordinates = coordinates;
}
}
The copy method should likely have this signature: 复制方法应该有这个签名:
Point copy();
so that you return a copy of the current instance. 以便您返回当前实例的副本。
The requirement for the Line class indicates the constructor takes two Point
s so it should look similar to this: Line类的要求表明构造函数需要两个
Point
所以它看起来应该类似于:
public Line(Point p1, Point p2);
and the methods working with p1
and p2
. 以及使用
p1
和p2
的方法。
You only need one array to represent each point. 您只需要一个数组来表示每个点。 The length of the array would tell you the dimension of that point.
数组的长度将告诉您该点的维度。 Your code is basically a hard coded 2D point.
您的代码基本上是一个硬编码的2D点。 Correct usage should look something like this:
正确的用法应该是这样的:
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;
Your line class will have two variables of type Point passed in through the constructor. 你的线类将有两个Point类型的变量通过构造函数传入。 You can make two quick methods
你可以制作两种快速方法
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.