Question

Write a program that generates a sequence of 20 random die tosses in an ArrayList and...

Write a program that generates a sequence of 20 random die tosses in an ArrayList and that prints the die values, marking only the longest run, like this: 1 2 5 5 3 1 2 4 3 (2 2 2 2) 3 6 5 5 6 3 1

If there is more than one run of maximum length, mark the first one.

Must use ArrayList, not an array!

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.ArrayList;
import java.util.Random;


public class Test {
  
  

public static void main(String argv[])
{
ArrayList<Integer> R = new ArrayList<Integer>();
  
Random r = new Random();
  
//generating and storing 20 random die tosses
//and also finding the longest run
int max=1;
int c1=0;
int c2=0;
int i=1;
int p=r.nextInt()%6;//random die value
if(p<0)p=p*-1;
p=p+1;
R.add(p);//adding to arraylist
int k;
int pc1=0,pc2=0;
  
while(i<20)
{
k=r.nextInt()%6;
if(k<0)k=k*-1;
k=k+1;
R.add(k);
if(p==k)//finding longest run
{
pc2=i;
if(max<(pc2-pc1+1))
{
max=pc2-pc1+1;
c1=pc1;
c2=pc2;
}
}
else
{
pc1=i;
}
  
p=k;
  
i++;;
}
  
//displaying output
// System.out.println(R.size());
for(k=0;k<20;k++)
{
if(k==c1)
{
  
System.out.print("("+R.get(k)+" ");
}
else if(k==c2)
System.out.print(R.get(k)+") ");
else
System.out.print(R.get(k)+" ");
}
System.out.println();
}
}

output:

run:
6 5 5 (3 3 3) 6 2 2 6 3 6 5 1 5 1 2 2 6 2
BUILD SUCCESSFUL (total time: 0 seconds)

Add a comment
Know the answer?
Add Answer to:
Write a program that generates a sequence of 20 random die tosses in an ArrayList and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of...

    4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of adjacent repeated values. write a Java class (RunsPrinter. java) with one main method that generates a sequence of 20 random die tosses in an array (and not an ArrayList) and then prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 124 3 (2 2 2 2) 3 6 (5 5) 6 3 1 Examples...

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • C++ Write a program that generates and prints 24 random values using an array and the...

    C++ Write a program that generates and prints 24 random values using an array and the rand () function. Implement an algorithm to find the maximum and a minimum number of random numbers generated. Use the header "#include <ctime>", "#include <cstdlib>" in addition to the usual header: "#include <iostream>"

  • Write a program that generates 1,000,000 random integers between 0 and 9 and determines the longest...

    Write a program that generates 1,000,000 random integers between 0 and 9 and determines the longest consecutive sequence of each of the numbers. The program displays the numbers and their longest consecutive sequence lengths in descending order. in a c++ visual studios 2017

  • F a as a sequence of adjacent array elements such that each value in the run array was of size 10...

    f a as a sequence of adjacent array elements such that each value in the run array was of size 10 9. We define a "run" of elements o (except for the first) is one greater than the previous and looked like: value. For example, say the 3 2 16 9 7 8 9 2 a: position 0 3 We have three runs in this array: (1) between 9,10,1, 12) and, (3) between positions 7 and 8, (15, 16) positions...

  • Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...

    Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will get a die face value of 1-6. We can use rand function and math ceiling function to get a random number of 1-6 generated. The program will simulating rolling 5 dice simultaneously and store their face values into an array of size 5. The program will keep rolling the dice and print the following: 1. All face values in one line 2. If any...

  • Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.r...

    Please write a JAVA program according to following requirement. Thanks Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.random () generates a random integer greater than or equal to 0 and less than 1. The expression Math. random) 6generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible...

  • (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list)...

    (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects The language must be written, compiled, and run on TEXTPAD. The Language is in Java.

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT