# CS 2500 Lab 4: Structures and Unions

#### New lab partners, new friends

It is time to switch lab partners! You may not work with a partner with whom you have previously worked this semester. Choose someone you haven't already worked with and relocate so that the two of you are sharing a single workstation. Sharing is caring, and also the key to good pair programming.

#### Switching pilot and co-pilot

In previous labs you were switching pair-programming roles in-between exercises. In this lab and in future labs, you will switch roles at regular intervals throughout the lab. The teaching assistants and tutors will announce that it is time to switch roughly every fifteen minutes.

#### Don't delete as you go

In previous labs some students deleted their solutions for exercises after completing them—don't do this! It is common for later exercises to make use of functions and templates introduced in earlier ones.

#### DR uber alles

Remember to practice using the design recipe when designing programs!

Use it, love it.

#### Don't Use IE

You will be unable to see the exercise numbers if you use IE.

## I. Last week on CS 2500

1. In this class, each lab section consists of a section number (1–7), a head TA, and a supporting TA. Design a data definition and define a data structure for representing lab sections. Give three examples of lab sections. (The course website has a table of all the lab sections and lists the names of all the TA's.)

Develop a program that consumes a lab section and returns a descriptive string similar to `"CS 2501 Section 3: George Washington, John Adams"`.

1. Develop a function that computes the result of the formula (a+b)2/(a-b)2. Give an example that uses the function. For your example, (1) write out the steps to evaluate it as comments in the definitions window, and then (2) use the stepper to compare how you evaluated the formula with how DrRacket evaluated the formula. Unless you have made a mistake, both sets of steps of the formula should give the same vaule.

## II. This week on CS 2500 – Structures and unions

Hint: in the following exercises give names to your examples so you can use them again later.

1. A rock band has a name and consists of a singer, a guitarist, a bassist, and a drummer. A jazz band has a name and consists of a trumpeter, a bassist, and a drummer. A pop band has a name and consists of a singer and a two synthesizer players. A band is either a rock band or a jazz band or a pop band. Write one data definition for each kind of band and then write a data definition for a band in general. Produce three examples of a band (one for each kind). Write a template for a function that consumes a band.
1. Design a function that consumes a band and a name and returns `true` if the name matches the name of the band and `false` otherwise.
1. Design a function that consumes a band and a drummer. If the band is a rock band it returns a rock band which is the same as the old one, but with the drummer replaced by the one given as input to the function. If the band is not a rock band it returns it unchanged.

## III. World with structures

In the following exercises, represent the world as a struct with two `posn`s. The first `posn` represents the current position of a blue circle, and the second `posn` represents the current position of a red circle. When the user clicks the mouse the red circle will immediately move to where they clicked, and over time the blue circle will move to meet it.

1. Design a function `mouse-handler` to react to mouse events. It consumes four inputs: a World, an x coordinate, a y coordinate, and a MouseEvent as described in `MouseEvent`. When the MouseEvent is `"button-down"` the function `mouse-handler` should create a World where the first `posn` is the same as the given World's first `posn` and the second `posn` is the position of the mouse click. On any other mouse event (`"button-up"`, `"drag"`, `"move"`, `"enter"`, or `"leave"`) the function `mouse-handler` should return the given World unchanged.
1. Design a function `tick-tock` to react to clock events. The purpose of the function is to gradually equate two `posn`s as the clock progresses. The function consumes a World and produces a new World where both coordinates x and y of the first `posn` are increased or decreased by 1 (or 0) so that they approach the coordinates of the second `posn`.

For example, if the input World is ((1,3), (5,1)) then `tick-tock` should return the new World ((2,2), (5,1)).

1. Design a function `world-draw` that consumes a World and returns a 300 × 300 scene with a solid blue circle of radius 15 at the position represented by the first `posn` and a solid red circle of radius 10 at the position represented by the second `posn`. When they overlap, the red circle should appear on top of the blue circle.
1. Use `big-bang` and the three functions you wrote to create an animation where you click to place a red circle somewhere in the canvas and then a blue circle moves along the canvas trying to reach the red circle. The initial position of the blue circle is determined by how you choose to initialize the World. You'll need `on-mouse`, `on-tick`, and `to-draw`.
1. Extend the definition of the world to include a String that is one of `"red"`, `"yellow"`, or `"green"`. Modify `world-draw` to use this String to determine the color of the circle we draw when the user clicks. Also write a function `cycle-color` to cycle through the colors: `"red"``"yellow"``"green"``"red"`. Using this function, modify `mouse-click` to cycle the color when the user clicks.

## That's All Folks

If you had trouble finishing any of the exercises in the lab or homework—or just feel like you're struggling with any of the material—come to office hours to talk to a TA or a tutor for extra help.