Question
Write a JAVA program to simulate a Die game played between the computer and the user. The Die class code is given as below: import
Write a JAVA program to simulate a Die game played between the computer and the user.
The Die class code is given as below:
import java.util.Random; /** The Die class simulates a six-sided die. */ public class Die { private int sides; // Number of sides private int value; // The die's value /** The constructor performs an initial roll of the die. The number of sides for the die is passed as an argument. @param numSides The number of sides for the die. */ public Die(int numSides) { sides = numSides; roll(); } /** The roll method simlates the rolling of the die. */ public void roll() { // Create a Random object. Random rand = new Random(); // Get a random value for the die. value = rand.nextInt(sides) + 1; } /** The getSides method returns the number of sides for the die. @return The number of sides for the die. */ public int getSides() { return sides; } /** The getValue method returns the value of the die. @return The value of the die. */ public int getValue() { return value; } }
- Create a class diagram for the Die class based on the code.
- Now, write a demo class that creates two instances of the Die class (each a 6-sided die), one for the computer and one for the user. (Two-player game)
- In the Demo class, the program should ask the user the number of times the die will be rolled.
o A for loop will iterate for the given number by the user. In each iteration, the die will be rolled for both user and computer. The die with the highest value wins in that specific iteration. In case of a tie, there is no winner for that particular roll of the dice.
o As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user, based on the highest number of wins for the user and the computer.
*Include comments in your code explaining what tasks are performed by different classes, methods, and statements of your code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started