机器人方法如何访问类中的两个不同对象

时间:2014-08-05 16:54:01

标签: java

我试图编写一个简单的代码来计算两个机器人之间的曼哈顿距离。曼哈顿距离只是| 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
    }
}

3 个答案:

答案 0 :(得分:4)

只需这样做:

public double distance(Robot other) {
    return Math.abs(other.x - this.x) + Math.abs(other.y - this.y); 
}

第二个机器人在为第一个机器人调用方法distance时传递,如

firstRobot.distance(secondRobot);

然后,方法distance中的other.xother.y可以访问第二个机器人的x和y坐标。

还要确保检查传递的机器人是真的还是null。如果传递null

firstRobot.distance(null);

访问NullPointerExceptionother.x时,您的方法会抛出other.y

答案 1 :(得分:4)

这将解决您的问题:

public double distance(double xCoord, double yCoord) {
     System.out.println(x);
     System.out.println(y);
     double distance = Math.abs(x - xCoord) + Math.abs(y - yCoord);
     return distance;  
}

之前,你在计算你自己的物体之间的距离,它应该一直是0。

答案 2 :(得分:3)

让我们看看你刚才在做什么:

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;  
}

此代码存在的问题是xthis.getX()的值相同。您正在手动访问x的实例变量以及使用您创建的getter方法来获得相同的精确值。因此,您的距离方法将始终返回零。

您要做的是将当前的机器人(this)与不同的机器人进行比较。因此,将您的方法更改为以下内容:

public double distance(Robot other) {
    return Math.abs(this.getX() - other.getX()) + Math.abs(this.getY() - other.getY());
}

这样,您将访问当前对象的X和Y实例变量以及传递给方法的对象。