CS U212 - Lab 4

The lab assignment is to be completed in pairs. Each pair works on one computer. You may not work with a partner with whom you have previously worked this semester. Change roles after each exercise! Never forget to apply the design recipe!

Part I: Last week on CS U212

Exercise 1: In this class, each lab section consists of a lab number, a head TA, a TA who assists with the lab, and an undergraduate tutor (let's assume that all the lab sections only have a single tutor for the purposes of this exercise). Design a data definition and define a data structure for representing lab sections. Give three examples of lab sections. Develop a program that consumes a lab section and returns a descriptive string similar to: "CSU212 Lab 5 - Ben Chambers, Stevie Strickland, Julianne Emmons"

Exercise 2: Develop a function that computes the result of the formula (a+b)2/(a-b)2. Give an example of using the function. Then write down the evaluation steps in the Definitions window. Afterwards, use the stepper to compare the way you and DrScheme evaluated the formula. Unless you have made a mistake, both ways of calculating the result of the formula should give the same vaule.


Change Partners - Administrivia


Part II: This week on CS U212 - Structures and Unions

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

Exercise 3: 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.

Exercise 4: A studio album has a name and a year of publication. A live album has a name, a year of recording and a year of publication. A music album is either a live or a studio album. Write only one data definition to describe a music album. Produce two examples of a music album (one for each kind). Write a template for a function that consumes a music album.

Exercise 5: There are two kinds of unpublished music albums from the perspective of a music label -- those that are completed yet and those that aren't. Both completed and uncompleted albums have a serial number, but those albums that are completed also have a name and a release date. Design a data definition to describe an unpublished music album. Produce two examples of an unpublished music album (one for each kind). Write a template for a function that consumes an unpublished music album.

Exercise 6: Develop a function that consumes an unpublished album and a serial number to check whether the album matches the serial number.

Exercise 7: Develop a function that consumes an unpublished music album, a date of release and a name. If the album is not completed it constructs an instance of a completed album using the information given as input to the function.


Part III:Extras - The World

In the following exercises, the world should be represented as a struct with two posns. 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.

Exercise 8: Design a function mouse-click to react to mouse events. It consumes a World, two numbers (x and y coordinates), and a MouseEvent as described in on-mouse-event. When the MouseEvent is 'button-down, this function should return a new World where the second posn (the blue circle) has been moved to the position of the mouse click, and the first posn remains unchanged. On any other mouse event, the original World is returned unchanged.

Exercise 9: Design a function tick-tock to react to clock events. The purpose of the function is to gradually equate two posns. The function consumes a World and produces a new World where the coordinates x and y of the first posn are increased or decreased by one so that they get closer to the coordinates of the second posn of the original World. For example if the original World is (1,3),(5,1) then your function should return the new World (2,2),(5,1).

Exercise 10: Design a function world-draw that consumes a World and returns a 300-by-300 scene with an 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. The red circle should appear on top of the blue circle when appropriate.

Exercise 11: Create an animation where you put a red circle somewhere in the canvas, using the mouse, 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 the way you initialize the World.

Exercise 12 (Extra Challenge): This exercise asks you to modify the animation you made earlier, and provides some guidance for doing so. We highly suggest that you do this problem, but do not expect there to be enough time during for you to finish it. If you don't we suggest you try to complete it at home. Extend the definition of the world to include a symbol that is one of 'red, 'yellow, 'green. Modify world-draw to use this symbol when drawing the circle for the mouse. Write a function cycle-color to cycle through the colors ('red becomes 'yellow, 'yellow becomes 'green, 'green becomes 'red). Use this function and modify mouse-click to cycle the color (and make no other changes) when the user right-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, please feel free to come to office hours and talk to a TA or tutor for additional assistance.