yourLastName-hw1
(see "What to Turn In", below).
In the Interactions window, type in the number 7, then hit the Enter key. DrRacket gives you back the number 7. This is an example of an atomic expression. We can also write compound expressions, which consist of a left parenthesis, an operator, some more expressions (either atomic or compound), and a right parenthesis. Here are some examples, each conforming to the definition of "expression" by repeatedly applying the two definitions given above. Type each expression into the Interactions window and make sure you understand the results returned by DrRacket.
7
5
(+ 7 5)
(- (+ 7 5) 3)
(* (- (+ 7 5) 3) 2)
(In the third expression, the operator is +, and the two arguments required by the + operator are two numbers, 7 and 5.)
In class on Thursday and Friday we played with some built-in functions in the image library. Add the following line to the Definitions window:
(require htdp/image)Now click the Run button. The functions in the image library are now available to your program.
From the Help menu, choose Help Desk. A browser window will open.
Under the Languages heading, choose Teachpacks.
Click on entry number 1.1 (image.ss). Here
you will find a description of all the pre-defined functions (operators) in the
image.ss
library. Notice the search box at the top of the page. Type in
image-width
to find information about that operator. When the DrRacket Helpdesk provides the matches for image-width
, choose the one from "htdp/image".
Here is an expression that will display a solid red circle of radius 25:
(circle 25 "solid" "red")
Notice that this conforms to the definition of a compound expression. (The operator is circle. The three arguments supplied to the circle operator are a number, 25, and two strings, "solid", and "red".)
In the Interactions window, write a compound expression that will display two solid circles stacked on top of each other. Both circles should have a radius of 25. The circle on the top should be red, and the circle on the bottom should be yellow. (Hint: look at the definitions of the operators circle and overlay/xy in the help desk).
In the Interactions window, write a compound expression that will display three solid circles on top of each other, the top one being red, the middle one being yellow, and the bottom one being green. Use a radius of 25 for all three circles.
As you answer each of the remaining problems, use comments in your file to clearly indicate the number of the problem you are solving.
;; traffic-light: string string string -> image ;; consumes three strings representing the colors of the top, middle, and ;; bottom lights and produces an image of a traffic light in those colorsCopy the contract and purpose into the definitions window. Now write the function definition for traffic-light. (Hint: here is the first line of the function definition, with parameter names top, middle, and bottom:)
(define (traffic-light top middle bottom)
Hit the Run button. If DrRacket reports any errors, try to figure
out what's wrong, make corrections, and hit the Run button again. In the
Interactions window, call the function traffic-light
to create an
image of a traffic light. Call the function with arguments "chartreuse", "magenta", and "brown".
Define constants that represent the price of each food item, according to this price list:
hot dog 2.95 taco 1.50 fries 1.50 burger 3.95 salad 5.00 pizza 12.00
share-pizza-cost
that consumes the number of slices eaten
by one person and determines that person's share of the cost of a pizza.
For this and all remaining problems, make sure you write a contract and purpose for each function.
price-with-tip
that will add a 15% tip to a given number. (Write a contract and a purpose for your function.)
share-with-tip
that consumes the number of slices of
pizza eaten by one person and computes that person's share of the cost of a pizza, including the cost of the tip. Use the functions you defined in Problems
4 and 5.
add-side
that consumes two images
(for a meal item and a side-dish item), and produces an image
similar to this, picturing both the meal item and
the side item:
Don't forget to document the function with a contract and purpose.
A waiter working at the pizza shop can stack two items in his
arm when he delivers an order to a table. Of course, he knows how to balance
the items so they don't fall over.
Write a function stack-two
that consumes images
of two items and returns an image with the second item just above the
first one. To ensure that the stack of items is balanced, you should center the items one above the other, like this:
The images must be centered regardless of their sizes (in other words, don't assume that the images supplied as inputs to the function are of any particular height or width).
[HINT: It's difficult to know if your function is working correctly if you test your function only with the images supplied in this assignment, since they are all of similar width and height. I suggest testing your function by stacking (and centering) two images of your own creation of obviously different dimensions.]
Note that DrRacket has a function max
that consumes two
numbers and produces the larger of the numbers. You should also be using
the image-width
and image-height
functions provided with the image.ss
teachpack.
Is your function getting too complicated? Should you decompose the problem into multiple functions?
Using web-based turnin (see note below), turn in a single file containing all code and documentation for this assignment. Name your file
yourLastName-hw1
For example, if your name is Jane Doe, you would name your file doe-hw1
.
In addition, your name and your wpi ccc username must be listed in a comment at the top of your file. Programs will not be accepted for submission after
5pm on Tuesday, August 31.