1

I have a method in a class that sets "Position X" and "Position Y".

public class Player {

    int positionX, postinionY;

    public void setPosition (int position_X, int position_Y)
    {
         positionX = position_X;
         postinionY = position_Y;
    }

}

I have another class "World" that has an array in it called grid. I have to use the values of position x and y from Player to get location from the grid.

public class World extends Player {
    int [] [] grid = new int [16] [16];

    public int getLocationID (int x, int y)
    {
        return grid [x][y];
    }
}

I need help to take Position x,y(Player) and use them instead x,y (World)

1
  • What do you mean? And what is the logic behind World inheriting Player?? Commented Mar 15, 2012 at 11:57

2 Answers 2

5

You could add another method to World get the own (the players) location id:

public int getPlayerLocationID() {
    return grid[positionX][positionY];
}

But - honestly - in what kind of universum we have a relation "a world is-a player"? In all universums that I know (Ok, only one, actually), we usually have "a world has-many players".

Sign up to request clarification or add additional context in comments.

Comments

0

This method should do what you want.

public class World extends Player {
    int [] [] grid = new int [16] [16];
    public int getPlayerID (int locationX, int locationY)
    {
        if((locationX>15)||(locationX>16))
            throw new InvalidArgumentException("Player locations should be less than 16!");
        return grid [locationX][locationY];
    }
}

Note that it will throw an exception in case the arguments provided are not valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.