Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A run is a sequence of adjacent repeated values. Write a program that generates

ID: 3662583 • Letter: A

Question

A run is a sequence of adjacent repeated values. Write a program that generates a
sequence of 20 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

this is the code I have. It produces an out of bounds error.

import java.util.*;


public class Run {

    public static void main(String[] args) {
        int i;
        Random rand = new Random();
        int[] die = new int[20];
        for (i = 0; i < die.length; i++) {
            die[i] = rand.nextInt();
        }
      
        boolean inRun = false;
        if (die[i] != die[die.length])
        if (die[i] == die[i + 1]) {
            inRun = true;
        }
        for (i = 0; i < die.length; i++) {
            if (inRun) {
                if(die[i] != die[i - 1]) {
                    System.out.print(")");
                    inRun = false;
                }              
            }
            if (i == die.length - 1) {}
            else if (die[i] == die[i + 1]) {
                    System.out.print("(");
                    inRun = true;
                }
            }
                System.out.println(die[i]);

                if (inRun){
                    System.out.print(")");
                }
              
    }
        }
   

Explanation / Answer

/**The java program that prints an array of 20 random numbers with repeated numbers as
* in paranthesis.*/
//Run.java
import java.util.Random;
public class Run
{
   public static void main(String[] arg)
   {      
       //Create an Random class object
       Random rand= new Random();
       //An array of size 20
       int [] die = new int [20];

       //Note: The variable , i causing run time error
       //array index out of bounds exception.
       //Use local variable i in both for loops
       for (int i= 0; i < die.length; i++)
       {
           die[i] = rand.nextInt(6)+1;
       }

       //set inRun to false
       boolean inRun = false;

       for (int i = 0; i < die.length-1; i++)
       {
           //Check if inRun is true
           if ( inRun == true )
           {
               //Check if no more same elements then close the paranthesis.
               if (die[i] != die[i-1])
               {
                   System.out.print(")");
                   inRun = false;
               }
           }

           //Check if inRun is false and check for same consecutive elements
           if (inRun == false)
           {
               if(die[i] == die[i+1])
               {
                   System.out.print("(");
                   inRun = true;
               }
           }
           System.out.print(die[i]);

       }
       if(inRun)
       {
           System.out.print(die[die.length-1]);
           System.out.print(")");
       }
       else
       {
           System.out.print(die[die.length-1]);
       }
   }
}

------------------------------------------------------------------------------------------------------

Sample Output:

Sample run1:
(22)(111)(44)253165462(33)25

Sample run2:
(222)6(22)54652(55)6426(225)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote