我正在尝试编写一个简单的代码来计算两个机器人之间的曼哈顿距离。曼哈顿距离就是|x1-x2| + |y1-y2|。我已经编写了大部分代码,但不确定如何访问我创建的第二个机器人的 x、y 坐标
/**
* Project 1 -- Robot Position Calculator
*
* This program creates two Robot objects and
* calculates the distance between them.
* @author your name
* @lab section number and lab instructor's name
* @date date of completion
*/
import java.util.Scanner;
/**
* The name of each robot is provided as input from the user.
* The position of each robot is assigned randomly through the constructor.
* The method distance returns the distance between this robot and the other robot.
*/
public class Robot {
/**
* The name of the Robot.
*/
String name;
/**
* The x-coordinate of the Robot location.
*/
double x;
/**
* The y-coordinate of the Robot location.
*/
double y;
/**
* Constructor to assign values for instance variables
* name assigned using the passed argument. Member variables
* x and y are assigned random real values in range [0, 1).
*
* @param name the robot name
*/
public Robot(String name) {
// TODO assign this.name
this.name = name;
// TODO assign this.x and this.y using separate calls to Math.random()
x = Math.random();
y = Math.random();
}
/*
* Returns the robot name.
*
* @returns a string containing no whitespace
*/
public String getName() {
return this.name;
}
/*
* Returns the x-coordinate of the robot location
*
* @returns a real value in range [0, 1)
*/
public double getX() {
return this.x;
}
/*
* Returns the y-coordinate of the robot location
*
* @returns a real value in range [0, 1)
*/
public double getY() {
return this.y;
}
/*
* Calculate the Manhattan distance between the robot's location
* and the location specified by coordinates (x, y), i.e.:
*
* @param xCoord a real value for x-coordinate
* @param yCoord a real value for y-coordinate
* @returns a real value representing the distance
*/
public double distance(double xCoord, double yCoord) {
System.out.println(x);
System.out.println(y);
double distance = Math.abs(x - this.getX()) + Math.abs(y - this.getY());
return distance;
}
/**
* main() Method
* The main method must do the following:
* Input Name for robOne
* Input Name for robTwo
* Create the robOne object
* Create the robTwo object
* Display position of robOne
* Display position of robTwo
* Calculate the distance between both robots by calling distance function
* Display distance between the robots
*
* @param args can be ignored.
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Insert your name for the first Robot below...");
String a = in.nextLine();
Robot x = new Robot(a);
System.out.println("Insert your name for the second Robot below...");
String b = in.nextLine();
Robot y = new Robot(b);
System.out.println(x.getName() + ": (" + x.getX() + ", " + x.getY() + ")");
System.out.println(y.getName() + ": (" + y.getX() + ", " + y.getY() + ")");
// TODO Call distance(double xCoord, double yCoord) method of robOne
double d = x.distance(y.getX(), y.getY());
// TODO Print out the Manhattan distance between the robots in line 3
System.out.println(d);
// Note: be sure to adhere to the output format described below
}
}
最佳答案
这将解决您的问题:
public double distance(final double xCoord, final double yCoord) {
System.out.println(x);
System.out.println(y);
final double distance = Math.abs(x - xCoord) + Math.abs(y - yCoord);
return distance;
}
之前,你在计算你自己的物体之间的距离,它应该一直为 0。
关于java - 机器人方法如何访问类中的两个不同对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25144294/