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.
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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps