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
  • Write a program that generates a sequence of 20 random die tosses and that prints the...

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

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

    . A run is a sequence of adjacent repeated values. Write a program that generates a sequence of 20 random die tosses and then 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: Set a boolean variable inRun to false. For each valid index i in the list if inRun If...

  • 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...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • Write a program in C# that generates (clean) insults at random. The program must have a...

    Write a program in C# that generates (clean) insults at random. The program must have a function that takes in the name of a person and prints out an insult directed to them. Further, the program should ask if the person has had enough insults and continue until they type “yes”. Your code must have at least five different kinds of insults. No profanity. Please use a function method for the random insult and do not use an array. The...

  • 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...

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