user1098185
user1098185

Reputation: 109

Get data from another class

I'm building an application and i am trying to find a way to get data from one class to another class.

in my main class called Driver, i have a function which throws two dices and then i get a sum of them. This function is created in a class called Dices. I have a new class called Players, where i need to get the data from the dice throw from my main class.

The code is the following

public class Player {
public String spillernavn;
public int pos;



public String SpillerNavn() {
        return spillernavn;
    }
public int move(int steps){
  move=??;
  pos=steps+pos;
    return ??;
}
public int SpillerPos() {
        return pos;
    }
}

The Methode public int move(int steps){} is where i need to use the data from the dice throw and then make the addition with the pos, which stands for position.

The main function is the following

public static void main(String[] args) {

        //Intializing dices
        Dice die1 = new Dice();
        Dice die2 = new Dice();

        //Summ of dice
        int sum = die1.throwDice() + die2.throwDice();

Upvotes: 1

Views: 133

Answers (2)

Aditya Singh
Aditya Singh

Reputation: 2453

File name: Dice.java

import java.util.Random; 



public class Dice {

    private Random random;

    public Dice() {

        random = new Random();
    }  

    public int throwDice() {

        int num = 0;

        while(num == 0)  // Dice doesn't have a zero, so keep looping unless dice has a zero.
            num = random.nextInt(7);  // returns a random number between 0 - 6

        return num;
    }
}  

File name: Players.java

public class Players {

    private int sum;


    public Players(int sum) {

        this.sum = sum;  // You got the data from `Driver class` here.
        // You got the sum of two dice. Now do whatever you want to.
    }
}  

public class Driver {

    public static void main(String[] args) {

        Dice dye1 = new Dice();
        Dice dye2 = new Dice(); 
        int sum = dye1.throwDice() + dye2.throwDice();
        Players player = new Player(sum);  // Passing the data to Class Players
    }    
}  

To send data, you need to pass the argument in the ()

Upvotes: 1

Daniel Christopher
Daniel Christopher

Reputation: 99

You could do something like this.

public class Player {
public String spillernavn;
public int pos;



public String SpillerNavn() {
    return spillernavn;
}

public int move(int steps, int move){
    move=move;
    pos=steps+pos;
    return ??;
}

public int SpillerPos() {
    return pos;
}
}

Then in main:

public static void main(String[] args) {

    //Intializing dices
    Dice die1 = new Dice();
    Dice die2 = new Dice();

    //Summ of dice
    int sum = die1.throwDice() + die2.throwDice();  

    player.move(steps, move);

For future reference. You should probably create an instance of your dice in your player class. It's simple passing data between classes. Getters and setters.

Upvotes: 1

Related Questions