| Due date: 9/24 @ 11:59pm
The goal of this problem set is to practice designing data representations
using structures and functions for processing such forms of
data. Furthermore, you must follow the design recipe and the guidelines
when you develop functions.
Problems A1
Consider the following structure definitions:
(define-struct lecture-hall (number capacity))
(define-struct automobile (year make model))
(define-struct football-player (name position number))
(define-struct highway (lanes speed))
(define-struct shirt (material size color))
-
What are the names of the constructors and the selectors that each of
the structures adds to Racket? Draw box representations for each of these
structures.
-
Provide data definitions for the structure definitions above. Make appropriate assumptions about
what data goes with which field.
-
Develop templates for functions that consume the structures above.
Problem A2
Here is a data definition for keeping track of time on a clock
(which, of course, is just a computer with a display attached to it):
(define-struct time (hours minutes))
;; Time = (make-time Number Number)
;; Constraints:
;; The first number is always between 0 and 11;
;; the second number is always between 0 and 59.
Use these definitions for the following exercises:
-
Design the function
tock from Time to Time .
It adds one minute to the given time.
-
Design the function time->text, which converts a time to a
text. The text should look like the display of a common alarm clock,
i.e., it should separate the minutes from the hours with a colon. Hint: a
text is an image, not a string, but you will need a string
version of the time, too. See HelpDesk (image.ss) for more on the
text function.
- Optional: After you have developed these functions, you may wish to add the
following lines to your program:
(big-bang (make-time 0 0)
(on-tick tock 0.1)
(to-draw time->text))
If you do, be sure to delete them before you turn in
your solution. We will subtract points if you leave them in.
Problem A3
You've found a summer job with the pyschology department.
They'd like you to develop a program that helps people
control the movement of a ball across the screen. (They
wish to study brain models and reaction times and this is
just a small part of it.)
The ball can move on straight lines only, that is, up, down, left, or
right. Pressing the arrow keys on the keyboard changes the ball's location.
-
Develop a data representation for the current position
of the ball. The position is best described with a pair of
positive integers.
-
Develop a data representation for velocity of the ball.
You may assume that the ball always moves exactly 10 pixels
at a time but remember that velocity also includes the
direction of the movement.
-
Develop a data representation for the ball.
-
Design the function
ball-next , which consumes (the
representation of) a ball and create a ball that represents where it
will be after one "tick."
-
Design the function
ball-image , which consumes
(the representation of) a ball and produces a rectangle of 300 x 300
pixels with a red dot (diameter 10 pixels) placed at the ball's
position.
-
Design a function ball-change , which consumes a ball
and a "key event," which represents the user hitting a key on the
keyboard. If the key event represents the user hitting the
up-arrow key, then ball-change produces a new ball which is
just like the input ball, except that the new ball is moving upward;
similarly for key events representing the left-, right- and down-arrow
keys: they cause the resulting ball to be one that is moving left,
right, or down, respectively.
Passing any other keystroke to ball-change
causes the ball to be unchanged.
You can read about key events in the help desk—the
on-key clause of big-bang is a good
place to start.
- Optional: After you have developed these functions, you may wish to add the
following lines to your program:
(big-bang some-initial-ball
(on-tick ball-next 1/28)
(to-draw ball-image)
(on-key ball-change))
If you do, be sure to delete them before you turn in
your solution. We will subtract points if you leave them in.
|