CSU2500 Lab 2

Electronic Homework Submission

Directions for homework submission.

Pair programming

In this lab, we will practice pair programming again. You will be working with your partner from last week. Like last time, each pair should only be working on one machine. Remember, in pair programming, one member of the team is the pilot and the other is the co-pilot. The pilot does the typing and the co-pilot makes sure that the pilot is typing the right thing. Even though the pilot is the only one typing, both partners should be active in trying to come up with solutions to the exercises. Make sure to switch roles when indicated in the lab!

Testing

In this lab, you must write tests for all of your functions. Use the built-in check-expect form to test your code. You should write enough tests for each function such that all branches of your code are executed. When you press the "Run" button in DrScheme, DrScheme will tell you whether there are any failing tests.

Stepping through functions

Exercise 1: Design a function that consumes a quantity (a nonnegative integer), and returns a string representing it in unary (base 1). In unary, an integer is represented by a number of symbols (the letter "I", for this exercise) corresponding to the value of the number. For instance, the number 2 would be represented by "II" and the number 5 would be "IIIII". To make these numbers readable, add the number in decimal form after its unary form. Thus, 2 would be represented as "II (2)" and 5 as "IIIII (5)". Look up the functions number->string, make-string, and string-append in the Help Desk. (if you are having trouble accessing the Help Desk, you can also get it on the web at http://docs.plt-scheme.org/)

After you are done writing your function, come up with some tests for the function. You should use the check-expect function to test your code. Press the "Run" button in DrScheme and make sure all your tests pass before moving on to the next exercise.

Exercise 2: Design a function that consumes two quantities (nonnegative integers), and produces a string illustrating how their unary representations add up (with the decimal form present as well). For instance, if given 2 and 4, it should produce the string "II (2) + IIII (4) = IIIIII (6)". You should use the program you wrote for Exercise 1! If you don't, your code will be unreadable, and take forever to write.

Don't forget to write tests for your function.

Exercise 3: The Stepper is a DrScheme tool that shows every step a program takes. Click on the Stepper button, the one labeled "Step" with a picture of a foot, to the left of the Run button. Use the Stepper on your programs from Exercise 1 and 2. Figure out what happens in the Stepper when one program calls another program.

Switch Roles


Conditional programs

Exercise 4: Design a function that calculates late fees on a movie rental. The function consumes the number of days the movie has been rented. Up to 3 days is a regular rental, no fee. For the next week the fee goes up $3 each day. Starting on the 10th day (i.e., a week late) there is a flat $20 fee that never changes. Write tests for your function when you are done. Make sure to try out all the branches in your code.

Exercise 5: Run the Stepper on your program from Exercise 4. Figure out what happens in the Stepper when the program makes a conditional decision.

Switch Roles

Exercise 6: Design a function that calculates sales tax. The function consumes the sale price and the tax percentage (as a decimal or a fraction) and produces the final price. For instance, if given 20 and 5/100 it should compute 105% of $20, or 21.

Exercise 7: Design a function that calculates conditional sales tax: only prices $100 or more are taxed. The function consumes a sale price and a tax percentage and produces the final price. For instance, if given 20 and 5/100, it computes $20 (no tax). But if given 200 and 5/100, it computes $210 (5% tax). Hint: Use your program from Exercise 6.

Did you write tests that covered all the branches of your code for exercises 6 and 7?

Exercise 8: Run the Stepper on your program from Exercise 7. Figure out where the Stepper calls your program from Exercise 6. Try it with values both over and under $100.

Switch Roles


Playing with Posns

Posns are a kind of data provided by DrScheme that represent a position on a plane. As you would expect, a position on a plane is represented using two numbers, one for the value of the x axis, and the other for the y axis.

To create a Posn and use it in your program, you must use the make-posn function. Use the help desk to find out how to use it. Once you have a Posn, you will likely want to know what its x and y values are, to pass them to another function, for example. To do so, use the posn-x and posn-y functions, which give you the x and y values of the Posn, respectively.

Exercise 9: Design a function place-circle that consumes a Posn and produces a 300-by-300 scene with a red circle of radius 10 at the position represented by the Posn. For instance, if given (make-posn 10 290) it should produce a scene with a red circle in the lower left corner.

Switch Roles


Mouse clicks

In this part of the lab, we will create an interactive animation. Add the "universe.ss" teachpack in DrScheme, if it's not there already. We will use the big-bang function. We've seen how to use the on-draw and on-tick clauses before. Look them up in Help Desk if you need to remind yourself how they work. Now we have one more function for reacting to the mouse pointer. Look up on-mouse in Help Desk. Just like on-draw and on-tick, we have to give a function to on-mouse. Find out what this function's inputs and outputs mean, and what a MouseEvent is.

Exercise 10: Design a function mouse-click to react to clicks. It consumes a World, two Numbers (x and y coordinates), and a MouseEvent, as described in on-mouse. In this lab, a World is a Posn (make sure to write this in your program). When the MouseEvent is "button-down", this function produces the x and y coordinates of the mouse click as a Posn. Any other time, it produces the original World unchanged.

Exercise 11: Put this function and the above place-circle function together to make an animation. Add the following expression to your Definitions window, hit Run, and try clicking in the new window that opens.

(big-bang (make-posn 0 0)
          (on-draw place-circle)
          (on-mouse mouse-click))

If you are done early, try playing around with different MouseEvents and see what happens. As a challenge, make it so that the clicking the mouse creates an expanding circle at the cursor's position. (You won't be able to handle multiple circles at once just yet.)