Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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, dont 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 1Die 2Sum 4 13 17 1 17 18

5 10 15 3 19 22 4 12 16 123 145 3 8 11 213 268 314

(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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

Students also viewed these Databases questions