---
tags: Labs-F20, 2020
title: Lab 02 (Part 2)
---
# Lab 2: Conditionals and Reading Code, Part 2
## Problem 3.1 - *An* Implementation
A member of the club presents the following code:
```
include image
WIDTH = 200
CHAIR = scale(0.15, image-url("https://tinyurl.com/y25powyf"))
CAT = 2
if (CAT == 1):
place-image(CHAIR, 80, 130,
above(
rectangle(WIDTH, 100, "solid", "blue"),
above(
rectangle(WIDTH, 30, "solid", "aqua"),
rectangle(WIDTH, 30, "solid", "gold"))))
else if (CAT == 2):
place-image(rotate(120, CHAIR), 80, 130,
above(
rectangle(WIDTH, 100, "solid", "gray"),
above(
rectangle(WIDTH, 30, "solid", "aqua"),
rectangle(WIDTH, 30, "solid", "gold"))))
else:
place-image(rotate(170, CHAIR), 80, 80,
above(
rectangle(WIDTH, 100, "solid", "black"),
above(
rectangle(WIDTH, 30, "solid", "aqua"),
rectangle(WIDTH, 30, "solid", "gold"))))
end
```
\
Do you notice any repeated sections of code? How can you restructure the `if` expressions to reduce repetition? Write your answers in your Google Doc.
---
### CHECKPOINT:
**Call a TA over when you've discussed.**
---
## Problem 3.2 - *Your* Implementation
Now, try coding the solution you came up with in Problem 3.1!
What are the advantages and disadvantages to these two styles of code? Write your answers in your Google Doc.
---
### CHECKPOINT:
**Call a TA over when you've finished.**
---
## Problem 3.3 - Helper Functions
Another member of the club has come up with a helper function that would be useful for the conditional graphics code:
```
fun generate-scene(chair :: Image, sky-color :: String) -> Image:
place-image(chair, 80, 130,
above(
rectangle(WIDTH, 100, "solid", sky-color),
above(
rectangle(WIDTH, 30, "solid", "aqua"),
rectangle(WIDTH, 30, "solid", "gold"))))
end
```
Rewrite your implementation using `generate-scene`. How does it compare to the solution you came up with? What are the pros and cons of using a helper function in this case? Write your answers in your Google Doc.
---
## Success!
Congrats on finishing Lab 2!