Dot Patterns Laboratory - Adapted from Northeastern University

Lab 3
Due September 28, 1999

Overview

In this laboratory, you will draw a series of 16 dot patterns using nested for loops. You may think of the dot patterns as geometric puzzles to be solved using loop structures. The 16 puzzles are printed immediately after the description of the exercise.

Project Files

To start on this exercise, copy the folder Dot Patterns from the course folder. This folder contains the source files DotPattern.java and DrawingBoard.java, as well as a compiled solution PatternsSolution.

Activities

Run the PatternsSolution program. You will see Pattern 1 drawn in the drawing window. The dot pattern should look like the picture below (except that we have reduced the size by 50% and we have added row and column labels).

 

By inspecting procedure DrawPattern in the source file DotPattern.java, you will see that the shape of Pattern 1 is controlled by the following nested loop structure found the first block of the switch statement in procedure DrawPattern:

switch (pattern) {

case 1:

    for (row = 0; row < 8; row++)

        for (col = 0; col < 8; col++)

            BigDot(row, col);

    break;

As this nested loop executes, procedure BigDot automatically connects each dot to the next dot that is drawn. You may ask: "Why are most of the connections horizontal lines?" The answer lies in the order in which the loops are nested. The row loop is the outer loop and the column loop is the inner loop. This means that, for each value of the row index, an entire cycle of the column loop must be executed. Therefore, for each row, the inner column loop draws a dot in column 0, then in column 1, and so on through column 7. This explains why the dots in each row are connected by a sequence of horizontal lines. When a given row is finished the last dot drawn is in column 7. The next row begins with a dot in column 0. This explains the diagonal lines from upper right to lower left which connect successive rows.

The procedure BigDot(row, col) arranges the details of drawing the dots and making the lines which connect successive dots. Since these details are not the main focus of this exercise, we will not discuss them here. However, you might note that the DrawingBoard class can serve as the basis for graphical classes in the future.

Your first task in this exercise is to draw at least 12 of the remaining 15 dot puzzles using appropriate loop structures. Each puzzle requires giving a new twist to the loop structures. For example, in puzzles 3 to 6, you must figure out how to replace the constants 0 and 8 in the inner loop by expressions which depend on the row index. In some of the later puzzles, you will need a sequence of loop constructs since a single nested loop structure will not be sufficient. In a few cases, you will need an extra loop variable besides row and col.

You should insert the code for the remaining 15 patterns as blocks in the switch statement of procedure DrawPattern. See the location in the file marked with the comment:

// ï?ï?ï Enter additional pattern solutions below

When your solutions are entered in the switch statement, the driver routine UserLoop will enable you to interactively test all of the patterns.

When your solutions are complete, analyze their complexity.  For now we will do this in the simplest way possible: Keep track of the actual step count for each pattern (just the pattern code you write). The best way to do this is to simply write the number of times each statement will execute next to it on the hardcopy of the program that you turn in.

Educational Goals:

1. Loop constructs can be confusing when expressed in the symbols of a programming language such as Java. This laboratory should help you learn how to visualize what loop constructs are doing and how to create loops with specified properties.

2. The regular arrangement of dots in a rectangular grid imitates what people do when information is organized in a table. You may think of each dot as representing a data location and the loops as visiting all or some of the data locations in a particular order. The concept of visiting data in a particular order will be a recurring theme in this course.

3. The concept of table is formalized by computer scientists as the array and by mathematicians as the vector or matrix which we have already seen.

4. Finding solutions to the loop puzzles should also improve your problem solving skills. I hope you have fun solving the puzzles.

Lab 5 Deliverables

A copy of your code in the dropbox with the usual naming conventions.  Hardcopy of your code and your step counts.

The patterns

Pattern 2.

Pattern 3.

Pattern 4.

Pattern 5.

Pattern 6.

Pattern 7.

Pattern 8.

Pattern 9.

Pattern 10.

Pattern 11.

Pattern 12.

Pattern 13.

Pattern 14.

Pattern 15.

Pattern 16.