For this assignment, design and implement a class to represent a
die (singular of "dice"). A normal bag of dice for playing Dungeons
and Dragons, for example, contains dice with the following numbers
of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides
(or faces) of the die are numbered, starting with one. The current
face value of the die corresponds to the side that is currently
facing upward. By default, the face value is set to one; when the
die "comes to rest" after a random roll, the face value should
change accordingly. Create a class called Die that represents an
n-sided die, where n is specified as an integer argument to the
constructor. The class should have the following public methods:
int roll(): sets the face value of the die to a uniform random
number between 1 and the number of faces, and returns this value to
the caller. int getFaceValue(): returns the current face value of
the die int getNumFaces(): returns the number of faces of the die
String toString(): returns the string representation of the number
of faces and the current face value of the die, separated by an
equals sign; for example, "d100 = 47" is a 100-sided die whose
current face value is 47 (see below for more examples) You must
also write a short program (in a separate class with its own main()
method) which creates and randomly rolls five dice, randomly chosen
from the set [d4, d6, d8, d10, d12, d20, d100]. In other words,
from this set of seven possible die sizes, your program should
randomly select and roll five dice. You may choose more than one of
any given size, as long as the sizes are selected randomly. The
dies should be created within a loop that runs five times, with
each iteration of the loop, one Die object should be created,
initialized, and stored in an ArrayList which represents the
contents of one dice bag. Next, the program should iterate through
all the dice in the ArrayList, randomly rolling each die and then
getting its string representation. These string representations
should be concatenated (joined together), and delimited by commas
and spaces, to compose an output string whose format exactly
matches the example given below. (I recommend using a StringBuilder
for this; see the lecture notes.) Finally, the output string should
be printed to the console. For instance, your program may randomly
choose and roll 1d4 + 2d6 + 1d20 + 1d100: one four-sided die, two
six-sided dice, one 20-sided die, and one 100-sided die. If so, the
output would show the dice as follows: d4 = 3, d6 = 2, d6 = 4, d20
= 17, d100 = 42 (Note: Remember the method you used to generate
random numbers in an earlier assignment; you will find it useful in
this assignment also.) In designing your classes, remember that the
number of faces and the current face are two aspects of a die's
current state. Once an object is created to represent a given die,
and once that die is randomly rolled, this state should remain
persistent within the object until another method call changes it,
or until the object goes out of scope. Remember also that this
state should be accessible and changeable only through calls to the
object's methods. For examples of Java class definitions, see
Chapter 4 of the textbook (from this week's reading list) and the
"Object-Oriented Programming, Part Two" lecture notes.
This is in java
Code
import java.util.ArrayList;
public class Die {
private int n;
private int face;
public Die() {
face=1;
n=1;
}
public Die(int n) {
this.n = n;
face=1;
}
public int roll()
{
face=(int )(Math. random() * n + 1);
return face;
}
public int getFaceValue()
{
return face;
}
public int getNumFaces()
{
return n;
}
@Override
public String toString() {
return "d"+n+" = "+roll();
}
public static void main(String[] args)
{
ArrayList<Die>dice=new ArrayList<>();
int n;
for(int i=0;i<5;i++)
{
while(true)
{
n=(int )(Math. random() * 100 + 1);
if(n%2==0)
break;
}
dice.add(new Die(n));
}
for(int i=0;i<dice.size()-1;i++)
{
System.out.print(dice.get(i)+", ");
}
System.out.println(dice.get(dice.size()-1));
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.