Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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): image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions

Question

What is the benefit of using ROC curve?

Answered: 1 week ago