On this page:
Introduction
1 Fixing Install Issues
2 Finish off Lab 1
3 Finger exercises
6.12

Lab 2: More Data Definitions

Introduction

You’ll work in this lab in ad-hoc pairs. Find a partner and get started.

The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

You both should install DrRacket, but only one instance should be use during the lab. At the end you’ll submit the lab as a pair via the UMD CS Submit Server so we can keep an eye on lab attendance.

1 Fixing Install Issues

See Lab 1: Simple Data Definitions with Class for full installation details.

The installation issues from last lab were due to whitespace being copied into the "Package Source" text area along with the following URL:

https://github.com/dvanhorn/dpc.git#racket-v6.10

If you encounter issues during the installation be sure to delete all text from the text area before you press "Install", even if you do not see any text. Call a TA over if you continue to have issues.

2 Finish off Lab 1

Make sure you finish all of Lab 1: Simple Data Definitions with Class, particularly the last problem.

3 Finger exercises

In the following exercises you’ll find simple programs that follow the design recipe from last semester. Redesign these programs using the class/0 language. You should use classes and methods instead in place of atomic data, structures, and functions.

Each exercise builds on the last. You should solve the exercises in-order and in the same definitions window.

Ex 1: This program is a simple video player.

;; A Video is a (make-video title year play-count)
;; where title      is a String,
;;       year       is an Int,
;;   and play-count is a PositiveInt.
;; Interp: A video multimedia entry.
(define-struct video (title year play-count))
 
;; play : Video -> Video
;; Play the given video, incrementing its play-count.
(check-expect (play-video (make-video "Caddyshack" 1980 4))
              (make-video "Caddyshack" 1980 5))
(define (play-video v)
  (make-video (video-title v) (video-year v) (add1 (video-play-count v))))

Ex 2: This program compares two videos to find the earlier.

;; earlier? : Video Video -> Boolean
;; Was the first given video made earlier than the second?
(check-expect (video-earlier (make-video "Caddyshack" 1980 4)
                             (make-video "Aladdin" 1992 40))
              #true)
(define (video-earlier v1 v2)
  (< (video-year v1) (video-year v2)))

Ex 3: This program collects videos in a library, allowing multiple videos to be played at once.

;; A VideoLibrary is one of:
;; - (make-mt)
;; - (make-kons Video VideoLibrary)
;; Interp: A collection of Videos
(define-struct mt ())
(define-struct kons (first rest))
 
;; play-all-videos : VideoLibrary -> VideoLibrary
;; Play all of the media in the given library.
(check-expect (play-all-videos (make-mt)) (make-mt))
(check-expect (play-all-videos
               (make-kons (make-video "Caddyshack" 1980 4)
                          (make-kons (make-video "Aladdin" 1992 40)
                                     (make-mt))))
              (make-kons (make-video "Caddyshack" 1980 5)
                         (make-kons (make-video "Aladdin" 1992 41)
                                    (make-mt))))
(define (play-all-videos l)
  (cond [(mt? l) l]
        [else (make-kons (play-video (kons-first l))
                         (play-all-videos (kons-rest l)))]))

Ex 4: This program allows users to view only new videos in the library.

;; only-new-videos : VideoLibrary -> VideoLibrary
;; View only the new items in the library.
(check-expect (only-new-videos (make-mt)) (make-mt))
(check-expect (only-new-videos
               (make-kons (make-video "Caddyshack" 1980 4)
                          (make-kons (make-video "The Replacements" 2000 0)
                                     (make-mt))))
              (make-kons (make-video "The Replacements" 2000 0)
                         (make-mt)))
(define (only-new-videos l)
  (cond [(mt? l) l]
        [else (cond [(= 0 (video-play-count (kons-first l)))
                     (make-kons (kons-first l) (only-new-videos (kons-rest l)))]
                    [else (only-new-videos (kons-rest l))])]))

Ex 5: This program extends the notion of media to include music.

;; A Music is a (make-music title album year play-count)
;; where title      is a String,
;;       album      is a String,
;;       year       is an Int,
;;   and play-count is a PositiveInt.
;; Interp: A music multimedia entry.
(define-struct music (title album year play-count))
 
;; A Media is one of:
;; - Video
;; - Music
 
;; play : Media -> Media
;; Play the given media, incrementing its play-count.
(check-expect (play (make-music "Safari Song" "From the Fires" 2017 100))
              (make-music "Safari Song" "From the Fires" 2017 101))
(check-expect (play (make-video "Caddyshack" 1980 4))
              (make-video "Caddyshack" 1980 5))
(define (play m)
  (cond [(video? m) (play-video m)]
        [else (make-music (music-title m) (music-album m)
                          (music-year m) (add1 (music-play-count m)))]))

Ex 6: Extend your data definition of Library to include both types of Media. This should include implementations of play-all and only-new that work on a Library of both Video and Music.