Assignment 2
Due Dates Friday 1/19 and 9:00pm and Tuesday 1/23 at 9:00pm
Submissions: There are two assignments available on the handin-server that you can submit to: be careful to submit the correct answers to the correct assignment.
Submit your tests (check-expects) to the first three graded exercises by Friday 1/19.
Submit your complete solutions to all of the problems by Tuesday 1/23. (Do include check-expects for all of the functions as well).
Purpose It is time to graduate from programming to program design.
Finger Exercises
Exercise 1 (Exercise 73 from HtDP) Design the function posn-up-x, that consumes a Posn p and a number new-x, and produces a new Posn whose x-coordinate is the given new-x value.
Exercise 2 Design move-linear, which consumes three numbers, p representing some position (e.g. the x- or y-coordinate of something), v representing a velocity in that coordinate, and dt an amount of time, and produces a number representing the new position after dt time has elapsed. (An example in English, to get started: “A car starts at x = 50 feet, and is moving at v = 20 feet/second. Where will it be after dt = 2.5 seconds?”)
Exercise 3 Design the world program control. Its task is to move a red dot—
as in (circle 3 "solid" "red")— on a 200-by-200 canvas in response to the left and right arrow keys. Every time the user presses the left arrow key, the dot moves left by 5 pixels; when the user presses the right arrow key, the dot moves right by 8 pixels. The dot does not respond to any other keyboard input. The main function consumes the initial x coordinate of the dot.
Graded Exercises
Exercise 4 The function rocket-land consumes a y-coordinate of a rocket and returns the y-coordinate of the rocket after one tick of the clock. If the given value of y is between 0 and 300, it increases the y-coordinate by 10 pixels. If the value of y is between 300 and 400, it increases the y-coordinate by 5 pixels. If the value is between 400 and 550, the y-coordinate is increased by 1 pixel. If the y-coordinate is greater than 550, there is no change to the y-coordinate.
Write a data definition for the y-coordinate of the rocket.
Design the function rocket-land.
Exercise 5 Design amount-of-ticket. Given the speed of a car and the speed limit of some road, the function determines the penalty a driver has to pay. Here is the table printed on the back of such speeding tickets:
Percentage of Excess Speed
Amount ($)
up to 10
50
up to 25
150
up to 100
400
beyond
2500
Hint: you will need a data definition for speed levels.
Exercise 6 Design move-ball, which (intuitively) computes the result of a ball moving in a straight line for a period of time. The function consumes a Ball-2d b and an amount of time dt. To create the resulting Ball-2d, both the x- and y-coordinates move linearly, and the velocity is unchanged.
Exercise 7 The drawing of a chameleon in Section 5.11 of HtDP/2e is a transparent image. To insert it into DrRacket, copy and save the image from HtDP/2e and insert it with the "Insert Image" menu item. Define the image with the name CHAM. Using this instruction preserves the transparency of the drawing’s pixels.
When a partly transparent image is combined with a colored shape, say a rectangle, the image takes on the underlying color. In the chameleon drawing, it is actually the inside of the animal that is transparent; the area outside is solid white. Try out this expression in your DrRacket:
(overlay CHAM (rectangle (image-width cham) (image-height cham) "solid" "red")) Design a world program that has the chameleon continuously walking back and forth across the screen (you should rotate the image 90 degrees so it is facing right). It starts walking from left to right and when it reaches the right end of the screen, it flips horizontally and begins walking from right to left and switches direction again when it reaches the left boundary.
Of course, like all chameleon’s, ours can change color, too: the key "r" turns it red, "b" blue, and "g" green.
Start with a data definition, VCham, for representing chameleons. You will need to represent the position, color and direction of the chameleon in your definition.
Here is a wish list for the functions you will need:
draw-cham – renders the world as a chameleon facing left or right
next-cham – compute the new location of the chameleon in one clock tick from now
change-cham – changes the color of the chameleon if the "r", "g" or "b" key is pressed