The question is asking: 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 4 6 1 3 (2 2 2 2) 5 4 2 1 (2 2)
This is what I have so far:
Die.java
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; i <= TRIES; i++)
{
int n = d.cast();
System.out.print(n + " ");
}
System.out.println();
}
}
I was able to print 20 random die tosses but I don't know how to put them in parentheses like what the program is asking.