Design a class that can be used to simulate rolling a
multi-faced die.
2. Write a Java class called Die in a class file called Die.java.
3. The Die class will have three private class attributes: a. The
first attribute, called numSides, is of type int and represents the
number of sides on the die.
b. The second attribute, called sumRolls, is static of type int and
will keep track of the sum of multiple die rolls by multiple
dice.
c. The third attribute, called r is of type Random and is used to
generate random integer values within your program.
4. The Die class contains a constructor, three non-static methods,
and one static method:
a. The constructor takes one argument, called sides, of type int.
The constructor should initialize the numSides attribute. If sides
is less than 2 or larger than 100, then numSides should be set to a
default value of six (6). Otherwise, numSides should be set to the
value of sides. Also, the constructor should instantiate a new
Random object and assign it to attribute r.
b. The public method called getNumSides() is an accessor for the
numSides attribute.
c. The public method called setNumSides() is an mutator for the
numSides attribute. It should have a single parameter called sides
of type int.
d. A public method called getRollSum() that is also static and is
an accessor for the sumRolls attribute.
e. A public method called rollDie() that generates and return a
random integer value between one (1) and the value of
numSides.
5. You will need to write a separate class containing a main method
to test your code. The example below is provided along with the
expected output to assist you. The provided example does NOT
sufficiently test the Die class (but, it is a start).
public class TestDie { public static void main(String[] args) { Die
d1 = new Die(6); Die d2 = new Die(10); System.out.println("Die 1
has " + d1.getNumSides() + " sides."); System.out.println("Die 2
has " + d2.getNumSides() + " sides."); d1.setNumSides(10);
System.out.print("Die 1 now has ");
System.out.println(d1.getNumSides() + " sides.");
System.out.println("Roll on Die 1: " + d1.rollDie());
System.out.println("Roll on Die 2: " + d2.rollDie());
System.out.print("The sum of the two rolls: ");
System.out.println(Die.getRollSum()); } }
Assuming that your Die class is correct, the above code should
produce the following result (Hint: the values of the rolls and sum
will vary for each run):
Die 1 has 6 sides. Die 2 has 10 sides. Die 1 now has 10 sides. Roll
on Die 1: 9 Roll on Die 2: 8 The sum of the two rolls: 17
// Screenshot of the code & output
// code to copy
Die.java
import java.util.Random;
public class Die {
int numSides;
static int sumRolls;
Random r;
// constructor
public Die(int sides) {
if(sides<2 || sides>100)
{
this.numSides=6;
}else {
this.numSides=sides;
}
this.r=new Random();
}
public int getNumSides() {
return numSides;
}
public void setNumSides(int i) {
this.numSides=i;
}
public static int getRollSum() {
return sumRolls;
}
public int rollDie() {
int rolls= (int) (Math.random() *
numSides) + 1;
sumRolls+=rolls;
return rolls;
}
}
TestDie.java
public class TestDie {
public static void main(String[] args) {
Die d1 = new Die(6);
Die d2 = new Die(10);
System.out.println("Die 1 has " +
d1.getNumSides() + " sides.");
System.out.println("Die 2 has " +
d2.getNumSides() + " sides.");
d1.setNumSides(10);
System.out.print("Die 1 now has ");
System.out.println(d1.getNumSides()
+ " sides.");
System.out.println("Roll on Die 1:
" + d1.rollDie());
System.out.println("Roll on Die 2:
" + d2.rollDie());
System.out.print("The sum of the
two rolls: ");
System.out.println(Die.getRollSum());
}
}
Get Answers For Free
Most questions answered within 1 hours.