CS U212 - Lab 4

The lab assignment is to be completed in pairs. Each pair works on one computer. Change roles after each exercise! Never forget to apply the design recipe!

Part I: Last week on CS U212

Exercise 1: A typical rock band has a name and consists of a singer, a guitarist, a bassist, and a drummer. Design a data definition and define a data structure for a rock band. Give three examples of rock bands. Develop a program that consumes a rock band and concatenates the name of the band and the names of the band's members. For example, "Led Zeppelin: Robert Plant, Jimmy Page, John Paul Jones and John Bonham"

Exercise 2: Develop a funtion that 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 Definitins 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 value.


Change Partners - Administrativia


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 not completed yet and the only available information about them is their serial number and those that are already completed and so they can be described by their serial number, their name and their 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 and if the album is not completed it constructs an instance of a completed album using the information given as input to the function.


Quiz


Part III:Extras - The World

Hint: For the following exrcise represent the World as a struct with two Posns.

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 returns a new world wich consists of the first Posn of the original World and the x and y coordinates of the mouse click as the second Posn. Any other time, it produces the original World 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 it produces a new World where the coordinates x and y of the first Posn are increased or dicreased 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 a blue circle of radius 10 at the position represented by the first Posn of the world and a red circle of radius 10 at the position represented by the second Posn.

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.