CPSC 120 Principles of Computer Science Fall 2025

Lab 5
Modularity and Abstraction

Due: Fri 10/10 at the start of class


Introduction

This week's lab deals with functions, an important tool for modularity and reuse. Declaring a function bundles a series of related instructions (such as the instructions to draw a complex shape) into one package distinct from the rest of the program, and adding parameters allows the same basic steps to be reused with different values without having to rewrite the steps themselves. Both of these aspects of functions become increasingly important as programs get larger and more complex.

Several of the exercises are based on traditional quilt patterns. These kinds of quilts are an excellent real-world example of modularity and abstraction — first individual pieces of fabric are assembled into blocks, then the blocks are assembled to create the whole quilt.

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 Oct 13.

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 slides, in-class exercises handouts, and in-class exercise solutions for information, templates, and examples.


Exercises

  1. In this exercise you'll create a sketch which draws the hourglass quilt shown. The requirements for your sketch:

    To do this:

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

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

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

    • Complete the sketch:

      • Write the function definition for the upright hourglass pattern, then use it to draw the upper left and lower right blocks.

      • Write the function definition for the sideways hourglass pattern, then use it to draw the other two blocks.

  2. In this exercise you'll create a sketch which draws the quilt shown. The requirements for your sketch:

    Use quad() to draw the center diamond in each block. Look it up in the Processing API to find out how to use it.

    To do this:

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

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

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

    • Draw the first layer of blocks as shown:

      • Write the draw block function, then use it to draw the upper left block.

      • One by one, add each of the other seven blocks.


    • Add the larger copy of the block in the center.


    • Add the final copy of the block in the center to complete the sketch.


  3. In this exercise you'll create a sketch which draws the quilt shown. (This is a traditional pattern known as Drunkard's Path.) The requirements for your sketch:

    You'll need to use arc(). Look it up in the Processing API to find out how to use it.

    To do this:

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

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

    • Complete the Exercise 3a section of the lab 5 worksheet.

    • Write the drawing functions for the four building blocks and test them by calling them from draw. Draw the blocks anywhere in the drawing window — this is just to see that they work. When your functions work, remove the calls from draw().

    • Complete the Exercise 3b section of the lab 5 worksheet.

    • Write the drawing function for a whole quilt block, then use it to draw the quilt block in the upper left corner.

    • One by one, add the function calls to draw the other three quilt blocks to complete the pattern.

  4. In this exercise you'll create a sketch of your own design. What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following elements:

    To do this:

    • Complete the Exercise 4a section of the lab 5 worksheet.

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

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

    • Build up your sketch bit-by-bit. When you get to something that you identified as needing a drawing function, complete that part of the Exercise 4b section of the lab 5 worksheet before writing code.

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.) Some possibilities for #4:

It's OK to add on to your lab5d sketch but be sure to include a brief description of what you've done for extra credit in a comment at the beginning of the sketch.