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 (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 and turning it in.
If you think any of the assignments are ambiguous, e.g. it's not clear what value to use in a borderline case, ask me. Failing that, think about what answer makes the most sense, and then tell me your interpretation (and why you chose that way) in a comment.
This assignment is to be done in pairs, just like previous homeworks, but with a different partner.
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.)
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....
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
that
takes in a posn
and draws a small green dot 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.)
Develop a function named draw-offset-dot
that
takes in a posn
and draws a small green dot 25 pixels to the
right and 12 pixels down from it. 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.)
Write a function named pick-color that takes in a number (0, 1, 2, or 3) and returns one of the symbols 'red, 'green, 'blue, or 'yellow respectively.
Write a function named random-color that takes in a symbol, ignores it, and returns one of the symbols 'red, 'green, 'blue, or 'yellow chosen at random. (Hint: you'll need to use the built-in random function. Look it up in the DrScheme Help Desk.)
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.
Write a function named draw-random-ball that takes in two numbers width and height and draws a small circle of a random color in a random location. Like most of our drawing functions, this one returns true. Test your function by making a graphics window with (start 400 300), then calling (draw-random-ball 400 300) several times from the interactions window.
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
Error log: /30
Function name | Contract | Examples | Definition | Test results |
---|---|---|---|---|
within-radius?
|
/5 | /5 | /10 | /5 |
in-box? |
/10 | /10 | /20 | /10 |
shift-posn |
/5 | /5 | /10 | /5 |
draw-dot |
/5 | /5 | /10 | /5 |
draw-offset-dot |
/5 | /5 | /10 | /5 |
pick-color |
/5 | /5 | /10 | /5 |
random-color |
/5 | /5 | /10 | /5 |
random-posn |
/5 | /5 | /10 | /5 |
draw-random-ball |
/5 | /5 | /10 | /5 |
draw-bar-chart |
/10 | /10 | /20 | /10 |
Following directions | /20 |
Writing contracts from word problems | /20 |
Choosing examples | /20 |
Choosing names | /20 |
Coding | /20 |
Code re-use and choice of auxiliaries | /20 |