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

Complete the following files:

DieSequencePrinter.java

import java.util.Random; /** This class generates randon die tosses and prints the longest run, by including it in parentheses. For example: 1 2 5 5 3 1 2 4 3 (2 2 2 2) 3 6 6 6 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(. . .); } seq.findLongestRun(); int start = seq.getLongestStart(); int size = seq.getLongestSize(); // Print the longest run . . . } }

Sequence.java

/** This class finds the longest "run", i.e., adjacent repeated values, in a sequence. */ public class Sequence { private int[] values; private int size; private int longestStart; private int longestSize; public Sequence(int capacity) { values = new int[capacity]; size = 0; } public void add(int value) { if (size < values.length) { values[size] = value; size++; } } public int get(int index) { if (0 <= index && index < values.length) return values[index]; else return 0; } /** Finds the longest run in a sequence and sets the longestStart and longestSize fields. If there are multiple runs of maximum length, the first one is found. Call getLongestStart and getLongestSize after calling findLongestRun. */ public void findLongestRun() { . . . } /** Gets the start of the longest run. Call findLongestRun before calling this method. @return the start of the longest run, or -1 if there was no run */ public int getLongestStart() { return longestStart; } /** Gets the size of the longest run. Call findLongestRun before calling this method. @return the size of the longest run, or 0 if there was no run */ public int getLongestSize() { return longestSize; } }

SequenceTester2.java

Use the following file:

SequenceTester.java

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);
      seq1.findLongestRun();
      System.out.println(seq1.getLongestStart());
      System.out.println("Expected: 0");
      System.out.println(seq1.getLongestSize());
      System.out.println("Expected: 2");
      Sequence seq2 = new Sequence(5);
      seq2.add(1);
      seq2.add(2);
      seq2.add(3);
      seq2.add(4);
      seq2.add(5);
      seq2.findLongestRun();
      System.out.println(seq2.getLongestStart());
      System.out.println("Expected: -1");
      System.out.println(seq2.getLongestSize());
      System.out.println("Expected: 0");
   }
}