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.
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.
Do exercise 4.4.3 in the textbook (the one about the credit card refund). As usual, watch for auxiliary functions and named constants. To be sure you understand the problem, work through the examples given in the problem by hand; don't start trying to write the function until you understand why those examples come out the way they do.
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.
Load the draw.ss teachpack and play with the built-in drawing functions: start, draw-solid-disk, draw-solid-rect, draw-circle, draw-solid-line, etc. Draw something interesting, and turn in the Scheme code you used to draw it (whether in Definitions or in Interactions).
Develop a function named draw-dot
that
takes in a posn
and a symbol (for color) and draws a small dot
of the specified color centered at that position.
Test it both on posns that you specify and by
composing it with wait-for-mouse-click
.
(Since these tests are graphics-based,
there's no "test run" to turn in; I'll test them myself interactively.)
Do supplementary problem 6.7. Note that the function assumes that certain variables, like WIDTH, HEIGHT, BAR-WIDTH, X1, etc. are defined; make sure you define all of these to suitable numbers in your definitions window.
Try to write this in such a way that
Develop a function within-radius?
that takes in two posns and a number, and tells whether the posns are
within that distance of one another. (Note: the
function does not draw any pictures on the screen. It will
help to write the distance
function first, taking two posns
and returning the distance between them as a number.)
Test your function, first by plugging in known posns and numbers for which you know the right answer. Once you've got it working in that context, test it graphically:
(start 500 300) (define center (make-posn 100 60)) (draw-solid-disk center 40 'green) (within-radius? center (wait-for-mouse-click) 40) (within-radius? center (wait-for-mouse-click) 40) (within-radius? center (wait-for-mouse-click) 40) (within-radius? center (wait-for-mouse-click) 40) ; and so onEach time you call
wait-for-mouse-click
, the computer will
wait for you to click the mouse in the graphics window. If you click
inside the disk, it should return true
; otherwise
false
.
(Since this test is graphics-based,
there's no "test run" to turn in; I'll test it myself interactively.)
Develop a function in-box?
that
takes in two posns and two numbers. The first posn is a location on the
screen that you want to know about; the second is the top left corner of
a rectangle; the first number is the width of the rectangle; and the
second number is the height of the rectangle. Your function should tell
whether or not the first posn is inside the rectangle specified by the
other three parameters.
Hint: You'll need a lot of test cases, because there
are a lot of different reasons a posn might be outside the box: too far
right, too far left, too far up, too far down....
Again, test your function first by plugging in known posns and numbers for which you know the right answer, and then graphically:
(start 500 300) (define top-left (make-posn 100 60)) (draw-solid-rect top-left 80 50 'green) (in-box? (wait-for-mouse-click) top-left 80 50) (in-box? (wait-for-mouse-click) top-left 80 50) (in-box? (wait-for-mouse-click) top-left 80 50) (in-box? (wait-for-mouse-click) top-left 80 50) ; and so on
Note: I originally wrote the above examples with the two posns in the wrong order. I apologize. The above example should work with the function as I assigned it.
Since this test is graphics-based, there's no "test run" to turn in; I'll test it myself interactively.
Develop a function named shift-posn that takes in a posn and two numbers x-difference and y-difference and returns another posn just like it but with the x and y coordinates increased by x-difference and y-difference respectively.
Develop a function named
draw-dot-right-color
which takes in a posn
(the location of the dot), another
posn
(the center of a circle), and a number (the radius of
the circle); it draws a dot at the specified location, green if it's
inside the circle and red otherwise.
Test this function, first with known posns and numbers, and then graphically:
(start 500 300) (define center (make-posn 100 60)) (draw-circle center 40 'blue) (draw-dot-right-color (wait-for-mouse-click) center 40) (draw-dot-right-color (wait-for-mouse-click) center 40) (draw-dot-right-color (wait-for-mouse-click) center 40) (draw-dot-right-color (wait-for-mouse-click) center 40) ; and so on
Write a function named random-posn that takes in two numbers, width and height, and returns a posn whose x coordinate is chosen randomly between 0 and width, and whose y coordinate is chosen randomly between 0 and height.
Note: you'll need to use the built-in random
function,
which takes in a natural number and returns a randomly chosen number
from 0 up to (almost) that number. Play with this for a while before
trying to write random-posn
.
Test this function first interactively, and then
graphically, replacing the calls to wait-for-mouse-click
in
the above with calls to random-posn
.
Error log: /40
Function name | Contract | Examples | Definition | Test results |
---|---|---|---|---|
carpet-price |
/5 | /5 | /10 | /5 |
4.4.3 pay-back |
/5 | /5 | /10 | /5 |
who-won |
/5 | /5 | /10 | /5 |
Something fun with graphics | /5 | /5 | /10 | /5 |
draw-dot |
/5 | /5 | /10 | /5 |
draw-bar-chart |
/10 | /10 | /20 | /10 |
within-radius? |
/5 | /5 | /10 | /5 |
in-box? |
/5 | /5 | /10 | /5 |
shift-posn |
/5 | /5 | /10 | /5 |
draw-dot-right-color |
/5 | /5 | /10 | /5 |
random-posn |
/5 | /5 | /10 | /5 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |