Homework 13: Dice Runs (Extra Credit)

Objective: To write a program that simulates the rolling of dice, and displays them, marking runs.

This assignment is a variation of Programming Project 6.1 from your textbook:

A run is a sequence of adjacent repeated values. Write a program that generates a sequence of random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this:

1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1

Use the following pseudocode:

  inRun = false
  For each valid index i in the array
    If inRun
      If values[i] is different from the preceding value
        Print ).
        inRun = false
    If not inRun
      If values[i] is the same as the following value
        Print (.
        inRun = true
    Print values[i].
  If inRun, print ).
Hint: Make sure there IS a following value before you check it.

To get full credit, you must use the following main method without changing it.

  public static void main(String[] args)
  {
    int[] dieTosses;
    int numDice;
    
    System.out.println("This program will roll dice for you.\nHow many dice do you want me to roll?");
    numDice = keyboard.nextInt();
    
    dieTosses = rollDice(numDice);
    
    System.out.println("Here are the die values, with the runs in parentheses:");
    printDiceWithRunsMarked(dieTosses);
  }
So your job is to write the methods "rollDice" and "printDiceWithRunsMarked" to produce the following sample output:
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
10
Here are the die values, with the runs in parentheses:
 2 6 4 3 2 5 (1 1) 5 6
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
5
Here are the die values, with the runs in parentheses:
 5 1 2 (4 4)
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
20
Here are the die values, with the runs in parentheses:
 2 1 3 5 1 3 (1 1) 6 6 1 5 (4 4) 3 5 6 1 3 2
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
20
Here are the die values, with the runs in parentheses:
 1 2 5 1 5 3 5 6 4 (3 3) 6 (1 1) 5 (2 2) 3 (2 2)
[cpersiko@fog cs111a]$ java DiceRuns
This program will roll dice for you.
How many dice do you want me to roll?
25
Here are the die values, with the runs in parentheses:
 3 5 (6 6 6 6) 5 (5 5 5) 1 4 (2 2) 5 1 2 6 4 2 4 6 4 2 1
[cpersiko@fog cs111a]$ 
This program is worth 50 points extra credit (Adds up to 5 percentage points to your overall semester grade)

Return to CS 111A page