Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

RiggedDice

Related Links:
Author: Marty Stepp (on 2012/03/21)

You have been asked to extend an existing class Dice representing a set of many 6-sided dice that can be rolled by a player. The Dice class includes the following members:

Member Description
public Dice(int count) constructs a dice roller to roll the given # of dice; all dice initially have the value of 6
public int count() returns the number of dice managed by this dice roller, as passed to the constructor
public int getValue(int index) returns the die value (1-6) at the given 0-based index
public void roll(int index) rolls the given die to give it a new random value from 1-6
public int total() returns the sum of all current dice values in this dice roller
public String toString() returns a string representation of this roller's dice values, such as "[4, 1, 6, 5]"

Define a new class called RiggedDice that extends Dice through inheritance. Your class represents dice that let a player "cheat" by ensuring that they will always roll a value that is greater than or equal to a given minimum value. You should provide the same methods as the superclass, as well as the following new behavior.

Member Description
public RiggedDice(int count, int min) constructs a rigged dice roller to roll the given number of dice, using the given minimum value for all future rolls; all dice initially have the value 6 (if the min value is not between 1-6, throw an IllegalArgumentException)
public int getMin() returns the minimum roll value as passed to the constructor

A RiggedDice should behave like a Dice object except for the following differences. You may need to override or replace existing behavior in order to implement these changes.

  • Every time a die is rolled, you must ensure that the value rolled is greater than or equal to the minimum value passed to your constructor. Do this by re-rolling the die if the value is too small, as many times as necessary.
  • The rigged dice should return a total that lies and claims to be 1 higher than the actual total. For example, if the sum of the values on the dice add up to 13, your rigged dice object's total returned should be 14.
  • When a rigged dice object is printed or used as a string, it should display that the dice are rigged, then the dice values, then the minimum dice value, such as, "rigged [4, 3, 6, 5] min 2"

Also make RiggedDice objects comparable to each other using the Comparable interface. RiggedDice are compared by total dice value in ascending order, breaking ties by minimum roll value in ascending order. In other words, a RiggedDice with a lower total dice value is considered to be "less than" one with a higher total. If two objects have the same total, the one with a lower min value passed to its constructor is "less than" one with a higher course count. If the two objects have the same total and the same min, they are considered to be "equal."

You should not only implement the correct behavior, but also appropriately utilize the behavior you have inherited from the superclass rather than re-implementing behavior that already works properly in the superclass. You may assume that the superclass already checks all arguments passed to its constructor and methods to make sure that they are valid.

Type your solution here:


This is an inheritance problem. Write a Java class using inheritance. (You do not need to write any import statements.)

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.