Write a class Die representing a single die. Your class should include: An instance variable for the number of sides on the die. A constructor that allows you to specify the number of sides on the die. This method should check that there are at least 2 sides; if not, it should default to having 6 sides. A toString method that returns a description of the die, including its number of sides. A roll method that randomly returns one of the numbers on the die. Each number should have an equal probability of being returned. Assume that the numbers on the die range from 1 to the number of sides. A testDie method that takes one parameter for the number of times to roll the die. This method should roll the die that number of times, keeping track of the results. Once all rolls are complete, the method should display statistics on how many times each number appeared, and the corresponding percentage. Example: Die myD4 = new Die(4);  myD4.testDie(100000); might result in something like: Results from rolling 4-sided die 100000 times: qty of 1's: 25184 (25.184%) qty of 2's: 24758 (24.758%) qty of 3's: 25033 (25.033%) qty of 4's: 25025 (25.025%) Write a subclass of Die named GoodLoadedDie. This subclass behaves like a regular Die, except it tends to make good rolls. GoodLoadedDie should override Die’s roll method to only return values in the upper half of the usual range (with equal probability for each value). For example, rolling a loaded 6-sided die would result in only 4, 5, or 6. If the die has an odd number of sides, round up on the number of values returned. For example, rolling a loaded 5-sided die would result in only 3, 4, or 5. Override Die’s toString to indicate that this is a (good) loaded die. Use super whenever possible! Write a subclass of Die named BadLoadedDie. This subclass behaves like a regular Die, except it tends to make bad rolls. BadLoadedDie should override Die’s roll method to return values over the entire range, but with higher probabilities for lower values. In particular, make it where rolling a last second number is twice as likely as rolling a last number, rolling a last third number is three times as likely as rolling a last number, rolling a last forth number is four times as likely as rolling a last number, and so on. For example, rolling a 4-sided BadLoadedDie should have the following probabilities for values returned: 1 0.4 2 0.3 3 0.2 4 0.1 Write a client program that creates at least one object from each class and calls its testDie method to ensure that your dice are operating correctly. Be sure to test both even and odd numbers of sides! Note: to set up the BadLoadedDie class is an optional.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a class Die representing a single die. Your class should include:

  • An instance variable for the number of sides on the die.
  • constructor that allows you to specify the number of sides on the die. This method should
    check that there are at least 2 sides; if not, it should default to having 6 sides.
  • toString method that returns a description of the die, including its number of sides.
  • roll method that randomly returns one of the numbers on the die. Each number should have an equal probability
    of being returned. Assume that the numbers on the die range from 1 to the number of sides.
  • testDie method that takes one parameter for the number of times to roll the die. This method should roll the die
    that number of times, keeping track of the results. Once all rolls are complete, the method should display statistics
    on how many times each number appeared, and the corresponding percentage.

Example:

Die myD4 = new Die(4);
 myD4.testDie(100000);
might result in something like:

Results from rolling 4-sided die 100000 times:
qty of 1's: 25184 (25.184%)
qty of 2's: 24758 (24.758%)
qty of 3's: 25033 (25.033%)
qty of 4's: 25025 (25.025%)

Write a subclass of Die named GoodLoadedDie. This subclass behaves like a regular Die, except it tends to make good rolls.

  • GoodLoadedDie should override Die’s roll method to only return values in the upper half
    of the usual range (with equal probability for each value).
    For example, rolling a loaded 6-sided die would result in only 4, 5, or 6.
    If the die has an odd number of sides, round up on the number of values returned.
    For example, rolling a loaded 5-sided die would result in only 3, 4, or 5.
  • Override Die’s toString to indicate that this is a (good) loaded die.
  • Use super whenever possible!

Write a subclass of Die named BadLoadedDie. This subclass behaves like a regular Die, except it tends to make bad rolls.
BadLoadedDie should override Die’s roll method to return values over the entire range, but with higher probabilities for lower values.
In particular, make it where rolling a last second number is twice as likely as rolling a last number, rolling a last third number is three times
as likely as rolling a last number, rolling a last forth number is four times as likely as rolling a last number, and so on. For example,
rolling a 4-sided BadLoadedDie should have the following probabilities for values returned:

1 0.4
2 0.3
3 0.2
4 0.1

Write a client program that creates at least one object from each class and calls its testDie method to ensure that your dice are operating correctly.
Be sure to test both even and odd numbers of sides!

Note: to set up the BadLoadedDie class is an optional.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY