Question
Write a program that uses the Die class that was presented in this chapter to play a simple dice game between the computer and the
Write a program that uses the Die class that was presented in this chapter to play a simple dice game between the computer and the user. The program should create two instances of the Die class (each a 6-sided die). One Die object is the computers die, and the other Die object is the users die. The program should have a loop that iterates 10 times. Each time the loop iterates, it should roll both dice. The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.) 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. You just need to write an application class for the program. Some running Examples (Yours could be different):
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; } }
Computer. . . .5 ser.._.... Conputer The computer is the grand winner
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