<<uses>> Dice -die:int[] -random:Random +Dice() +Dice(numDice:int) +roll():int -
ID: 3755640 • Letter: #
Question
<<uses>>
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 shows that the Diceclasshas-a random object 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 must use 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 String returned 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 “.
Refer now back to the original UML class diagram. The DiceClientclass has a single method called main().
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.
<<uses>>
Explanation / Answer
Answer:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Dice.java
import java.util.Random;
public class Dice {
// attributes
private int die[];
private Random random;
/**
* default constructor
*/
public Dice() {
die = new int[2];
random = new Random();
// rolling for the first time, uncomment it if you dont need
roll();
}
/**
* constructor with one argument
*
* @param numDice
* number of dice
*/
public Dice(int numDice) {
die = new int[numDice];
random = new Random();
// rolling for the first time
roll();
}
/**
* method to roll all dice and return the sum total
*/
public int roll() {
int total = 0;
for (int i = 0; i < die.length; i++) {
int value = rollDie();
total += value;
die[i] = value;
}
return total;
}
/**
* method to roll a single die, return a value between 1-6
*/
private int rollDie() {
// below statement will return a value between 1 and 6 [inclusive]
return random.nextInt(6) + 1;
}
/**
* method to fetch the array containing dice values
*
* @return the die array
*/
public int[] getDieValues() {
return die;
}
/**
* returns true if there are any double-values amongst the die-values. This
* is confusing when there are more than two dice. So I'm checking for
* adjacent doubles; that is this method returns true if two adjacent dice
* have the same value
*/
public boolean hasDoubles() {
for (int i = 0; i < die.length - 1; i++) {
if (die[i] == die[i + 1]) {
// this and next die has same value
return true;
}
}
return false;
}
@Override
public String toString() {
String data = "";
for (int i = 0; i < die.length; i++) {
data += die[i] + " ";
}
return data;
}
}
// DiceClient.java
public class DiceClient {
public static void main(String[] args) {
//creating and testing Dice objects
Dice dice1=new Dice();
System.out.println("Dice1: "+dice1);
System.out.println("Rolling...");
int sum=dice1.roll();
System.out.println("Sum = "+sum+", values: "+dice1);
System.out.println("Has doubles: "+dice1.hasDoubles());
Dice dice2=new Dice(5);
System.out.println("Dice2: "+dice2);
}
}
/*OUTPUT*/
Dice1: 2 4
Rolling...
Sum = 9, values: 6 3
Has doubles: false
Dice2: 5 1 2 6 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.