Lab 18: Math Dice Review¶
Math Dice is a game for learning arithmetic. Visit ThinkFun.com to learn how to play the game (see "Description"). 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(), andtoString(). - 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/lab18 folder:
- Part 1
- Part 2
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¶
-
Make the following changes to Die.java:
-
Implement the
Comparableinterface. Compare thevalue()of this die with thevalue()of the other die. -
Override the
equals()method. Two dice are considered equal if they have the sameindexand they are of the same class. Hint: UsegetClass(). -
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.- Hint: Look at the Java docs for Class and getSimpleName()
-
-
Create RegularDie.java that extends Die:
-
The class should have one attribute:
private int sides; -
Implement a default constructor
RegularDie()and a value constructorRegularDie(int sides). The default constructor should initialize sides to 6, representing a regular 6-sided die. The value constructor should throw anIllegalArgumentExceptionif sides is not between 2 and 100 (inclusive). -
The value of a regular die is
index + 1. (Because the index will range from 0 tosides - 1, and we want values that range from 1 tosides).
-
-
Create SpecialDie.java that extends Die:
-
The class should have one attribute:
private int[] values; -
Implement a constructor
SpecialDie(int[] values)that makes a copy of the given array. -
Implement another constructor
SpecialDie(Collection<Integer> values)that copies values from the given collection. -
The number of sides is the length of the
valuesarray. The value of a special die is in the array at the currentindex.
-
-
Run Part1Test.java and fix any bugs you find.
Part 2: Recursive Solution¶
-
Make the following changes to MathDice.java:
-
Implement the constructors. Each target die should be a
RegularDie(12), and each scoring die should be aRegularDie(). Initialize theresultto null. -
Review the
toString()method. Following is an example of what the format looks like. Ifresultis 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 -
Implement the
play()method. After rolling the dice in both arrays,play()should call thesolve()method. Theresultarray should be initialized before callingsolve(). Ifsolve()returns false,resultshould be reset to null. -
Write the base case for the
solve()method (whenindexreaches the end of theresultarray). Evaluate the sequence of operators in theresult, and return whether that value equals theproductof the target dice.Note
For this version of Math Dice, you may ignore the mathematical order of operations. Simply evaluate the
resultoperators from left to right, as if they all have the same precedence. For example,1 + 2 * 3would be 9, not 7. -
Write the recursive case for the
solve()method. Use the following for loop to iterate every value of theOperatorenum. Assign the operator at the current index of the result, and call the recursive method withindex + 1. If a solution is found down that path, return immediately.for (Operator oper : Operator.values()) { }
-
-
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.