Question 2 Diejava and DiceTesterjava 20 marks Implement a c

Question 2. (Die.java and DiceTester.java, 20 marks) Implement a class used to simulate a die (singular of
dice). Your class should have two data fields, one to store the number of sides on your die and another to store which side
(value) is currently facing up. Your class should have two constructors – one that takes zero arguments and creates a
standard 6-sided die and a second constructor that takes a single argument. The argument should allow the user to create a
die that has 4, 6, 8, 10, 12, 20 or 100 sides; if the user enters any other value then the die should default to 6 sides. Both
constructors should leave the side-up data field with a random (but valid) value. Your class should have a single accessor
(getValue()) that returns the current value facing up. Your class should have a single void mutator method (roll()) that
randomly rolls the die – make use of your number of sides data field here. Finally, add a toString() (see chapter 8!) method
to make it easy to print out the state of your die objects.


Write a complete tester program to show that your die class is fully functional. In particular you should create at least two
dice, one with each constructor (get user input for the constructor that takes an argument, don’t do any input checking).
Then roll them independently singly and using a loop. Also, since many games use more than one die show the sum of all
your dice objects. ***Write JavaDoc comments for this class.***

Creating two dice - one with 6 sides, enter number of sides for the second one: 20
Die[6 sides, value=3]
Die[20 sides, value=11]
Rolling each separately and printing the values:
Die 1 showing:6
Die 2 showing:3
Sum of the dice:9
for loop to roll the two dice 10 times and show the values and the sum:
Die 1 Die 2 Sum
4 13 17
1 17 18
5 10 15
3 19 22
4 12 16
1 2 3
1 4 5
3 8 11
2 1 3
2 6 8
3 1 4

(Extra – 5 marks) Add the following to your tester class. Create five more six-sided dice and use nested loops (for loop
and while loop?) to determine the average number of rolls it take to get a Yahtzee (all 5 dice with the same value). Keep
rolling the dice until you achieve a Yahtzee – keep track of the number of rolls it took to get it. Once you have done this,
wrap it in a loop that repeats this process 1000 times. Display the maximum, minimum and average number of rolls it
takes to get a Yahtzee. The output below should be in addition to the output shown above.

Creating five dice.
Die[6 sides, value=5]
Die[6 sides, value=4]
Die[6 sides, value=4]
Die[6 sides, value=2]
Die[6 sides, value=3]
Rolling until I find 1000 Yahtzees. Counting the number of rolls it takes to get each one.
Found 1000 Yahtzees. Max rolls to find one: 12057 min rolls: 1 avg rolls: 1297.22

Solution

DiceTester.java

public class DiceTester {

   public static void main(String[] args) {
       Die d1 = new Die(6);
       Die d2 = new Die(20);

       System.out.println(\"Die1\\tDie2\\tSum\");
       for (int i = 0; i < 10; i++) {
           d1.roll();
           d2.roll();
           System.out.println(d1.getValue() + \"\\t\" + d2.getValue() + \"\\t\"
                   + (d1.getValue() + d2.getValue()));
       }

       Die a1 = new Die(6);
       Die a2 = new Die(6);
       Die a3 = new Die(6);
       Die a4 = new Die(6);
       Die a5 = new Die(6);
       int numOfRolls = 0;
      
       int max = Integer.MIN_VALUE;
       int min = Integer.MAX_VALUE;
       for(int i=1; i<=1000 ;i++){
       int count = 0;
       while (true) {
           a1.roll();
           a2.roll();
           a3.roll();
           a4.roll();
           a5.roll();
           count++;
           if (a1.getValue() == a2.getValue()
                   && a3.getValue() == a4.getValue()
                   && a5.getValue() == a1.getValue()
                   && a1.getValue() == a4.getValue()) {
               numOfRolls= numOfRolls + count;
              
               if(max < count){
                   max = count;
               }
               if(max > count){
                   min = count;
               }
               break;
           }
       }
       }
       System.out.println(\"Max rolls to find one: \"+max);
           System.out.println(\"Min rolls to find one: \"+min);
           System.out.println(\"avg rolls:: \"+numOfRolls/(double)1000);
          
   }

}

Die.java

import java.util.Random;


public class Die {
   private int sides;
   private int value;
   Random r = new Random();
   public Die(int sides){
       this.sides =sides;
   }
   public int getSides() {
       return sides;
   }
   public void setSides(int sides) {
       this.sides = sides;
   }
   public int getValue() {
       return value;
   }
   public void roll() {
       this.value = r.nextInt(sides)+1;
   }
  
}

Output:

Die1   Die2   Sum
5   9   14
3   2   5
2   13   15
4   11   15
6   8   14
3   19   22
3   13   16
1   9   10
3   14   17
2   11   13
Max rolls to find one: 8245
Min rolls to find one: 131
avg rolls:: 1233.344

Question 2. (Die.java and DiceTester.java, 20 marks) Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to
Question 2. (Die.java and DiceTester.java, 20 marks) Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to
Question 2. (Die.java and DiceTester.java, 20 marks) Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site