vendredi 26 octobre 2018

Dice thrower with methods

I'm currently doing some exercises from my study book, and one of the tasks was to: Write a dice class "Dice" which has got a value between 1-6. There should also be a constructor that picks a random value, and a method roll() that also picks a random value. The method getValue() should also be created, which is going to be used to retrieve the value being shown. Write a test class for this program.

This is my code so far:

public class Dice {
    int value;
    int currentRoll;

    //Constructor
    public Dice() {
        this.value = 1;
        this.currentRoll = 0;
}

    public int roll() {
        Random rand = new Random();
        this.currentRoll = rand.nextInt(6) + 1;
        return this.currentRoll;
}

    public int getValue() {
        return currentRoll;

}

}

What I do not understand is:Why should you random the value both in the constructor, and in the roll () method? Also, what am I missing out on?




Aucun commentaire:

Enregistrer un commentaire