CMPS 134 Fall 2022
Prog. Assg. #5: Dice Game
Due: Nov. 10, 11:59:59pm


Image of a pair of dice
You have probably played board games (e.g., Parker Brothers' Monopoly) that involve rolling a pair of six-sided dice, such as shown to the right. For this assignment you are to complete the development of the Java application DiceGame, which "plays" a game in which a pair of dice is repeatedly rolled until particular conditions have been met. (The details of the game can be found below.)

Provided to you are the complete Java classes Die and PairOfDice. As their names suggest, an instance of the former class (i.e., an object created therefrom) simulates the behavior of a single six-sided die, while an instance of the latter simulates the behavior of a pair of such dice. Not surprisingly, PairOfDice is a client of Die. (Indeed, each object arising from PairOfDice has, as one might expect, two instance variables of type Die.)

Your program is to be a client of PairOfDice, which will make it, indirectly, a client of Die. In particular, this means that to successfully compile your program, you will need to place both the and files into the same folder as your source souce code file.1 Note that your solution will be tested under the assumption that it is a client of the provided versions of and Thus, if either of those classes need to be changed in order for your solution to work correctly, your solution is wrong.

To illustrate how a Java application can use an instance of the PairOfDice class to simulate rolling a pair of dice (and observing and reporting the results of doing so), you are provided with the Java application DiceApp. Use it as a model for developing your program.

Some Dice Terminology

Each roll of a six-sided die yields a result, which is an integer in the range 1..6. To distinguish it from a "plain" number, we put it inside square brackets. Thus, for example, if a die is rolled and the face with four pips ends up on top, we denote the result of the roll as [4].

When a pair of six-sided dice is rolled, the result is the combination of the results of the rolls of each individual die. For example, [[3][5]] is how we express the result when the first die in the pair had [3] as its result and the second die had [5] as its result. (Note that here we are assuming that, in a pair of dice, one of them is identified as being the "first" die and the other is identified as being the "second".)

Often times when a pair of dice is rolled, we are mostly concerned with the result-sum, i.e., the sum of the results of the two dice. For example, the result [[6][2]] gives rise to a result-sum of 8.

Suppose that [[i][j]] and [[k][m]] are two results of rolling a pair of dice. We say that they are identical results if both i=k and j=m. We say that they are equivalent results if either they are identical or both i=m and j=k). For example, the results [[3][5]] and [[5][3]] are equivalent but not identical.


Your program's intended behavior is to play a rather silly dice game. The purpose of developing it is to give you practice in how to create and make use of (software) objects and to how to use loops and conditional (i.e., if-else) statements to express procedural logic.

Here's how the game is played:

As an example, suppose that on the fourth dice roll, the result is [[6][5]] (yielding a result-sum of 11), after none of the first three rolls had a result-sum of 7 or 11. Suppose that the first time a 7 result-sum occurs is on the 10th roll. Suppose, further, that the result of that roll was [2][5]. At that point, the first phase of the game comes to an end, as both 7 and 11 have occurred as result-sums. The second one to have occurred, [[2][5]], must be remembered.

The second phase is to roll the dice until a result equivalent to [[2][5]] occurs. Suppose that that happens after 22 more rolls, when a [[5][2]] is rolled. Then the game would have ended after a total of 32 dice rolls.

As output, your program should report on which dice roll the first 7 or 11 was achieved, and the result of that roll. Next it should report on which dice roll the first phase ended, together with the result of that roll. Finally, it should report on which roll the game ended, and the result of that roll.

For example, if the game described above were to take place, the corresponding output would be like this:

Dice roll 4: [[6][5]]
Dice roll 10: [[2][5]]
Dice roll 32: [[5][2]]

Note that the strings "[[6][5]]", "[[2][5]]", and "[[5][2]]" are exactly of the form produced by the toString() method of the PairOfDice class.

Also note that, in our example game, a result-sum of 11 may have occurred one or more times among rolls 5..9, but none of those events is reported. That is, during Phase 1 of the game, only the first occurrence of each of 7 and 11 should be reported.

As usual, it is expected that your code will be properly formatted, well-commented, and show signs that procedural decomposition was used in its design. Deficiencies in any of these aspects may result in significant point deductions.

Java Loop Constructs

The do-while loop. You may find it convenient to employ a do-while loop in your application, particularly for carrying out the second phase of the game. Java has three varieties of loops, of which we have covered two, the for loop and the while loop. Their syntaxes are as follows:

for loop while loop do-while loop
for (<lcv init>; <loop guard>; <lcv update>) {
   <loop body>
while (<loop guard>) {
   <loop body>
do { 
   <loop body>
while (<loop guard>);

As the syntax of the do-while loop suggests —with the loop guard being placed after the loop body— it differs from a while-loop in that the loop guard is evaluated after each iteration rather than before. As a consequence of this, a do-while loop always iterates at least once, unlike a while-loop.


[1] In general, it is not necessary for all Java classes comprising a program to be in the same folder. But how to make it work otherwise is beyond the scope of this course.

Header Comments

Make sure to include comments in your program identifying yourself, indicating that it is a solution to CMPS 134 Prog. Assg. #5, acknowledging any persons with whom you collaborated in developing your solution, and pointing out any flaws of which you are aware.

Submitting Your Work

Use the CMPS 134 "Dropbox" utility to submit your file into the prog5_dir folder. (Note: Do not submit any other .java file or any .class file.) You may submit more than one time, which would be appropriate if you made corrections or other improvements to your work since your last submission. But every time you submit, it should be a file named Within your dropbox folder, the names of the files containing your previous submissions will automatically be changed.