For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and send me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Execute button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and send me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
If you need help with the functions that require images or animations,
please see the
documentation for the tiles-world
teachpack.
Develop a function named
counterchange
which takes in two images and returns a 2x2
arrangement of them, as shown at right. (Click on the image for a
full-scale view.)
Develop a function named checkerboard
which takes in two colors (e.g. strings) and returns a 2x2 arrangement
of them, as shown at right. Hint: This is extremely
easy if you re-use a previously-written function!
You and your friends want to go on a weekend road-trip, but nobody has a car. You can rent a car with unlimited mileage for $30/day plus a fixed $10 processing fee, but you have to pay for the gas yourself: the car gets 30 miles to the gallon, and gas costs $2.10 a gallon. In addition, you're planning to stay in motels for $40/night; note that if you go away for Friday, Saturday, and Sunday (3 days), you actually only need motel rooms for 2 nights.
Develop a function named trip-cost
which takes in the number of
days you expect to be away, and the number of miles you expect to drive,
and tells you how much it'll all cost. For example, if you went for 3
days and drove 330 miles, it would cost $100 to rent the car, $80 to
stay in motels, and 11 gallons of gas, which costs $23.10, for a total
of
$203.10.
Hint: This can be done as one big function, but please do it with several auxiliary functions instead. As always, follow all the steps of the design recipe for your auxiliary functions too.
Hint: For readability and modifiability, I
recommend defining variables to represent the numbers that are fixed as
part of the problem, e.g.
(define CAR-COST-PER-DAY 30)
(define FUEL-COST-PER-GALLON 2.10)
etc.
How much of your program would you need to change if the car's fuel efficiency were improved to 33 miles/gallon? For example, if you went for 3 days and drove 330 miles, it would still cost $100 to rent the car, $80 to stay in motels, but only 10 gallons of gas, which costs $21.00, for a total cost of $201.00. (Make the change to your program, try this example, and make sure it produces the correct answer.)
Do (but don't turn in) some of the exercises in sections 4.1, 4.2, and 4.3, as many as you think you need in order to feel comfortable with booleans and conditionals.
A carpet store needs a function to compute how much to charge
its
customers. Carpeting costs $5/yard, but if you buy 100 yards or more,
there's a 10% discount on the whole order,
and if you buy 500 yards or more, the discount becomes 20% on the whole
order. Develop a function carpet-price
which
takes in the number of yards of carpeting and returns its total price.
Three candidates (Anne, Bob, and Charlie) are running for mayor
of Schemeville, which, by court order, has a new computerized voting
system.
Develop a function named
"who-won
" which takes in three numbers (the
number of votes for Anne, the number of votes for Bob, and the number of
votes for Charlie, respectively) and returns a string indicating the
name of the person who won -- either "Anne", "Bob", or
"Charlie".
For example,
(who-won 7 4 5) "should be" "Anne" (who-won 5 11 36) "should be" "Charlie"For now, you may assume that there are no ties.
Develop a function within-distance?
that takes in two posns and a number, and tells whether the posns are
within that distance of one another.
Hint: It may help to write an auxiliary function named
distance
which computes the distance between two posns.
This is computed by the formula:
sqrt( (x1-x2)2 + (y1-y2)2 )
where x1 and y1 are the coordinates of one of the posns, and x2 and y2 the coordinates of the other.
There is a predefined Scheme function named random
that
produces random numbers. Read about it in the DrScheme
Help Desk. Try some examples in the Interactions
window.
Develop a function named random-posn
that
takes in two numbers (max-x and max-y) and returns a
posn whose x coordinate is chosen at random between 0 and
max-x,
and whose y coordinate is chosen and random between 0 and
max-y.
Note: In your examples, you won't be able to predict
exactly what answer comes out, but you can say something like
"should be a posn with 0 ≤ x < 20 and 0 ≤ y < 50".
add-colored-dot
which takes a posn, a color, and an image, and returns the same image
with
a dot of the specified color added at the specified location.
You may assume that the given image has its pinhole at the top-left.
(An easy way to construct such images is with the
empty-scene
function in the tiles-world teachpack. It
takes in a width and height, like rectangle
, and produces
a solid white rectangle of those dimensions with the pinhole at the top
left corner.
In the example at right, I've used the nw:rectangle
function in the tiles-world teachpack. It works like
rectangle, but puts the pinhole at the top left
rather than in the center. If you get an error message from using
this function, try re-installing the latest version of tiles-world.plt.)
add-smart-dot
which
takes an image (background), a posn (center), a number
(radius), and another posn (new-point), and returns the
same image with either a green dot or a red dot added at position
new-point;
the dot should be green if it is within radius of
center, and red otherwise.
add-random-smart-dot
which takes an image (background),
a posn (center), and a number (radius),
and returns the same image with a randomly chosen dot added to it,
green or red depending on whether it's within radius of
center, as above.
show-world
function is incredibly easy); it starts
with a box, and at each tick of the clock, it updates the world using
add-random-smart-dot
. Turn in not only
the
Scheme code you used to run the animation, but also a screen shot
showing the animation after at least fifty clock ticks.
Error log: /30
(I'm not grading on how many or how few errors you encountered,
only on whether you recorded them adequately.)
Function name | Contract | Examples | Definition | Test results |
---|---|---|---|---|
counterchange |
/5 | /5 | /10 | /5 |
checkerboard |
/5 | /5 | /10 | /5 |
trip-cost & auxiliaries What would you need to change? |
/15 | /15 | /30 | /15 |
/5 | ||||
carpet-price & auxiliaries, if any |
/10 | /10 | /20 | /10 |
who-won |
/5 | /5 | /10 | /5 |
within-distance? |
/5 | /5 | /10 | /5 |
random-posn |
/5 | /5 | /10 | /5 |
add-colored-dot |
/5 | /5 | /10 | /5 |
add-smart-dot |
/5 | /5 | /10 | /5 |
add-random-smart-dot |
/5 | /5 | /10 | /5 |
animation | /20 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |