To ensure that the code you supply conforms to the intended loop invariant, there are assert statements to verify that the invariant is true immediately before and after each loop iteration. (If, when an assert statement is executed, the condition specified in the statement evaluates to false, an exception is thrown.)
By default, assertions are disabled in Java, which means that they have no effect. To enable them, follow these instructions.
Provided within the class is a main() method intended to be used for testing purposes. Here is a sample run, with user input shown in boldface:
$ java -ea FindKthOccurrence Welcome to the Find K-th Occurrence Program. Enter string of interest: the cattle had a battle in Seattle Enter character of interest: t "t" has 1 occurrences of 't' "the cat" has 2 occurrences of 't' "the catt" has 3 occurrences of 't' "the cattle had a bat" has 4 occurrences of 't' "the cattle had a batt" has 5 occurrences of 't' "the cattle had a battle in Seat" has 6 occurrences of 't' "the cattle had a battle in Seatt" has 7 occurrences of 't' "the cattle had a battle in Seattle" has fewer than 8 occurrences of 't' Goodbye. |