Here we define a plateau within a sequence to be any contiguous nonempty subsequence all of whose values are the same. In particular, if the sequence in question is stored in an array a[], then a plateau within that sequence would be any array segment a[low..high) such that 0 ≤ low < high ≤ a.length and a[low] = a[low+1] = ··· = a[high-1].
For this activity, you are to complete the lengthOfLongestPlateau() method in the MaxLenPlateauApp application program in accord with the loop invariant that is specified there. The method includes strategically located assert statements to verify that the loop invariant is true at the beginning and end of each iteration of the loop body. (If, when an assert statement is executed, the condition specified in the statement evaluates to false, an exception is thrown.)
The postcondition indicated after the loop should give you hints as to the appropriate loop guard and initial values for the variables.
By default, assertions are disabled in Java, which means that they have no effect. To enable them, follow these instructions.
To test your work, the provided application program includes a main() method that expects to receive, via a command line argument (or what jGrasp refers to as a "run argument"), the name of a data file containing sequences of integers, one sequence per line. Here is such a file.
Note: If you forgot how to supply an application program with run arguments, ask your GTA. Or read this. End of note.
Feeding the data in the given data file to the program should result in output that looks like this:
Array: [3, 7, -2, 4, 4, 4, 2, 2, 2, 2, 2, 2, 8, 5, 5, 3, 1, 0, 4, 4, 4, 7, 9, 11, 0, -5, 7, 7, 7, 3] Computing length of longest plateau ... Longest plateau is of length 6 Array: [9, 9, 9, 0, 0, 2, 5, 5, 5, 5, 5, 5, 0, 0, 1, 1, 1, 1, 1, 1, 1, -4, 4, 5, 6, 7, 8, 8, 8] Computing length of longest plateau ... Longest plateau is of length 7 Array: [0, 0, 4, 9, 9, 9, 2, 2, 7, 7, 7, 7, 4, 5, 6, 8, 8, 8, 8, 8, 2, 1, 1, 1, 1, 1, 1, 1, 1] Computing length of longest plateau ... Longest plateau is of length 8 Array: [6, 6, 6, 6, 3, 4, 5, 6, 7] Computing length of longest plateau ... Longest plateau is of length 4 Goodbye |