EECS 111: Homework 4

Due: Tuesday, May 7 at 11:59 PM

Please note: This assignment is to be completed with your partner from Homework 3.

Purpose

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:

Your Task

  1. Recall your library data definitions from Homework 3.

    1. You should have given data definitions for LibItem and LibLoan
      (note)Note:
      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
      CheckOutLibLoan
      item-overdue?loan-overdue?
      that look something like these:
      ; A LibItem is . . .
      
      ; A LibLoan is . . .

      Copy these data definitions (yours, not ours) along with any necessary define-structs into your file for Homework 4. (You may want to copy templates and data examples as well.)

    2. 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)
      
    3. Make two examples of ItemCollection and two examples of ListOfLoans.

    4. Write templates (in #;-style comments) for processing ItemCollection and ListOfLoans.

  2. Design a function count-collection that counts the number of items in an ItemCollection.

  3. Design a function total-time that, given an ItemCollection, computes the total running time of all the videos in the collection.

  4. Design a function find-overdue that, given a ListOfLoans, returns a ListOfLoans containing only the LibLoans that are overdue. (You may—indeed, should—reuse code from Homework 3.)

  5. 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):

    1. (define (in->cm in)
        (* in 2.54))
      
      (define (cm->in cm)
        (/ cm 2.54))
      
      (in->cm (cm->in 12))
      
    2. An expression that applies count-collection to an ItemCollection containing two items representing:

      1. Tracy Kidder: “The Soul of a New Machine” (1981).
      2. “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”.

  6. 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.

    1. 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:

      Donald Knuth: “The Art of Computer
       Programming” (1965). “Six-String Samurai” (Dir. Lance Mungia), 91 minutes (1998).

      Design a function render-card that creates an Image from a LibItem 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).

    2. 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:

      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.)

    3. 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.)

    4. Design a function run-catalogue that, given an ItemCollection, starts the catalogue browser using big-bang and allows the user to browse through the stack of cards by clicking the mouse. This function can’t be tested using check-expect, so to preserve your test coverage, you should comment it out with #;.

    5. 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.