Question: Write a program that uses the Die class to play a simple dice game between the computer and the user. The program should create two

Write a program that uses the Die class 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-side 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 use.

Use following UML diagram to design your class.

Class: Die

Data Members:

-computename: String

-username: String

-sides: int

- value: int

Method Members:

+Die (sides: int)

+Die (computename: String, username: String)

+ getSide (): int

+ setSide (sides: int): void

+ getValue () : int

+ setValue(value : int) : void

+roll() : void

+getComputername (): String

+setComputername (value: String): void

+getUsername (): String

+setUsername (value: String): void

Computename is a specially formed String that consists of 3 letters (can be upper-case or lower-case), followed by a dash, followed by 4 numbers.

For example, "abc-1234".

All computer names follow this pattern, otherwise they are invalid. The username cannot be an empty string. All mutator methods should only assign the parameter value if it is valid, otherwise an IllegalArgumentException is thrown with a specific, yet concise and informative error message Roll () method simulates the rolling of the die.

Create object of Random class and assign the value=rand.nextInt(sides)+1 which return random integer in the range of 1 through 6. Call roll () method in Die class constructor. In test class declare three constants. DIE1_SIDES and DIE2_SIDES are the number of sides for the die which is 6 for both constant. MAX_ROLLs is the number of times to roll the die which is 10. Create two instances of the Die class.

Use DIE1_SIDES for first object and DIE2_SIDES for second object. Use for loop which iterate 10 times and each time call the roll () method.

Sample output:

Enter computer name: yza-3512 Enter username: Johny

This simulates the rolling of a 6-sided die and a 6-sided die Rolling the dice 10 times

Die1 Die2

3 6

5 4

6 6

6 5

1 4

4 4

Computer=8 user=2 Grand winner is computer yza-3512 Or Grand winner is user xyz Or There is tie between computer xyz-1234 and user Han.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!