Question: < > Dice -die:int[] -random:Random +Dice() +Dice(numDice:int) +roll():int -rollDie():int +getDieValues():int[] +hasDoubles():boolean +toString():String java.util.Random // Do a web search for details // and examples +Random(seed:int) +nextInt()
| < |
| Dice
-die:int[] -random:Random
+Dice() +Dice(numDice:int)
+roll():int -rollDie():int
+getDieValues():int[] +hasDoubles():boolean
+toString():String |
| java.util.Random
// Do a web search for details // and examples +Random(seed:int) +nextInt() +nextInt(limit:int) |
| DiceClient
// Part 2
+main(args:String[]) |
The UML class diagram below must be followed exactlyfor full marks. By doing so, you will learn good programming practices.
The UML class diagram shows that the Diceclasshas-aRandomobject as an instance variable as well as an integer array of die. The default constructor will construct a default pair of die whereas the one-argument constructor will allow the construction of any number of dice (e.g. perhaps we want to play Yahtzee which uses five dice). The principle operation is the roll() method which will roll all of the dice at once, returning their total value. The implementation of this roll() method mustuse of (i.e. call) a private method rollDie() which rolls a single die and ensures that this rolled value is between 1 and 6, inclusive. The class has two getter-type methods: (1) getDieValues() returns an array of all the individual die-values and (2) hasDoubles() returns true if there are any double-values amongst the die-values. Finally, as you will see in this course, every good class has a toString() method. The format required for the Stringreturned by this method is as follows: a space-separated list of all the individual die-values. Example: For two dice, the toString() method should return 4 6 .
Special implementation requirements:
The one-argument constructor must ensure that at least one die is constructed. Place the following code at the beginning of this constructor to enforce this requirement. We will learn about exceptions after the midterm; for now, just copy.
if (numDice < 1)
throw new IllegalArgumentException ("Put a nice msg);
Below is some sample code for constructing a random object (Hint: Also put this in your one-argument constructor).
this.random = new Random(new Date().hashCode());
This code sample calls the one-argument Randomconstructor that takes in a seed value which is used to initialize the underlying math that ultimately creates your random sequences of numbers. Oftentimes, the current time is used as the initial seed value because the current time will always be different each time that the program runs, in turn ensuring that the sequence of numbers generated are different each time. The current time is found with this code: new Date()
When rolling a die, remember that the values should be from 1 to 6, inclusive.
The getDieValues() method must return a defensive-copy of the die-values. See your notes about defensive-copies (Lecture 5)
In this first part of the assignment, you are to write the implementation code for Dice.javaas described and then test your implementation of the Diceclass with the provided JUnit test class DiceTest.
You are writing the service side. I have written client side already, as a test class. This is called Test-Driven Development.
If you have questions and cant understand the preceding problem description, you can find clues by reading the test class, to see how the class will be used and what outputs are expected.
Tip: Remember the small How-To load a JUnit testthat is posted on CULearn.
Tip: Other than perhaps temporarily commenting out portions of the test code to allow you to build your programs incrementally, never fix a bug by changing the test code. The bug will be in your source code.
Part 2: Dice Client
Once you have a fully-tested bug-free version of your Diceclass (all tests pass GREEN), you will write your own client program that uses the Diceclass. This program will be an exercise in using a class but will also teach you more about random number generation, namely that it is not perfectly random.
Refer now back to the original UML class diagram. The DiceClientclass has a single method called main().
public class DiceClient {
public static void main (String[] args) {
// You write ALL your client code here,
// including all variables
}
}
Your code must generate a statistical sample set of 2000 dice-rolls from which it will print out a histogram of the 4000 values (2000 rolls of a pair of dice = 4000 die values) as well as their average and standard deviation. Check out this URL for calculating the standard deviation: https://www.strchr.com/standard_deviation_in_one_pass
Sample output that you are to replicate in your program is shown below, to help you understand the task at hand.
The average roll was 3.48475
The standard deviation of the rolls was 1.721704805563369
The histogram of the rolls is:
1(697) :*********************************************************************
2(660) :******************************************************************
3(650) :*****************************************************************
4(671) :*******************************************************************
5(644) :****************************************************************
6(678) :*******************************************************************
| In the histogram, there are 6 lines, one line for each possible value. On each line, the value of the die is printed, followed by the total number of rolls of this value in brackets, followed by a visual representation of this total. A * should be printed for every 10 rolls. |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
