简体   繁体   中英

Robot method how to access two different objects in the class

I'm trying to write a simple code that calculates the manhattan distance between two robots. The manhattan distance is simply |x1-x2| + |y1-y2|. I have written most of the code but am not sure how to access the x,y coordinates of the second robot that I have created

/**
 * 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
    }
}

Simply do that :

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

The second robot is passed along when calling the method distance for the first robot like

firstRobot.distance(secondRobot);

Then the x and y coordinates of the second robot are accessible in the method distance as other.x and other.y .

Be also sure to check if the passed robot is really one or null . In case of passing null ,

firstRobot.distance(null);

your method will throw a NullPointerException when accessing either other.x or other.y .

This'll fix your problem:

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

Before, you were calculating the distance between your own object, and that should be 0 all the time.

Let's look at what you're doing already:

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

The problem with this code is that x and this.getX() are the same value. You are manually accessing your instance variable of x as well as using the getter method that you created to get the same exact value. Because of this, your distance method will always return zero.

What you want to do is compare your current Robot ( this ) with a different Robot. So, change your method to something like this:

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

This way, you are accessing the X and Y instance variables of your current object as well as the object that is passed to your method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM