Lab 4: Loops

Download and unzip lab4.zip, which contains all the files you will need for this assignment. Open the file Lab4.java in DrJava. All the code you write for this assignment should be added to this file. Note that some useful methods have already been implemented for you.

In each exercise, you will be given a task for the robot to complete. It does not matter where the robot ends up at the completion of the task, as long as the appropriate squares have changed color. To receive full credit, your program must complete the task and terminate without any errors. As always, you should aim to minimize duplicate code in your solutions.


Exercise 1: completeBars

In this exercise, the robot initially finds itself on a map like the one shown in the example below.

Each column initially contains exactly one dark square. Complete the completeBars method, which should darken all the squares in each column that are below the original dark square. After calling the completeBars method on the example shown above, the map should appear as follows:

You should assume that the robot will always start in the southwest corner facing east, that there will be exactly one dark square per column, and that there will be no walls on the map. You should not make any assumptions about the dimensions of the map, or about the position of the dark squares within each column.

Be sure to test your code on both "bars1.txt" and "bars2.txt". (For your convenience you may use the testCompleteBars1 and testCompleteBars2 methods to test your code on these maps.)


Exercise 2: combinePiles

In this exercise, there are initially two piles of dark squares, one at the bottom of each column, as shown in the example below (lefthand picture).

Before combinePiles: After combinePiles:

Complete the combinePiles method, which should move all dark squares from the left pile to the top of the right pile. After calling the combinePiles method on the example shown in the lefthand picture above, the map should appear as shown in the righthand picture.

Hint: Move one dark square at a time, from the left column to the right column.

You should assume that the robot will always start in the southwest corner facing north, that there will always be exactly 2 columns, and that each column contains a pile of zero or more (contiguous) dark squares. There will never be any walls on the map. A pile of dark squares may fill an entire column, or there may be light squares above the pile. There will always be at least enough room in the right column to add all the darks from the left pile (but there may be exactly enough room). You should not make any assumptions about the number of rows in the map, or the heights of the 2 piles.

Be sure to test your code on both "piles1.txt" and "piles2.txt".


Exercise 3: connectDots

In this exercise, the robot initially finds itself at the beginning of a dotted path, like the one shown in the example below.

There will always be exactly one light square between each dot. Complete the connectDots method, which should darken squares to connect the dots. After calling the connectDots method on the example shown above, the map should appear as follows:


Challenge: sort

The robot is initially facing east on the northwest corner of a rectangular room (of unknown size) containing piles of dark blocks (of unknown sizes, in an unknown arrangement).

Write a method called sort to arrange the piles from smallest to largest, as illustrated in the sample solution below. You should not need to use any additional Java constructs.