CPSC 120 Principles of Computer Science Fall 2025

Lab 7
Repetition

Due: Fri 10/31 at the start of class


Introduction

Loops provide a powerful shortcut for repetition — instead of cutting and pasting (with lots of small, tedious, error-prone edits) to get multiple copies of something, a loop allows you to repeat things as often as you want with only a few extra lines of code.

Quilts once again provide a good source of inspiration, since quilt block patterns often utilize repetition in their designs and the quilt itself is often constructed from multiple copies of individual blocks. This lab features three traditional quilt blocks.

Successfully completing this lab means that you are able to:


Handin and Presentation Meeting

Handin

Hand in a hardcopy (paper) of your worksheet in class.

To hand in your sketches:

It is OK if you copy your files to the handin directory at the very beginning of class.

Presentation Meeting

Presentation meetings for this lab will be the week of Nov 3.

Exercise #4 is the presentation problem. Come to the presentation meeting prepared to discuss your sketch. You may be asked to point out and explain how your code meets the requirements of the problem, explain how portions of your code work, and/or apply skills from the problem to a new situation.


Policies

The policies on late work and extensions, academic integrity, and the use of AI for this lab are the same as for lab 2. Review them there.


Preliminaries

Reference

Review this week's and last week's slides, in-class exercises handouts, and in-class exercise solutions for information, templates, and examples. The slides have some examples of the loop questions and translating the loop question answers into code. Match up the code (solutions for the 10/22 in-class exercises) with the answers to the loop questions (solutions for the 10/27 in-class exercises) for additional examples.


Exercises

  1. In this exercise you'll create a sketch which displays a strip of right-pointing flying geese, as shown. Each "goose" is a triangle whose long dimension is twice the length of the short dimension; for a horizontally-oriented pattern like the one shown, this means the height of the triangle is twice the width.

    The requirements for your sketch:

    To do this:

    • Complete the Exercise 1 section of the lab 7 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab7a.

    • Start with the basic sketch structure: open a drawing window and clear the background.

    • Write the loop to draw the flying geese pattern.

  2. In this exercise you'll create a sketch which which displays a single rail fence block, as shown. There should be four stripes within each quadrant, with a smooth change in the color of the stripes from left to right and top to bottom.

    The requirements for your sketch:

    To do this:

    • Complete the Exercise 2 section of the lab 7 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab7b.

    • Start with the basic sketch structure: open a drawing window and clear the background.

    • Write the function definition for the vertical stripe pattern, then use it to draw the upper left and lower right parts of the quilt block.

    • Write the function definition for the horizontal stripe pattern, then use it to draw the other two parts of the quilt block.

  3. In this exercise you'll create a sketch which displays a single log cabin block, as shown. Pay careful attention to the example so you get details of the pattern correct!

    The requirements for your sketch:

    To do this:

    • Complete the Exercise 3 section of the lab 7 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab7c.

    • Start with the basic sketch structure: open a drawing window and clear the background.

    • Implement the loop(s) you've identified, one at a time. Be sure to test your sketch after writing each loop.

  4. In this exercise you'll create a sketch which displays a quilt made from the blocks created in #1-3, as shown. The requirements for your sketch:

    To do this:

    • Complete the Exercise 4 section of the lab 7 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab7d.

    • Start with the basic sketch structure: open a drawing window and clear the background.

    • Write the function definition for the flying geese strip, then use it to draw those portions of the quilt.

    • Write the function definition for a rail fence block, then use it to draw those portions of the quilt.

    • Write the function definition for a log cabin block, then use it to draw those portions of the quilt.

Extra Credit

Challenge yourself and earn extra credit by going substantially beyond the required elements. (See the assignments and evaluation policy for more details on extra credit.)

For extra credit, create a new sketch named lab7e which displays a quilt containing one or more of the blocks described below. For each type of quilt block, you must have a drawing function which draws a single instance of that block. Include at least two instances of each block in your quilt.

Hint: For the twisted square and circular patterns, use of rotate() will be necessary/useful, as may other transformations (translate() and scale()) and the notion of vertex shapes. See sections 14.3, 14.5, and 14.7-14.8 in the textbook.)