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.
Do (but don't turn in) some of the exercises in sections 6.3, 6.4, and 6.5, as many as you think you need in order to feel comfortable defining structures. Although you don't need to turn these in, make sure you know how to do them before going on to the next exercises.
Define a structure to represent a television. Interesting properties
of a television include its screen width and height (in inches), whether it's digital or not
(I suggest the name "digital?" for this), and its price. Be sure to follow the recipe for defining
structures: describe the structure in English; write contracts for its constructor, getters,
and discriminator; write some examples using those functions; write a
define-struct
; and test to make sure things work as they should.
Also write an input template (and, optionally, an output template)
for televisions. You can copy and paste this template to save time writing
the following three functions.
Next, develop a function screen-area
which takes a television
and returns its screen area in square inches.
Next, develop a function tv-affordable?
which takes a television
and a number (representing your budget), and tells whether you can afford the television within
your budget.
Next, develop a function upgrade-to-digital
which takes a
television and returns a television. If the television was already digital, the function
returns it unchanged. If not, it returns a digital television with the same sized screen
and twice the price of the original television.
Define a structure to represent a candidate in an election, comprising a name (represented by a string) and a number of votes. Again, be sure to follow the recipe for defining a structure, including writing an input template (and, optionally, an output template), which you can use as a starting point in writing the following two functions.
Develop a function who-won
that takes in
three
candidate structures and returns the winning struct (i.e. both
the
name and the number of votes). Return the string "tie"
if there is no single winner.
For example,
(who-won (make-candidate "Anne" 3) (make-candidate "Bertha" 4) (make-candidate "Cyril" 2)) "should be" (make-candidate "Bertha" 4)
Develop a function add-a-vote
which
takes
in a candidate and returns a candidate with the same name and one more
vote. For example,
(add-a-vote (make-candidate "Anne" 3)) "should be" (make-candidate "Anne" 4)
Define a mixed data type "appliance" which can be either a television (see above) or a computer (as discussed in class on June 2). Write examples of the new type, at least one for each choice.
Develop a function appliance-affordable?
which takes an appliance and a number (a budget, in dollars) and returns
whether or not you can afford the appliance within your budget.
Develop a function mark-down
which
takes an appliance and a number (e.g. 0.10 for a 10% mark-down)
and returns an appliance just like the old one, but costing the
specified percentage less. For example,
(mark-down (make-television 30 20 false 800) .10) "should be" (make-television 30 20 false 720)
Error log: /40
television struct |
Data definition: /5 | Contracts: /5 | Examples: /5 | Template: /10 |
screen-area |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
tv-affordable? |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
upgrade-to-digital |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
candidate struct |
Data definition: /5 | Contracts: /5 | Examples: /5 | Template: /10 |
who-won |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
add-a-vote |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
appliance mixed type |
Data definition: /5 | Examples: /5 | Template: /10 | |
appliance-affordable? |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /5 |
mark-down |
Contract: /5 | Examples: /5 | Definition: /10 | Test results: /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 |