Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please don't forward the answer already uploaded on Chegg which I have been getting back before as well. Please modify it according to the instructions

Please don't forward the answer already uploaded on Chegg which I have been getting back before as well. Please modify it according to the instructions of the question which demands using various functions. It will be a great help. Thanks!

Write a program in Java 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

6 6

1 2

3 5

2 6

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

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions