Question

In: Computer Science

JAVA : Many games are played with dice, known as a die in the singular. A...

JAVA :

Many games are played with dice, known as a die in the singular. A die is a six-sided cube whose sides are labeled with one to six dots. The side that faces up indicates a die's face value.

Design and implement a Java class Die that represents one 6-sided die. You should be able to roll the die and discover its upper face. Thus, give the class the method int roll, which returns a random integer ranging from 1 to 6, and the method int getFaceValue, which returns the current face value of the die (last value rolled). Note a call to roll will return the same value as a subsequent call to getFaceValue.

Often, games depend on the roll of two dice. Using your class Die, design and implement a Java class TwoDice with an aggregation ("has-a") relationship to the Die class. An object of TwoDice represents two six-sided dice that are rolled together. Include at least the following TwoDice methods: int rollDice, int getFaceValue1, int getFaceValue2, int getValueOfDice (returns the sum of the two dice values), boolean isMatchingPair, boolean isSnakeEyes, andString toString.

Demonstrate your Die and TwoDice classes using the following test client:

public class Main
{
    public static void main(String[] args)
    {
        Die dieOne = new Die();
        Die dieTwo = new Die();
        Die dieThree = new Die();
        
        System.out.println("Demonstrating the Die class:");
        System.out.println("Rolling dieOne: " + dieOne.roll());
        System.out.println("Rolling dieTwo: " + dieTwo.roll());
        System.out.println("Rolling dieThree: " + dieThree.roll());
        
        TwoDice dice = new TwoDice();
        
        System.out.println("Demonstrating the TwoDice class: ");
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");
        
        dice.rollDice();
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");
        
        dice.rollDice();
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");

    }
}

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

===========================================================================

import java.util.Random;

public class Die {

    public static final int SIDES = 6;
    private int faceValue;

    public Die() {

        faceValue = 0;
    }

    public int roll() {

        Random random = new Random();
        faceValue =1 + random.nextInt(SIDES);
        return faceValue;
    }

    public int getFaceValue() {
        return faceValue;
    }


}
import java.util.Random;

public class Die {

    public static final int SIDES = 6;
    private int faceValue;

    public Die() {

        faceValue = 0;
    }

    public int roll() {

        Random random = new Random();
        faceValue =1 + random.nextInt(SIDES);
        return faceValue;
    }

    public int getFaceValue() {
        return faceValue;
    }


}
import java.util.Random;

public class Die {

    public static final int SIDES = 6;
    private int faceValue;

    public Die() {

        faceValue = 0;
    }

    public int roll() {

        Random random = new Random();
        faceValue =1 + random.nextInt(SIDES);
        return faceValue;
    }

    public int getFaceValue() {
        return faceValue;
    }


}

==================================================================

public class TwoDice {

    private Die diceOne;
    private Die diceTwo;

    public TwoDice() {
        diceOne = new Die();
        diceTwo = new Die();
    }

    public String getFaceValue1() {

        return String.valueOf(diceOne.getFaceValue());
    }

    public String getFaceValue2() {

        return String.valueOf(diceTwo.getFaceValue());
    }

    public void rollDice() {
        diceOne.roll();
        diceTwo.roll();
    }
}

===================================================================

public class Main
{
    public static void main(String[] args)
    {
        Die dieOne = new Die();
        Die dieTwo = new Die();
        Die dieThree = new Die();

        System.out.println("Demonstrating the Die class:");
        System.out.println("Rolling dieOne: " + dieOne.roll());
        System.out.println("Rolling dieTwo: " + dieTwo.roll());
        System.out.println("Rolling dieThree: " + dieThree.roll());

        TwoDice dice = new TwoDice();

        System.out.println("Demonstrating the TwoDice class: ");
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");

        dice.rollDice();
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");

        dice.rollDice();
        System.out.println("Rolling dice: [" + dice.getFaceValue1()
                + ", " + dice.getFaceValue2() + "]");

    }
}

=============================================================


Related Solutions

Many games involve the use of dice. The singular form of dice is die. A standard...
Many games involve the use of dice. The singular form of dice is die. A standard die is a cube, i.e. a polyhedron with six sides. Each side of a die has a number of dots (or pips) on it; each side of a die is unique and contains a number of pips from 1 through 6. A die is rolled or tossed such that it comes to rest with one side randomly facing up. The die is said to...
This code in java: A typical ball is circular in shape. Many games are played with...
This code in java: A typical ball is circular in shape. Many games are played with ball e.g cricket ball football etc. Impingement a scenario in which you create cricket ball and football or any other game ball. All balls differ in color, size and material
Java programming One of the most popular games of chance is a dice game known as...
Java programming One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2,3,4,5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on...
Dice Suppose that a red die and a green die are rolled and the numbers on...
Dice Suppose that a red die and a green die are rolled and the numbers on the sides that face upward are observed. (See Example 7 of this section and Example 2 of the first section.) (a) What is the probability that the numbers add up to 9? (b) What is the probability that the sum of the numbers is less than S?
1. Many games require rolling 2 dice and adding the rolls together. Fill in the table...
1. Many games require rolling 2 dice and adding the rolls together. Fill in the table below with the sum of the two die rolls. The first few cells have been completed as an example. Sum of Die Rolls First Die Roll 1 2 3 4 5 6 Second Die Roll 1 2 3 4 2 3 3 4 5 6 a. We assume die rolls are all equally likely. There are 36 possible outcomes (6x6) when we give them...
role playing games like dungeons & dragons use many different types of dice. suppose that a...
role playing games like dungeons & dragons use many different types of dice. suppose that a SIX sided die has faces marked 1,2,3,4,5,6. The intelligence of a character is determined by rolling this die twice and adding 1 to the sum of the spots. The faces are equally likely and the two rolls are independent. what is the average (mean) intelligence for such characters? how spread out are their intelligence , as measured by the standard deviation of the distribution?(round...
An experiment consists of rolling three fair dice --- a red die, a blue die, and...
An experiment consists of rolling three fair dice --- a red die, a blue die, and a white die --- and recording the number rolled on each die. Assume that the dice are fair, so that all outcomes are equally likely. (1) What probability should be assigned to each outcome? equation editorEquation Editor (2) What is the probability that the sum of the numbers rolled is 5? equation editorEquation Editor (3) What is the probability that the sum of the...
Two 6-sided dice are rolled. One die is a standard die, and the other is a...
Two 6-sided dice are rolled. One die is a standard die, and the other is a Fibonacci die with sides 1, 1, 2, 3, 5, and 8. a. What is the probability distribution of this experiment? b. What is the shape of the probability distribution? c. What is the expected value when these 2 dice are rolled?
Consider a six-sided fair dice. Complete the following tables for this die: Number on the dice...
Consider a six-sided fair dice. Complete the following tables for this die: Number on the dice Number of times it appears on the dice Relative frequency of seeing that number 1 2 3 4 5 6 Total This is the entire question, does not say how many times the dice is thrown
The average score for games played in the NFL is 22.4 and the standard deviation is...
The average score for games played in the NFL is 22.4 and the standard deviation is 9.1 points. 43 games are randomly selected. Round all answers to 4 decimal places where possible and assume a normal distribution. A. What is the distribution of ¯xx¯? ¯xx¯ ~ N(,) B. What is the distribution of ∑x∑x? ∑x∑x ~ N(,) C. P(¯xx¯ > 22.3185) = D. Find the 60th percentile for the mean score for this sample size. E. P(21.0185 < ¯xx¯ <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT