CPSC 120 Principles of Computer Science Fall 2024

Lab 4
Modularity and Abstraction

Due: Fri 9/27 at the start of lab

Labs are due at the start of lab. It is OK if you show up to lab and copy your files to the handin directory at the very beginning of lab, 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. I will check timestamps, and files handed in more than five minutes after the start of lab will be considered late.

See the policy on late work and extensions.


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:

Academic Integrity and Collaboration

Labs are a chance to practice and gain understanding. You may get help in office hours, at Teaching Fellows, and from other students and may use other materials (such as reference books or websites) but the course materials (including provided links or references to documentation and other materials) along with office hours and the Teaching Fellows should be your primary resources. Always start with these! You must document any help received and any outside resources used.

Be careful not to rely too much on others — things often look easy (or at least easier) when someone else does it. (You don't want the exam to be where you discover this!) You should always make the first attempt at doing something yourself, and make sure that you always fully understand the help you received — you should be able to explain your solution to someone else, and should never just write down code that someone else wrote or told you to write without be able to explain what it does and why.

Also remember that the goal of the exercises and of this course is learning the process of creating programs, and that what you hand in for a grade is a reflection of your engagement of that process. When you get help, also ask about that process — if you're stuck on what to do next, ask not only what to do next but also how one knows that's what to do, or if your program isn't working correctly, ask not only what's wrong and how to fix it but also how to track down the problem for yourself.

You may not shortcut to a solution by copying code (except as specifically authorized in instructions) or working with others to write code together. You may not be in possession of someone else's program or solution before you have handed in your own. See the full collaboration policy for more on this.

Handin

To hand in your work:


Preliminaries

Getting Started

Before you start writing code, remind yourself of:

All of these things have been covered in class, and the slides and in-class exercises handouts posted on the schedule page are the places to find this information. It is also a good idea to review the examples from class and make sure you understand the role of each line of code in the sketch — what does it do? Why is it there? How does it relate to the patterns for function definitions and class (as well as the previous topics of sketch structure, variables, and animation)?

Keep incremental development in mind — even though the exercises list the "create drawing function(s)" step before the "complete the sketch" step, you can still apply incremental development. Start with a simple function body — draw just one of the shapes you'll need, for example — and make one call to the function from draw(). Gradually build up the body of the function; once it draws the complete pattern, add a second call to it in draw() (and so on). You can also start without parameters — have the function draw the shape in a fixed spot — and then add parameters once you have that working.

Also remember how to work out coordinates — draw pictures and label what you know! This is especially important when variables and parameters are involved because you need to work out expressions to calculate positions and sizes rather than just figuring out the correct number.

Notes on Functions

Integer Division

Let's say that width is 600 — what is the value of 2/3*width? You expect it to be 400, but this expression actually evaluates to 0! This is because the division operator / works differently depending on the type of its operands. When used with floats, as in 2.0/3.0 the result is as expected — 2.0/3.0 = 0.66667. But when used with ints, the result is the number of whole times the denominator divides the numerator — 3/2 = 1, 600/140 = 4, and 2/3 = 0. (Another way to think about it is that integer division does the division but then drops the decimal part e.g. 3/2 = 1.5, which is 1 when the 0.5 is dropped.)

To avoid surprises, here are two guidelines:


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.)

Exercise 1

The goal in this exercise is to create a sketch named lab4a which draws the hourglass quilt shown.


To create this sketch:

Exercise 2

The goal in this exercise is to create a sketch named lab4b which draws the quilt shown.


To create this sketch:

Exercise 3

The goal in this exercise is to create a sketch named lab4c which draws the quilt shown. (This is a traditional pattern known as Drunkard's Path.)


To create this sketch:

Exercise 4

Create a sketch of your own design. (Name this sketch lab4d.) What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following required elements:

Extra Credit

You can earn extra credit by going substantially beyond the required elements. Add to your sketch for #4 and include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. Some possibilities:

More creative and challenging elements will earn more points.