RU beehive logo ITEC dept promo banner
ITEC 380
2012fall
ibarland
tlewis32

homelecturesexamshwsbreeze (snow day)

hw04
handling lists
due Oct.05 (Fri) in class

Due Oct.05 (Fri) in class (hardcopy, and on D2L) Your name and the assignment-number must be in a comment at the start of the file.

  1. (3pts) Chpt.15, review question #13, syntax,semantics of let. (If using 8ed: #7)
  2. (3pts) Chpt.15, review question #16. (If using 8ed: What is tail recursion, and why is it desirable?)

  3. The following problems build on the Asteroids project started in hw03—handling lists: due Sep.26 (Wed). You should add this code to (a copy of) the file Ship.java. Do not re-assign to any fields or variables.

  4. The static Java method move (implicitly) which takes in a Ship, and returns a whole new (different!) Ship object which has traveled for one unit of time (a "tick").
    1. Write a stub method.
    2. (3pts) Write and compile two test cases.
      (Remember that a test-case includes printing out 1 both (a) the actual and (b) the expected results of calling your method; presumably you'll want to write a toString to help with this.) You can put those test cases in Ship.main, or in a separate test-driver class if you prefer.

      You might want to review notions of distance vs. acceleration: one site is u3l2c.cfm. (Post on the message boards, if you find a site/explanation you like better.) We aren't actually dealing with acceleration at all, yet, but we will.

      You do not need to worry about the screen-size or wrapping around for this hw (though you can add that if you like; you'll eventually need it).

    3. (3pts)After writing the test cases and getting them to compile, now complete the method.
    You should have a single .java file (which compiles and runs); it should be less than a page of well-spaced, commented code.
  5. (6pts) In your racket version, write the corresponding move-ship function (including test-cases of course, presumably using check-expect).

  6. (6pts) Continue your racket version (not the Java version) by defining a struct for asteroids. (Hint: Give it a shorter name, something like “astr”.) (For every field, give a two-to-three word description of what it stands for, and its units.) Create two examples (this is half the points).
  7. (6pts, half for test cases) Write move-astr, which takes in an asteroid and returns an asteroid one tick later.
  8. (6pts, half for test cases) Write move-astrs : (listof astr) -> (listof astr), which updates all asteroids in a list.

For the following functions: If you want to test these functions, you can either call empty-scene, or use any previous image you've made. You don't need check-expects for the image functions (though you are certainly welcome to do so).

  1. (2pts) In racket2, write a function draw-ship which takes in a ship and an image (the screen / background), and returns a new image which contains [a picture of] the ship overlayed on the given background.

    Look in the racket documentation for helpful image-drawing functions. (Either search directly, or take a function you know about (like circle), put the caret over it, and hit F1 to call up the documentation.)

    You do not need to provide any test cases for the draw functions, but you are welcome to. In fact, you might find that writing one test case makes writing the function trivial. Here's a test-case I used (your artwork3 will differ, of course):

  2. (2pts) Write draw-astr : astr, image -> image.
  3. (3pts) Write draw-astrs : (list-of astr), image -> image.

As ever: Do not use any images that you do not have a copyright to. Cite any images you got from others.


1 If you want to be fancier than just printing out results, you can use JUnit tests. This will require overriding equals.      

2I am not asking for a Java version of a drawing program. Java drawing libraries tend to be very heavyweight. I have heard of a java-implemention of racket's drawing primitives; you can experiment with javalib if you like. But only do so after finishing the racket exercises.      

3 Here is some code I used to make the picture of house and flowers. Note how make-blossom-help follows the data definition for natural numbers.

;; Some example of drawing simple pictures, using the image.ss library.
;; See:  http://docs.racket-lang.org/teachpack/2htdpimage.html



(define house-image (above (triangle 60 'solid 'saddleBrown)
                           (square 60 'solid 'darkGoldenRod)))


; one ellipse will serve as two petals:
(define petals2 (ellipse 50 10 200 'MediumSeaGreen))

; make-blossom: natnum -> image
; Return a flower-center, with 2*n petals.
;
(define (make-blossom n)
  (make-blossom-help n n))

; make-blossom-help: natnum, natnum -> image
; Return a flower with 2*i petals, out of 2*n total.
;
(define (make-blossom-help i n)
  (cond [(zero? i) empty-image]
        [(positive? i) (overlay (rotate (* i (/ 360 n 2)) petals2)
                                (make-blossom-help (sub1 i) n))]))



(define the-blossom (make-blossom 7))

; Add the stem, whose height is the same as the blossom:
(define flower
  (overlay/offset the-blossom
                  0  (/ (image-height the-blossom) 2)
                  (line 0 (image-height the-blossom) 'black)))


(define house-with-flowers
  (place-image
   (circle 30 'solid 'yellow)
   250  0
   ; house+flowers, enclosed in a scene:
   (overlay/align 'middle 'bottom 
                  (beside/align 'bottom flower flower house-image flower)
                  (empty-scene 250 180))))
  
     

homelecturesexamshwsbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Oct.10 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme