Question

For this assignment, design and implement a class to represent a die (singular of "dice"). A...

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

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
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...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
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, and100. 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...
For this assignment, we will be creating at least two classes. The first will be the...
For this assignment, we will be creating at least two classes. The first will be the Die class. The second will be the DiceTower class. The specifications for the classes are below. You will also be expected to create and deliver a test plan that demonstrates how you will prove your classes fulfill requirements and are bug-free. Use expected naming conventions and commenting. Part I: Die: Die(int sides): creates a die with sides quantity of sides with values from 1...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a...
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...
Currently stuck on this part of a project. I am struggling with creating the String for...
Currently stuck on this part of a project. I am struggling with creating the String for creating the dice values in the boxes for the "showDice" string. I have already completed the first part which was creating the class for the Die. This is Javascript. For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest...
Design an application that uses the C# random generator to randomly roll a six face dice...
Design an application that uses the C# random generator to randomly roll a six face dice ( no 1 - no 6) . If the user rolled a 3, the user gets to roll the six face die again. You must use a switch statement to write the result /s of the random dice roll/s to the screen. Hint: You should have nested switches in your program
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
I need to make some modifications to the code below. Numbers 1 through 3 are the...
I need to make some modifications to the code below. Numbers 1 through 3 are the required modifications. 1. Write a static method to have the players guess what was rolled 2. If guessed what was rolled you get 10 points 3. You win if you reach 30 points before the end of the round otherwise the person with the points wins at the end of five rounds The code is below import java.util.Scanner; public class ChoHan { public static...
Using Python: Write a program which asks for a number of dice and the number of...
Using Python: Write a program which asks for a number of dice and the number of sides on the dice (you may assume all dice have the same number of sides). Your program should then roll the dice 100 times and print the average value rolled. For example: If I choose one 6-sided die, the possible rolls are between 1 and 6. However, if I choose two 6-sided dice, the possible rolls are between 2 and 12. If I choose...
Assignment 1. Console Application IN PYTHON For this assignment you are to create a class called...
Assignment 1. Console Application IN PYTHON For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit whole number: sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6 reverseNums - This method takes as input a three digit whole number and returns the...