简体   繁体   中英

Returning a value to another class and storing

Suppose given a class Die and it contains a random value for a six sided die.

Another class PairOfDice needs to access getvalue in Die and store two die values.

An error: cannot find symbol occurs when PairOfDice is executed.

How can this problem be fixed? And are there any other suggestions for the java code?

public class Die {
public static Random rand = new Random();
private int sides; // Number of sides
private int value; // Die's value
public Die() {
    sides = 6;
    roll();
}
public void roll() {
    value = rand.nextInt(sides) + 1;
}
public int getSides() {
    return sides;
}
public int getValue() {
    return value;
}

The second class given is:

public class PairOfDice {
private int dieOne;
private int dieTwo;
public void main(String[] args){
    Die die;
    die = new Die();
}
private void dieOne(int value){
    dieOne = die.getValue();
}
private void dieTwo(int value){
    dieTwo = die.getValue();
}
public int getDieOneValue(){
    return dieOne;
}
public int getDieTwoValue(){
    return dieTwo;
}
}

This quest should be generalized: I wrote the Die class with two public constructors. If the constructor is without the parameter, default size of die is six, else you can have any number of sides. Then, I wrote the Dices class with two constructors. First one have the number of dices (with 6 sides), and second one have the List of dices with preferred sides. If you want to learn how to generalize the problem (any problem) you can check my code. (Of course, it can be done more efficiently and with more elegance, but here is the simple code):

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Die {
    private Random RAND = new Random();
    private int noOfSides;
    private int value;

    // Default constructor without the parameter of sides gives the six sized die
    public Die() {
        this.noOfSides = 6;
    }

    // The constructor WITH number of sides
    public Die(int noOfSides) {
        this.noOfSides = noOfSides;
    }

    // rolling the die
    public void roll() {
        this.value = RAND.nextInt(noOfSides) + 1;
    }

    public int getValue() {
        if (value == 0) roll(); // if the die is never rolled -> roll it!
        // else return the rolled value
        return value;
    }

    // just for curiosities
    public int getNoOfSides() {
        return noOfSides;
    }

    public String toString() {
        return "Die has the " + noOfSides + " sides, and the last roll value was " + getValue();
    }
}

class Dices {

    private int noOfDices;
    private List<Die> myDices = new ArrayList<Die>();

    // NO constructor without the number of dices
    private Dices() {
    }

    public Dices(int noOfDices) {
        this.noOfDices = noOfDices;

        // example is for 6 sided dices
        for (int i = 0; i < noOfDices; i++) {
            getMyDices().add(new Die());
        }
    }

    // example with the list of dices with predefined sizes
    public Dices(List<Die> myDices){
        this.myDices = myDices;
    }

    public List<Die> getMyDices() {
        return myDices;
    }

    public String toString() {
        String s = "";
        for (Die die : getMyDices()) {
            s = s + die + "\n";
        }
        return s;
    }
}

public class Answer {

    public static void main(String[] args) {

        //test with two dices (6 size):
        Dices twoDices = new Dices(2);
        System.out.println(twoDices);

        //test with 4 dices size 3, 7, 9, 22
        Dices fourDices = new Dices
                (List.of(new Die(3),
                        new Die(7),
                        new Die(9),
                        new Die(22)));
        System.out.println(fourDices);
    }
}

You can see, if the die is never rolled, getValue first roll the die, then return the value. Otherwise you can roll the die and the value will be stored into the private field value...

We will presume that the Die class works as expected.

So, one could think of these changes to PairOfDice that likely resolve the immediate issue.

public class PairOfDice {
  private Die dieOne = new Die();
  private Die dieTwo = new Die();

  public static void main(String[] args) {
    PairOfDice pair = new PairOfDice();
    System.out.printf("Pair: %d - %d%n", pair.getDieOneValue(), pair.getDieTwoValue());
  }

  public int getDieOneValue() {
     dieOne.roll();
    return dieOne.getValue();
  }

  public int getDieTwoValue() {
    dieTwo.roll();
    return dieTwo.getValue();
  }
}

The PairOfDice class should hold references to two dice (in the private Die instance variables).

The getDieXValue() methods work with the instance variables, and return a value, after generating a roll() .

Now, the question is whether the requirement is to store the values of two dice, or just access to the ability to get the values. If truly the need is to store the values, then one could do:

public class PairOfDice {
  private int dieOneValue;
  private int dieTwoValue;

  public PairOfDice {
    Die die = new Die();

    // get a value for the first die
    die.roll();
    dieOneValue = die.getValue();

    // get a value for the 2nd die
    die.roll();
    dieTwoValue = die.getValue();
  }

  public int getDieOneValue() {
    return dieOneValue;
  }

  ...

Personally, if one is going to create objects, then store and use the objects.

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