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 but the co-pilot drives the process. 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!
In previous labs some students deleted their solutions for exercises after completing them—don't do this! It is common for exercises to make use of functions or templates defined in earlier exercises.
2would be represented by the string
"II"and the number
"IIIII". To make these numbers more readable, we will also display the number in decimal after its unary form. Thus,
2will be represented as
"IIIII (5)". The functions
string-appendwill be helpful hereŚlook them up in the Help Desk.
4, it should produce the string
"II (2) + IIII (4) = IIIIII (6)". Reuse the program you wrote for the previous exercise. If you don't, your code will be unreadable and take forever to write.
(+ (sqr 2) (* (round 3.2) (- (sqrt 4) (/ 42 6)))) (place-image (circle 10 "solid" "black") 70 70 (place-image (circle 10 "solid" "black") 30 70 (place-image (rectangle 80 40 "solid" "red") 50 50 (empty-scene 100 100))))As far as DrRacket is concerned, what's the difference between an image and a number?
5/100it should compute
21(105% of $20).
5/100, it computes $20 (no tax). But if given
5/100, it computes $210 (5% tax). Hint: reuse your program from the previous exercise!
Posns are a kind of data provided by BSL 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.
make-posn function creates a posn. 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
which give you the x and y values of the posn,
(require 2htdp/image) to the top of your file so we can use
image functions like
place-circlethat consumes a posn and produces a 300 × 300 scene with a red circle of radius 10 at the position represented by the posn. For instance, if given
(make-posnit should produce a scene with a red circle in the lower left corner.
In this part of the lab, we will create an interactive animation. Add
(require 2htdp/universe) to the top of your file so we can
big-bang and friends. We've seen how to use the
before. Look them up in the Help Desk if you need to remind yourself how they
work. Now we have one more function for reacting to the mouse pointer. Look
on-mouse in the Help Desk. Just like
on-tick, we have to
give a function to
on-mouse. Figure out what this
function's inputs and outputs should be, and what a MouseEvent is.
mouse-clickthat reacts to mouse 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 note 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.
place-circle, let's create an animation. Add the following expression to your Definitions window, hit Run, and try clicking in the new window that opens.
0) (to-draw place-circle) (on-mouse mouse-click))
If there is time left, try playing around with different MouseEvents to see what you can do with them. See if you can create an animation where...
Be creative, have fun with it!