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,

Thw language is java

Homework Answers

Answer #1
package com.dungeons.dragons;

import java.util.ArrayList;

class Die {
    private static int[] AVAILABLE_SIDES = {4, 6, 8, 10, 12, 20, 100};
    private int faces;
    private int faceValue;

    Die(){
        //randomly select number of faces in the die from allowed list of numbers;
        faces = AVAILABLE_SIDES[(int)(Math.random()*7)];
        //set default faceValue to be equal to 1
        faceValue = 1;
    }

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

    public int getFaceValue() {
        return faceValue;
    }

    public int getNumFaces(){
        return faces;
    }

    //overriding toString method to return string representation of instance of class
    @Override
    public String toString() {
        return "d" + faces + " = " + faceValue;
    }
}

public class Main {
    public static void main(String... args){
        ArrayList<Die> dice = new ArrayList<>();
        StringBuilder result = new StringBuilder();
        int i;
        for(i=0; i<5; i++){
            dice.add(new Die());
        }

        for(i=0; i<dice.size(); i++){
            dice.get(i).roll();
            if(i != dice.size()-1)
                result.append(dice.get(i).toString()+ ", ");
            else
                result.append(dice.get(i).toString());
        }

        System.out.println(result.toString());

    }
}

ScreenShots

Output

Test 1:

Test 2:

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
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...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
There are four dice with varying numbers on each face: Die A. has 1 2 4...
There are four dice with varying numbers on each face: Die A. has 1 2 4 5 6 7 Die B. has 4 4 4 4 4 4 Die C. has 2 6 2 6 2 2 Die D. has 3 5 3 5 3 5 You and three friends play a game where you each roll one of the dice and the highest number wins. You get first pick from the dice. Question: Which die should you choose in...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT