Skip to content

Lab 12: Math Dice Review

Math Dice is a game for learning arithmetic. Visit ThinkFun.com to learn how to play the game. The standard version of Math Dice requires 2 target dice and 3 scoring dice. In this lab, you will implement a general version of Math Dice that takes any number of dice.

Learning Objectives

After completing this lab, you should be able to:

  • Summarize course topics: inheritance, abstract classes, interfaces, collections.
  • Override common methods including compareTo(), equals(), and toString().
  • Iterate a collection of objects using an enhanced for loop (which uses an iterator).
  • Summarize course topics: enumerated types, arrays of objects, and recursion.
  • Implement a recursive method that explores all possible paths of a problem.

Provided Files

Save the following files into your src/labs/lab12 folder:

Note

Part1Test.java and Part2Test.java are the same files that run on Gradescope. So you will be able to test the entire lab on your own computer.

Part 1: Supporting Classes

  1. Make the following changes to Die.java:

    1. Implement the Comparable interface. Compare the value() of this die with the value() of the other die.

    2. Override the equals() method. Two dice are considered equal if they have the same index and they are of the same class. Hint: Use getClass().

    3. Override the toString() method. The format should be "%s with %d sides (value: %d)" using the simple name of the class, the number of sides, and the current value.

  2. Create RegularDie.java that extends Die:

    1. The class should have one attribute: private int sides;

    2. Implement a default constructor RegularDie() and a value constructor RegularDie(int sides). The default constructor should initialize sides to 6, representing a regular 6-sided die. The value constructor should throw an IllegalArgumentException if sides is not between 2 and 100 (inclusive).

    3. The value of a regular die is index + 1. (Because the index will range from 0 to sides - 1, and we want values that range from 1 to sides).

  3. Create SpecialDie.java that extends Die:

    1. The class should have one attribute: private int[] values;

    2. Implement a constructor SpecialDie(int[] values) that makes a copy of the given array.

    3. Implement another constructor SpecialDie(Collection<Integer> values) that copies values from the given collection.

    4. The number of sides is the length of the values array. The value of a special die is in the array at the current index.

  4. Run Part1Test.java and fix any bugs you find.

Part 2: Recursive Solution

  1. Make the following changes to MathDice.java:

    1. Implement the constructors. Each target die should be a RegularDie(12), and each scoring die should be a RegularDie(). Initialize the result to null.

    2. Review the toString() method. Following is an example of what the format looks like. If result is null, last two lines is omitted. Notice there are two spaces before each die value and operator.

      Target dice:
      5  1
      Scoring dice:
      3  2  4
      Result:
      MINUS  PLUS
      

    3. Implement the play() method. After rolling the dice in both arrays, play() should call the solve() method. The result array should be initialized before calling solve(). If solve() returns false, result should be reset to null.

    4. Write the base case for the solve() method (when index reaches the end of the result array). Evaluate the sequence of operators in the result, and return whether that value equals the product of the target dice.

      Note

      For this version of Math Dice, you may ignore the mathematical order of operations. Simply evaluate the result operators from left to right, as if they all have the same precedence. For example, 1 + 2 * 3 would be 9, not 7.

    5. Write the recursive case for the solve() method. Use the following for loop to iterate every value of the Operator enum. Assign the operator at the current index of the result, and call the recursive method with index + 1. If a solution is found down that path, return immediately.

      for (Operator oper : Operator.values()) {
      }
      
  2. Run Part2Test.java and fix any bugs you find.

Submission

Submit the following files to Gradescope:

  • Dice.java
  • RegularDie.java
  • SpecialDie.java
  • MathDice.java

Your submission must pass a checkstyle audit to receive any points.