Dot Patterns Laboratory - Adapted from Northeastern University

Lab 1
Due September 18, 2002

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 Dots from the course materials folder. This folder contains the source files Dots.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.

 

By inspecting procedure DrawPattern in the source file Dots.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 < ROWS; row++)

        for (col = 0; col < COLS; 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 ROWS - 1. 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 COLS - 1. 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 13 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.

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. In addition, this lab will provide a preview of many of the issues we will see when dealing with Matrices.

3. The concept of table is formalized by computer scientists as the array and by mathematicians as the vector or matrix which we will spend significant time on.

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

5. This lab provides an ideal time to learn how to use the Debugger in ProjectBuilder. We'll discuss this at the beginning of the lab.

What is a good solution? The goal of this lab is for you to learn how to write concise, general, solutions to loop problems like these. First off, your solution should not depend explicitly on the fact that we have exactly eight rows and columns. It ought to be easily expanded to different numbers (e.g. 10). To that end use constants (ROWS, COLS) instead of actual numbers when at all possible. Second, this generality is further supported by conciseness. As a rule of thumb, the fewer calls to BigDot you have the higher the likelihood that your code is general. Most of these puzzles can be done with a single call to BigDot and none of them require more than two.

Do not use extra variables in your loops. Stick to row, col, and k. You are not allowed to use any Java statements other than for loops and the calls to BigDot. No if statements for example.

Lab 1 Deliverables

Put a copy of your Dots.java file in the course dropbox after renaming to include your name (e.g. if I did it I would call it ChownDots.java; You should also give me hardcopy of your code.

Grading guidelines. Things you will lose points for: not doing the required number of patterns, using extra variables, having statements other than "BigDot(...)" and "for ( ... )", using any numbers other than 0, 1 or 2 in your code, code that only works for grids of size 8. Things you get points for: correct solutions, writing general code, minimizing the number of loops needed.

This lab is to be done on your own since there is not any difficult coding. Showing others your code will be considered plagarism.

For future reference, we will be doing a lot of projects that use graphics in this course. In those projects you will be writing the drawing routines yourself for the most part. This is a good opportunity to look at the code to start to get an idea of how graphics works in Java.

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.