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.
Edit* I moved the randomizer up to the constructor, leaving the roll method empty. What should I do in the roll() method when the constructor already randomizes?
This is my code so far:
public class Dice {
int value;
int currentRoll;
public Dice() {
Random rand = new Random();
this.value = 1;
this.currentRoll = rand.nextInt(6) + 1;
}
public int roll() {
Random rand = new Random();
this.currentRoll = rand.nextInt(6) + 1;
return this.currentRoll;
}
public int getValue() {
return this.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?