Question

In: Computer Science

*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...

*****IN JAVA*****

A run is a sequence of adjacent repeated values. Write a code snippet 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 3)

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]

//special processing to print last value

If inRun and last value == previous value, print  “ “ + value + “)”)

else if inRun and last value != previous value, print  “) “ + value )

else print “ “ + last value

Solutions

Expert Solution

Solution:

I have used the given pseudocode.

I also added some changes for formatted printing and removing exceptions.

Code:

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(6)+1;
        boolean inRun = false;
        
        for (i = 0; i < die.length-1; i++) 
        {
                if(inRun)
                {
                        if( i>0 && die[i]!=die[i-1])
                        {
                                System.out.print(")");
                                inRun = false;
                        }
                        else
                                System.out.print(" ");
                }
                if(!inRun)
                {
                        if(i>0)
                                System.out.print(" ");
                        if(die[i]==die[i+1])
                        {
                                System.out.print("(");
                                inRun = true;
                        }
                        
                }
                System.out.print(die[i]);       
                
        }
        if(inRun && die[die.length-1] == die[die.length-2])
        {
                System.out.print(" "+ die[die.length-1] + ")");
        }
        else if(inRun && die[die.length-1] != die[die.length-2])
        {
                System.out.print(")"+ die[die.length-1]);
                }
        else 
        {
                System.out.print(" " + die[die.length-1]);
        }
            
    }
}

Output of 5 random runs:

Note:Each line is an output of seperate runs.

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

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

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

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

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

Related Solutions

Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet 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 3) Use the following pseudocode: inRun = false for each valid index i in the array If...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a java code snippet that allows a teacher to track her students’ grades. Use a...
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
***IN JAVA*** 5. Currency conversion: Write a snippet that first asks the user to type   today's...
***IN JAVA*** 5. Currency conversion: Write a snippet that first asks the user to type   today's US dollar price for one  Euro. Then use a loop to: -- prompt the user to enter a Euro amount. (allow decimals) -- convert that amount to US dollars. (allow decimals) -- print the amount to the screen, formatted to two decimal places Use 0 as a sentinel to stop the loop.
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert...
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert an array of N element (Please do not use the default function “reverse()). 1.2 - There is a famous programming (algorithm) concept that can vastly reduce the complexity of Recursive Fibonacci Algorithm. 2 .a) What is the name of this concept 2.b) Write the execution time and memory complexity of Fibonacci sequences, once this concept is applied 2.c) Explain the main idea behind this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT