Write java code to create dice program The UML class diagram shows that the Dice
ID: 3748128 • Letter: W
Question
Write java code to create dice program
The UML class diagram shows that the Dice class has-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 “.
Dice iava.util.Random -die:int[ -random Random // Do a web search for details // and examples +Random(seed:int) +nextlnt) +nextlnt(limit:int)* +Dice()- +Dice(numDice:int)v +roll(:int -rollDie()int DiceClient // Part 2 +main(args:String[]) +getDieValues():intll +hasDoubles():boolean +toString():String»Explanation / 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.