Please note: This assignment is to be completed with your partner from Homework 3.
Purpose
- To begin using recursive itemizations, in particular, lists.
Design & Code Style
Design and code style are both essential to effective programming, and thus each will be graded. For design, we expect you to follow the six-step Design Recipe. For style, it means that:
- No line may exceed 80 columns in length. How? Go to Racket Preferences > Editing > General Editing, and enable the “maximum character width guard.”
- The program must be properly indented, including the comments. How? Select some code and press Tab to automatically reindent just that code, or press Cmd/Ctl + I to reindent the whole file.
- Function and constant names must be descriptive, and argument names often should be.
- The program must follow the same naming conventions
as the textbook.
What?
For example, function names are
lower-kebab-case
, datatype names areUpper-kebab-case
orUpperCamelCase
, and constant names areSCREAMING-KEBAB-CASE
. In all style butUpperCamelCase
, words are separated by hyphens (-
).
Your Task
Recall your library data definitions from Homework 3.
-
You should have given data definitions for
LibItem
andLibLoan
(note)Note:that look something like these:HW3 allowed two different names for this class of data and one of its functions. When you copy your HW3 code into HW4, you should also update to the new names if you used the old names before:old name new name CheckOut
LibLoan
item-overdue?
loan-overdue?
; A LibItem is . . . ; A LibLoan is . . .
Copy these data definitions (yours, not ours) along with any necessary
define-struct
s into your file for Homework 4. (You may want to copy templates and data examples as well.) -
The library needs to keep track of its collection, including multiple items or loans at once, so add the following data definitions:
; An ItemCollection is one of: ; - '() ; - (cons LibItem ItemCollection) ; A ListOfLoans is one of: ; - '() ; - (cons LibLoan ListOfLoans)
-
Make two examples of
ItemCollection
and two examples ofListOfLoans
. -
Write templates (in
#;
-style comments) for processingItemCollection
andListOfLoans
.
-
You should have given data definitions for
-
Design a function
count-collection
that counts the number of items in anItemCollection
. -
Design a function
total-time
that, given anItemCollection
, computes the total running time of all the videos in the collection. -
Design a function
find-overdue
that, given aListOfLoans
, returns aListOfLoans
containing only theLibLoan
s that are overdue. (You may—indeed, should—reuse code from Homework 3.) Evaluate the following expressions step by step and write down next to each step whether it is (1) arithmetic (of any form—not just on numeric data), (2) plugging (function application) or (3) cond (a conditional step):
-
(define (in->cm in) (* in 2.54)) (define (cm->in cm) (/ cm 2.54)) (in->cm (cm->in 12))
-
An expression that applies
count-collection
to anItemCollection
containing two items representing:- Tracy Kidder: “The Soul of a New Machine” (1981).
- “Purple Rain” (dir. Albert Magnoli), 114 minutes (1984).
As a confidence check, you can run each program and make sure your stepping result agrees with DrRacket’s result. Please write the steps in the same style we used in class, and don’t forget to label each arrow with one of “arith,” “plug,” or “cond”.
-
-
In this exercise, we will design a card catalogue browser. The world will be an
ItemCollection
representing a stack of cards. The browser displays the stack of cards and allows the user to discard the top card to see the next card by clicking the mouse.-
A catalogue card is white, 375 pixels wide by 225 pixels high, with a red line running horizontally across it, 50 pixels from the top. The information from the displayed
LibItem
is formatted nicely. Here are two catalogue cards:Design a function
render-card
that creates anImage
from aLibItem
as in the two example cards. (Don’t worry if some of the text is too long to fit across the card.)When writing tests for
render-card
, try to avoid embedding rendered images in the code. Text rendered on different computers may not match exactly due to different fonts, so placing images in the tests could cause them to fail when the code is run on a different machine (such as your TA’s). -
Design a function
render-card-stack
that renders a stack of cards in a 500-by-300 scene. Here’s my rendering of a stack of three cards:The top card shows and the remaining cards are hidden. (Don’t worry if there are too many cards to fit in the scene.)
-
Design a mouse event handler function
handle-mouse
that responds to mouse clicks by removing the first card from the collection. Other mouse events should be ignored. If the collection is empty already, it should remain empty. (Don’t crash.) -
Design a function
run-catalogue
that, given anItemCollection
, starts the catalogue browser usingbig-bang
and allows the user to browse through the stack of cards by clicking the mouse. This function can’t be tested usingcheck-expect
, so to preserve your test coverage, you should comment it out with#;
. -
Bonus! Your catalogue browser would be a lot more useful if it allowed moving in both directions. Modify your program so that pressing the right arrow on the keyboard moves down through the stack of cards just as the mouse clicks do, and the left arrow moves back up by returning the most recently removed card to the top of the stack. Because
ItemCollection
will no longer suffice to represent your world state, you will need to design a new data definition.
-
Turn In
Please write all your code in one .rkt
file and
submit it through
Canvas.