Due: Wednesday, January 14, 2009 at 500pm
The goal of this problem set is to help you design domain-specific
functions, functions that deal with enumerations and unions, functions
that deal with struct
s, and functions that have it all.
Remember that you must follow the design recipe. Your deliverables include the data analysis, contract and purpose header, code, and tests. (Remember that there is no deliverable corresponding to the template.)
All problems are from HtDP unless otherwise specified.
Note 1: You must use DrScheme's HtDP Beginning Student Language to solve the problems.
Note 2: Changes to "world constants" in the solutions should have no side effects on the workings of the test cases or the workings of the program in problem 2.
A falling object accelerates at 9.8 meters per second per second like this:
after t = 0 1 2 3 4 ... 10 ... 15 seconds it has traveled 0.0 4.9 19.6 44.1 78.4 ... ?? ... ?? meters
Your first task is to design the function how-far
, which,
given a number of seconds, computes how far the object has fallen.
Hint: Guess a formula for calculating how far the car has gotten in t seconds. Once the formula works for the first five entries, use it and DrScheme's Interactions Window to fill in the two boxes with ?? in the above table.
Your second task is to design the function to-image
,
which, given a number of seconds, creates a scene (see
World
teachpack) of a circle whose center has fallen
as many pixels from the top edge of the scene as how-far
dictates. The circle must be tangent to the left edge of the scene.
Hint: You should tabulate the first few elements of this
function just like we tabulated the first few elements of
how-far
for you. Paper and pencil is enough; no need to turn
in this table.
A traffic light that is "off" should be rendered as three colored circles; if one of the bulbs is supposed to be "on", render the corresponding position as a solidly colored disk. For simplicity, make each phase last one second. Make sure that you can change the size of the traffic light with a single change to your program.
Modify the program so that when the viewer presses the space key (key
event: #\space
) the traffic light goes into the fail-safe
state of displaying a blinking red light. That is, every other second the
red light is on; otherwise all lights are "off." Pressing the space key
a second time return the traffic light to its regular function. --
Clearly indicate which portions of the initial solution you had to change.
Domain knowledge: If you do not know how traffic lights are arranged or in what order they work in this country, observe the light on Huntington for a couple of minutes.
(define-struct enum3 (li1 li2 li3))
;; Enum is one of:
;; -- (make-enum3 String String String)
;; interpretation: an enum3 is a list of exactly three items.
;; Style is one of:
;; -- 'number
;; -- 'roman-numeral
;; -- 'letter
;; -- 'solid-dot
;; -- 'open-dot
Think of these enum
as something like an HTML enumeration,
ordered (ol) or unordered (ul).
The rendering program consumes an enum
and
a style, and produces an image consisting of the items in
one of the five styles.
Here are three examples.
the code | produces |
---|---|
(enum3-to-image (make-enum3 "hey" "you" "dummy") 'number)
|
|
(enum3-to-image (make-enum3 "hey" "you" "dummy") 'open-dot)
|
|
(enum3-to-image (make-enum3 "hey" "you" "dummy") 'solid-dot)
|
Domain knowledge: The rendering inserts exactly one "string space" between the "enumeration anchor" (number, bullet, circle) and the text of the item. Text is rendered as 11pt and black.
The purpose of this exercise is to design a program that computes the movement of a ball under the influence of gravity. The ball starts in the upper-left corner of the scene with a fixed horizontal velocity and zero vertical velocity. The ball is accelerated vertically by gravity, but maintains constant horizontal velocity, as dictated by Newton's laws.
The ball continues until it reaches the bottom of the scene, where it takes a perfect bounce. Here is an example:
Design a data representation for the ball.
Design the function next-location
, which computes the next location of
the ball assuming it travels for one tick.
Use the World teachpack to create an animation of the ball.
Domain knowledge: Velocity consists of two numbers in a 2d space: one for how far the ball travels horizontally, one for the vertical direction. Acceleration consists of one number, which alters the velocity at each tick. A perfect bounce means that the ball travels to the wall and is then reflected at exactly the same angle as it hits the wall. This reflection is just a negation of the vertical part of its velocity. From this bounce, it continues its trip for the rest of the distance as if nothing had happened.
Last modified: Mon Jan 05 16:02:33 Eastern Standard Time 2009