CPSC 120 Principles of Computer Science Fall 2024

Lab 9
Repetition, Again

Due: Fri 11/8 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab.


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.

This week's lab focuses on breaking up complex pictures into loops, and working with the different loop patterns discussed in class (repeat-as-long-as loops and counting loops; one thing in the loop body, multiple things in the loop body; a single loop variable, multiple loop variables; a single loop, one loop after another, nested loops). Quilts once again provide the source of inspiration.

Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

The short version:

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Preliminaries

Getting Started

Review the posted slides and examples for identifying loop patterns, as well as the posted slides and examples for repeat-as-long-as loops, counting loops, and nested loops. Keep in mind that considering what changes and how it changes is crucial for deciding on loops — in one loop, whatever changes should change consistently from one repetition to the next.

Also read through all of each exercise before starting it — there's important information about how to do the task after the initial statement.

Then, consider the loop questions to help you build up each loop. It is strongly recommended that you identify at least what is repeated and what changes in comments in your sketch.

"Any Size Window"

The exercises ask you to design your sketches so that they work with any size window. Remember that this means to use the system variables width and height instead of specific numbers when referring to the drawing window dimensions.


Exercises

Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in. (Refer back to lab 2 for more on Auto Format if needed.)

You must use loops wherever it is appropriate to do so! (Reminder: a loop when something repeats more than a few times and there is predictable change from one copy to the next.) You will receive very little credit for producing the right drawing without using loops.

Exercise 1

Create a sketch named lab9a which draws a color variation of the Jacob's Ladder quilt block, as shown. You can choose your own colors, but you should have two light colors and two dark colors and the pattern should be the same as shown — things which are the same color in the picture should be the same color in your sketch.

Design your sketch so that it works with any size window.


To create this sketch:

Exercise 2

Create a sketch named lab9b which draws a tumbling blocks quilt, as shown. Use quad to draw the diamond shapes. You don't need to match the example exactly in terms of the number of repetitions of the pattern, but the window should be completely filled. You can also choose your own colors, but should use lighter, medium, and darker colors in the configuration shown.

Design your sketch so that it works with any size window.


To create this sketch:

Exercise 3

Create a sketch named lab9c which draws a strip of 16 flying geese as shown. You can choose your own colors, but each group of four triangles should be the same color and the color of successive groups should change smoothly, as shown. (In the example, red increases to the right.)

Recall from lab 8 that the height of each triangle should be twice its width. Design your sketch so that it works with any height of window, but make sure you size the window so that there is room for 16 triangles the height of the window.



To create this sketch:

Exercise 4

Create a sketch named lab9d which displays a block star quilt, as shown. (To see the star pattern, focus on the color rather than the white shapes.) You can choose your own colors, but you should vary one color component horizontally and a different one vertically. (In the example, the red decreases to the right and the blue increases going down.)

Design your sketch so that it works with any size window.

Hint: view the pattern as white shapes (squares and quads) drawn on top of a background of colored squares. (The white triangles on the edges are just quads that extend past the edges of the drawing window — you do not need to handle those specially by drawing triangles.) Break your development of the sketch down into three steps: draw the background of squares all the same color, add the color variation, and then draw the white squares and quads.


Extra Credit

You can earn extra credit by going substantially beyond the required elements. Create a new sketch called lab9e with a recognizable scene. (A geometric pattern, such as a quilt, is OK — the idea is that the scene should be deliberately created and have some overall theme rather than just being a jumble of different elements.) Some possibilities for extra credit:

Include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. More creative and challenging elements will earn more points.