A run is a sequence of adjacent repeated values. Write a program that generates
ID: 3650408 • Letter: A
Question
A run is a sequence of adjacent repeated values. Write a program that generates a sequence of 20 random die tosses 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: Set a boolean variable inRun to false. For each valid index i in the array list If inRun If values[i] is different from the preceding value Print ) inRun = false Else If values[i] is the same as the following value Print ( inRun = true Print values[i] If inRun, print ) Use the following class as your main class: import java.util.Random; /** This class generates randon die tosses and prints them out, marking "runs", i.e., adjacent repeated values, by including them in parenthesis. For example: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1 */ public class DieSequencePrinter { public static void main(String[] args) { final int TOSSES = 20; Random die = new Random(); Sequence seq = new Sequence(TOSSES); /* fill the array with random die tosses */ for (int i = 1; i <= TOSSES; i++) { seq.add(die.nextInt(6) + 1); } System.out.println(seq.markRuns()); } } Complete the following class in your solution: /** This class marks "runs", i.e., adjacent repeated values, by including them in parenthesis. For example: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1 */ public class Sequence { private int[] values; private int size; public Sequence(int capacity) { values = new int[capacity]; size = 0; } public void add(int value) { if (size < values.length) { values[size] = value; size++; } } /** Returns the string of values, with runs enclosed in parentheses. @return the string of values with the runs marked, for example "1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1" */ public String markRuns() { . . . } } Use the following class as your tester class: public class SequenceTester { public static void main(String[] args) { Sequence seq1 = new Sequence(5); seq1.add(3); seq1.add(3); seq1.add(4); seq1.add(5); seq1.add(5); System.out.println(seq1.markRuns()); System.out.println("Expected: (3 3) 4 (5 5)"); } }Explanation / Answer
sol: import java.util.Random; public class Die { /** Constructs a die with a given number of sides @param s the number of sides, e.g. 6 for a normal die */ public Die(int s) { sides = s; generator = new Random(); } /** Simulates a throw of the die @return the face of the die */ public int cast() { return 1 + generator.nextInt(sides); } private Random generator; private int sides; } DieTest.java public class DieTest { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 20; for (int i = 1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.