Wombat
Wombat

Reputation: 51

Java - trying to initialise an object with parameters passed in another constructor, in another class?

Very basic java knowledge at the moment, in need of some assistance in trying to create a game of Pacman.

Currently I have three classes, the player class, the dot class and the game class, all interacting to create a basic game of Pacman.

The issue I am having is this:

I need the 'initialX and initialY fields as show below (these will be user inputted coordinates);

public class Player
{
    private int x, y, collectedDots;

    public Player(int initialX, int initialY)
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
        }

} 

to pass through as my parameters within the new 'player' object within the Game Class.

public class Game
{


    public Game()
    {
        Player player = new Player();
        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }


}

It has me stumped, and I'm assuming I've either gone about this the wrong way, or I'm completely missing something.

Upvotes: 3

Views: 107

Answers (2)

Nico
Nico

Reputation: 3489

You can have variables defined in your Player class and then assign those variables to other variables in the constructor of the same class like so:

public class Player
{
    private int x, y, collectedDots;

    int initialX = 15;
    int initialY = 20;

    public Player()
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

}

Now your x is equal to 15 and y is equal to 20. But these values are all part of the Player class. However, you create an object of the Player class in the Game class and through that object you can access x and y like so:

public class Game
{


    public Game()
    {
        Player player = new Player();

        System.out.println(player.getX()); // prints out 15 in the console
        System.out.println(player.getY()); // prints out 20 in the console

        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }
}

You can of course do whatever you want with these values (like assign them to new variables in the Game class and manipulate them there). I am just printing them out so you can see that its the values from the Player class.

UPDATE

You can take user input before you create the Player object and then pass those values to the constructor of the Player class like so:

public class Game
{
    public Game()
    {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = sc.nextInt();

        Player player = new Player(i,j);

        System.out.println(player.getX()); // prints out 15 in the console
        System.out.println(player.getY()); // prints out 20 in the console

        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }
}

This is, of course, provided you keep your Player class how you posted it originally. But if you want to continue being able to change the values of x and y you can always provide Setter methods. Your class will then become:

public class Player
{
    private int x, y, collectedDots;

    public Player(int initialX, int initialY)
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public void setX(int newX)
    {
        x = newX;
    }

    public void setY(int newY)
    {
        y = newY;
    }
}

Whenever you want to then change the values of x and y from the Game class then you'd just have to do the following (just an example):

public class Game
{
    public Game()
    {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = sc.nextInt();

        Player player = new Player(i,j);

        player.setX(35);
        player.setY(48);
    }
}

Again, this is just an example. I wouldn't really be doing all of this in the constructor but just using this to explain how you can do what you want.

Upvotes: 0

Salah
Salah

Reputation: 8657

Your declared constructor take two int parameters, and you call it with out sending any parameters.

When use the below line, the compiler will look for a default constructor that takes no parameters, but it will fail since you haven't declared a default one, but you declared a constructor with two int parameters.

Player player = new Player();

With this line:

Player player = new Player(1, 2);// pass two int parameters t your defined constructor.

Upvotes: 1

Related Questions