THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, 2014 Last Name: First Name: Signature: UBC Student #: Important notes about this examination 1. This exam has two separate parts. Your time to complete the first part is 50 minutes. After that time, Part A will be collected and Part B distributed. You will then have 45 minutes to complete Part B. 2. This exam will be graded largely on how well you follow the design recipes. You have been given a copy of the Recipe Exam Sheet. Use it! 3. Put away books, papers, laptops, cell phones everything but pens, pencils, erasers and this exam. 4. Good luck! Student Conduct during Examinations 1. Each examination candidate must be prepared to produce, upon the request of the invigilator or examiner, his or her UBCcard for identification. 2. No questions will be answered in this exam. If you see text you feel is ambiguous, make a reasonable assumption, write it down, and proceed to answer the question. 3. No examination candidate shall be permitted to enter the examination room after the expiration of one half hour from the scheduled starting time, or to leave during the first half hour of the examination. Should the examination run fortyfive (45) minutes or less, no examination candidate shall be permitted to enter the examination room once the examination has begun. 4. Examination candidates must conduct themselves honestly and in accordance with established rules for a given examination, which will be articulated by the examiner or invigilator prior to the examination commencing. Should dishonest behaviour be observed by the examiner(s) or invigilator(s), pleas of accident or forgetfulness shall not be received. 5. Examination candidates suspected of any of the following, or any other similar practices, may be immediately dismissed from the examination by the examiner/invigilator, and may be subject to disciplinary action: i. speaking or communicating with other examination candidates, unless ii. iii. iv. otherwise authorized; purposely exposing written papers to the view of other examination candidates or imaging devices; purposely viewing the written papers of other examination candidates; using or having visible at the place of writing any books, papers or other memory aid devices other than those authorized by the examiner(s); and, v. using or operating electronic devices including but not limited to telephones, calculators, computers, or similar devices other than those authorized by the examiner(s) (electronic devices other than those authorized by the examiner(s) must be completely powered down if present at the place of writing). 6. Examination candidates must not destroy or damage any examination material, must hand in all examination papers, and must not take any examination material from the examination room without permission of the examiner or invigilator. 7. Notwithstanding the above, for any mode of examination that does not fall into the traditional, paper based method, examination candidates shall adhere to any special rules for conduct as established and articulated by the examiner. 8. Examination candidates must follow any additional examination rules or directions communicated by the examiner(s) or invigilator(s). Please do not write in this space: Question 6: Question 9: Question 7: Question 8: 5 236339 344835
Below is the beginning of a running horses world program. It is more than just the solution to problem 5 from part A. It also includes constants, complete data definitions, a main function and wish list entries for the big-bang handlers. Study it carefully before proceeding. REMEMBER: Do not worry if our solution to problem 5 is different than yours! We are not grading for whether your answer is exactly the same as ours. We are grading for whether you are working with the design recipes properly. But from this point forth it is essential that you do all your work based on the start of the program we are providing you below, do not refer back to your answers from part A. (require 2htdp/image) (require 2htdp/universe) ;; Running horses ;; ================= ;; Constants: (define WIDTH 400) (define HEIGHT 400) (define SPEED 3) (define HORSE ) (define MTS )
Problem 6: On the two data definitions below you should do three things: - draw appropriate reference, self-reference, natural helper and natural recursion arrows - label each arrow with R, SR, NH or NR - number each arrow with a number like 1, 2, 3 etc. to show which arrows within the type comments correspond to which arrows within the templates ;; ================= ;; Data Definitions: (define-struct horse (x y)) ;; Horse is (make-horse Integer Integer) ;; interp. a horse at position (x,y) in pixels (define H1 (make-horse 56 100)) (define H2 (make-horse 82 14)) (define (fn-for-horse h) (... (horse-x h) (horse-y h))) ;; Template rules used: ;; - compound: 2 fields ;; ListOfHorse is one of: ;; - empty ;; - (cons Horse ListOfHorse) ;; interp. a list of horses (define LOH1 empty) (define LOH2 (cons H1 (cons H2 empty))) #; (define (fn-for-loh loh) (cond [(empty? loh) (...)] [else (... (fn-for-horse (first loh)) (fn-for-loh (rest loh)))])) ;; Template rules used: ;; - one of: 2 cases ;; - atomic distinct: empty ;; - compound: (cons Horse ListOfHorse) ;; - reference: (first loh) is Horse ;; - self-reference: (rest loh) is ListOfHorse
Problem 7 Complete the design of the next-horses function, based on the wish-list entry below. All you need to write is examples, where you got the template and a completed function definition. You do not need to repeat the signature and purpose. If you do this properly, you will end up wishing for one additional helper function. For that helper function, you should produce a complete wish list entry, but you do not need to complete the design beyond that point. ;; ================= ;; Functions: ;; ListOfHorse -> ListOfHorse ;; start the world with (main empty) ;; <no tests for main functions> (define (main loh) (big-bang loh ; ListOfHorse (on-tick next-horses) ; ListOfHorse -> ListOfHorse (to-draw render-horses) ; ListOfHorse -> Image (on-mouse handle-mouse))) ; ListOfHorse Integer Integer MouseEvent ; -> ListOfHorse ;; ListOfHorse -> Image ;; render all horses in loh on MTS (define (render-horses loh) MTS) ; stub ;; ListOfHorse Integer Integer MouseEvent -> ListOfHorse ;; add a new horse at (x,y) when the mouse button is pressed (define (handle-mouse loh x y me) loh) ; stub ;; ListOfHorse -> ListOfHorse ;; advances all horses by SPEED (define (next-horses loh) loh) ; stub
Problem 8 Here is our version of render-horses. As were were designing it we ended up wishing for a helper function called place-horse. Complete the design of the place-horse function, based on the wish-list entry below. All you need to write is examples, where you got the template and a completed function definition. You do not need to repeat the signature and purpose. ;; ListOfHorse -> Image ;; render all horses in loh on MTS (check-expect (render-horses empty) MTS) (check-expect (render-horses (cons (make-horse 10 20) (cons (make-horse 87 23) empty))) (place-image HORSE 10 20 (place-image HORSE 87 23 MTS))) ;(define (render-horses loh) MTS) ; Took template from ListOfHorse (define (render-horses loh) (cond [(empty? loh) MTS] [else (place-horse (first loh) (render-horses (rest loh)))])) ;; Horse Image -> Image ;; place horse on img at the horse's x-coord and y-coord (define (place-horse h img) img) ; stub
Problem 9 We thought we completed our design of handle-mouse, but the second check-expect is failing and when we run the program and click the mouse to add a new horse we get the below error. [Note, in this case the mouse was clicked at (225,142)] Make the minimal changes necessary to make this program work properly, make the function design consistent and respect style rules and conventions. Please make your changes by editing the code below. ;; ListOfHorse Integer Integer MouseEvent -> ListOfHorse ;; handle mouse event (check-expect (handle-mouse empty 20 43 "drag") empty) (check-expect (handle-mouse LOH2 77 29 "button-down") (cons (make-horse 77 29) LOH2)) ;(define (handle-mouse loh x y me) loh) ; Took template from MouseEvent (define (handle-mouse loh x y me) (cond [(mouse=? me "button-down") (make-horse x y)] [else loh]))