Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need a little help starting this one, thanks . Die0: Default Constructor that in

ID: 3915974 • Letter: N

Question



need a little help starting this one, thanks

. Die0: Default Constructor that initialized both instance variables to DEFAULT_NUM_FACES. . Die(Die) Copy constructor . getFaceValue: returns the faceValue of the instance . getNumFaces: return the numFaces of the die. . setFaceValue: sets faceValue of the die to the supplied value toString: returns a string representation of the face value in the form of (faceValue) roll: Rolls the die to generate a new face value. The instance's face value will be set to this new value. You can use the random method of the Math class to accomplish this task. The random method generates a random number between 0 and 1. By multiplying this number by number of faces, casting the result to an integer and adding one to it, you will be able to generate a random number between 1 and numFaces Part 2 Instructions Create a main program that would use a pair of dice to compute the sum of three tosses of the dice Implementation Details The program should produce the results of each toss and a total at the end. For example, if the first toss generates a 5 and a 6, the next toss generates a 2 and a 1, and the third toss generates a 3 and a 6, then the program should produce to the screen as shown below. Toss a:generuted a 3) andafortotol of Toss Irgeneroted a (6) ond a (6) for e total of 12 Toss 2:genereted a (5) and a (1) fortotal of b

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Die.java
-------

public class Die {
private static final int DEFAULT_NUM_FACES = 6;
private int faceValue;
private int numFaces;

public Die() {
this.faceValue = DEFAULT_NUM_FACES;
this.numFaces = DEFAULT_NUM_FACES;
}

public Die(Die d) {
this.faceValue = d.faceValue;
this.numFaces = d.numFaces;
}

public Die(int faceValue) {
this.faceValue = faceValue;
this.numFaces = DEFAULT_NUM_FACES;
}

public Die(int numFaces, int faceValue) {
this.numFaces = numFaces;
this.faceValue = faceValue;
}

public int getFaceValue() {
return faceValue;
}

public int getNumFaces() {
return numFaces;
}

public void roll() {
faceValue =(int)( Math.random() * numFaces + 1);
}

public void setFaceValue(int val) {
faceValue = val;
}

public String toString() {
return "(" + faceValue + ")";
}
}


TestDie.java
-------


public class TestDie {

public static void main(String[] args) {
Die d1 = new Die();
Die d2 = new Die();
int total = 0;
int subTotal = 0;

for(int i = 0; i < 3; i++) // 3 tosses
{
d1.roll();
d2.roll();
subTotal = d1.getFaceValue() + d2.getFaceValue();

System.out.println("Toss " + i + ": generate a " + d1 + " and a " + d2 + " for a total of " + subTotal);
total += subTotal;
}

System.out.println("Total of the 3 tosses is: " + total);

}

}

output
----
Toss 0: generate a (3) and a (4) for a total of 7
Toss 1: generate a (4) and a (5) for a total of 9
Toss 2: generate a (4) and a (2) for a total of 6
Total of the 3 tosses is: 22