RU beehive logo ITEC dept promo banner
ITEC 380
2015fall
ibarland

most recent semester

hw03
structs
breakout structs

Due:
Section 01: Sep.16 (Tue) 23:59, on D2L and hardcopy in the following class
Section 11: Sep.17 (Wed)18 (Thu) 23:59, D2L.
Your submitted file should be named “hw03.rkt1.

Standard instructions for all homeworks:


Images in racket

To experiment with functions-which-create-and-handle-images in DrRacket, include (require 2htdp/image) near the top of your file. Then you should be be able to evaluate each of the following:

(rectangle 80 20 'solid 'blue)
(circle 20 'solid 'red)
(ellipse 80 20 'outline 'orange)

(beside (rectangle 80 20 'outline 'blue)
        (circle 20 'solid 'red))
(underlay (rectangle 80 20 'outline 'blue)
         (circle 20 'solid 'red))
; Think of 'underlay' and 'beside' as being "addition for images"

; underlay with an offset:
(underlay/offset (rectangle 80 20 'outline 'blue)
                0
                10
                (circle 20 'solid 'red))


; If you want to explore documentation:
;   In DrRacket, position the caret on a function-name
;   like 'underlay', and hit F1.

Test cases for images

For functions that return an image, your expected-value should either be (a) an expression involving calls to: the image primitives and/or previously-tested functions, OR (b) an image-literal (but, show the expressions you used to create that image-literal).

For example, if the problem were to "create a function that takes in a width, and returns a solid purple rectangle with the given width and a 16:9 aspect ratio", then the "expected result" part of the test cases might involve calling the image-library's functions like rectangle:

(check-expect (screen 32) (rectangle 32 18 'solid 'purple))
(check-expect (screen 16) (rectangle 16 9 'solid 'purple))
(check-expect (screen 0) (rectangle 0 0 'solid 'purple))
  ; Note: for the expected 0x0 rectangle, I could equally well write:
(check-expect (screen 0) empty-image)
Or, since DrRacket supports image literals (!), I could copy/paste images as the second argument to check-expects: a sample check-expect, with an image-literal as the expected result


    1. example results of calling donut, eyes
    2. (3pts) Write a function which takes in a size, and returns an image of a brown donut. (Mmmm, doughnut.)
    3. (3pts) Write a function eyes which takes a size and a color, and returns two colored rings side-by-side.
    4. (4pts) As needed, refactor the above functions so that there is little-to-no repeated code. (If the underlying helper function has a solid set of tests, the final function might get by with fewer tests — perhaps even just one.) You only need turn in your refactored versions.

    You only need steps 4,5,7,8 of the design recipe (once you decide what how you'll represent "size", which is a straightforward choice), and we're only using existing, simple datatypes (so no steps 1-3 or 6).

  1. (10pts) Watching a DVD takes longer than just the movie's play time: It takes 5 minutes to get everybody together, 1 minute to start the disc playing, and 3 minutes per preview (G-rated movies have 8 previews, all others have 4). Then, once the movie itself starts, there is a 5-minute pause for a bathroom break after every 42 minutes of movie (so long as the movie is still going).

    Write the function actual-time which takes in a dvd struct (from lecture), and returns the actual time (in minutes) required to watch it.

    hints:
    • The functions quotient and remainder can be helpful.
    • The built-in function zero? can be effected by using (= 0 ) but is still surprisingly handy.
    • You can use let*, or just use if as sub-expression of a larger expression.
    • You get more points for having a test-case that fails than for not having a test case that would fail.

    You only need steps 4-8 of the design recipe, since we already did steps 1-3 of in lecture, for dvds.

  2. Familiarize yourself with the Breakout arcade game, such as at kongregate.com. We will write a simplified version which does not need to deal with: score, powerups, having more than one level, or number-of-lives.
    1. (5pts) What are three different types of things (objects) in the game, that our program will need to model?
    2. (5pts) For one of the types in (a) above2 give a Java class definition for it (fields and the “natural” constructor3 only). Create two or three instances of the object (presumably in a test method that does nothing else). You'll submit a .java file along with a .rkt file.
    3. (5pts) Repeat the previous step (b), but in racket. (That is, give a struct-definition w/ a comment indicating the field-types, and create at same two examples.) All field-names should correspond directly — e.g. if a Java field was named “numPages”, then the racket name would be “num-pages”.
    Note that we are not writing any functions, so we don't have any check-expects. We are just doing steps 1 and 2 (only) of the design recipe (in both Java and in racket).
  3. Hints:

See also scheme-resources—ITEC380 Racket resources.


1Note that if you have embedded images in your program, it won't be a plain-text file; that's okay.      

2 Choose one of the types above which is naturally represented as a struct/object with more than one field. That's probably all three of those answers, but conceivably not be.      

3 The natural constructor is the one that just initializes each field directly from its arguments. It's totally automatic; in fact tools like Eclipse offer a menu option to auto-generate the natural constructor. Just like we did for Book.java in class.      

most recent semester


©2015, Ian Barland, Radford University
Last modified 2015.Sep.28 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.