CompSci 6- Classwork 13 - March 4, 2010
10 pts

Today's classwork focuses on using the one-loop for linear structures design pattern and reasoning about your code with loop invariants.

You will work with your group, but each of you will independently solve the problem. You should discuss the problem with your group and help each other debug.


Loop invariants provide a means to effectively determine the correctness of iterative solutions by helping one reason about the changes from one iteration to the next. A loop invariant is a boolean expression that is true each time the loop condition is evaluated. Typically the boolean expression is composed of variables used in the loop and evaluated each iteration of the loop. In particular, the initial establishment of the truth of the invariant helps determine the proper initial values of variables used in the loop condition and body. In the loop body, some statements make the invariant false, and other statements must then re-establish the invariant so that it is true before the loop condition is evaluated again.

The general structure of a loop is:

while (!done) { // check loop invariant: what is true at every step // body // update to maintain invariant }

Invariants can serve as both aids in recalling the details of an implementation of a particular algorithm and in the construction of an algorithm to meet a specification. You can also use invariants to characterize a variety of iterative processes.

Problem

For the following problem, complete the code in Eclipse, then run and test using the APT page within your browser.

  1. RunLengthEncode

You should state an invariant for your main loop in your README.


Submitting

When you are satisfied that you have completed the problems above, you should submit your project through Eclipse using the name Classwork13Mar4. A submission is not considered complete unless it includes all the Java code for the project and a README file.