1 / 30

CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II

CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II. Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/. Review.

kathymorris
Download Presentation

CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 160Computer Programmingfor Non-MajorsLecture #11: Conditionals II Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/

  2. Review • Functions that need to determine which of several conditions holds for the input are called conditional functions. One such function is interest-rate. • A conditional function without numbers. • Using conditional functions to enhance our Animations. • CW6: practice with conditional functions. Coming up…

  3. I. Another Conditional Function

  4. Exercise 5.1.2 • Develop the function check-guess. • It consumes two numbers, guess and target. • Depending on how guess relates to target, the function produces one of the following three answers: ’(Too Small), ’(Perfect), or ’(Too Large).

  5. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence

  6. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Data Analysis: We take in a number and determine which ;of threeintervals it is in. ;We print out a different number for each of the intervals.

  7. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Data Analysis: We take in a number and determine which ;of threeintervals it is in. ;We print out a different number for each of the intervals. ;Examples: (check-guess 5 10) “should be” ‘(Too Small) (check-guess 12 6) “should be” ‘(Too Large) (check-guess -3 -3) “should be” ‘(Perfect)

  8. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Skeleton: ;(define (check-guess guess target) ; … guess … target …) ;Examples: (check-guess 5 10) “should be” ‘(Too Small) (check-guess 12 6) “should be” ‘(Too Large) (check-guess -3 -3) “should be” ‘(Perfect)

  9. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Template (define (check-guess guess target) (cond [question … answer …] [question … answer …] [question … answer …])) ;Examples: (check-guess 5 10) “should be” ‘(Too Small) (check-guess 12 6) “should be” ‘(Too Large) (check-guess -3 -3) “should be” ‘(Perfect)

  10. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Fill in the Questions (define (check-guess guess target) (cond [(= guess target) …answer…] [(> guess target) …answer…] [(< guess target) …answer…])) ;Examples: (check-guess 5 10) “should be” ‘(Too Small) (check-guess 12 6) “should be” ‘(Too Large) (check-guess -3 -3) “should be” ‘(Perfect)

  11. Exercise 5.1.2 ;Purpose: To check the guess of a random number. ;Contract: number number -> sentence ;Function Body (define (check-guess guess target) (cond [(= guess target) ‘(Perfect)] [(> guess target) ‘(Too Large)] [(< guess target) ‘(Too Small)])) ;Examples: (check-guess 5 10) “should be” ‘(Too Small) (check-guess 12 6) “should be” ‘(Too Large) (check-guess -3 -3) “should be” ‘(Perfect)

  12. II. Conditional Functions and Animations

  13. REVIEW: Animation #3 (define WIDTH 200) (define HEIGHT 180) (define WORLD0 (empty-scene WIDTH HEIGHT)) ;Purpose: To change the (model of the) world from one time to the next. ;When the clock ticks, the ball has drops two more pixels. (define (next-model t) (+ t 2)) ;Purpose: Display the ball in the middle of the canvas at a time, t. (define (next-view t) (place-image (/ WIDTH 2) t WORLD0)) (big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

  14. Animation #4 • Create an animation where the ball stops when it gets to the bottom of the canvas. • Then, it should stay at the bottom. STEP 1: EXPLAINING THE WORLD • The World is a number which is represented by the ball’s location.

  15. STEP 2: next-model ;Purpose: ;When the clock ticks, the world gets 2 bigger until it stops at 180. ;Contract: ;next-model: number -> number or sentence ;Data Analysis: ;We take in a number and determine which of ;two intervals it is in. ;We either ;add 2 ;or ;print out a message ;depending on the interval.

  16. STEP 2: next-model ;Examples: ;(next-model 0) “should be” 2 ;(next-model 4) “should be” 6 ;(next-model 180) “should be” 180 ;Skeleton: ;(define (next-model t) ; …t…)

  17. STEP 2: next-model Template: ;(define (next-view t) ; (cond ; [question answer] ;if the ball is at the bottom, ;it should stop ; [question answer] ;if the ball is not at the ;bottom, it should keep ;moving ; ))

  18. STEP 2: next-model ;Function: (define (next-model t) (cond [(>= t HEIGHT) (end-of-time "The ball has landed.")] [else (+ t 2)]))

  19. STEP 3: next-view ;Purpose: ;Display the ball moving across the middle of the ;canvas according to the time. ;Contract: ;next-view: number -> image ;Examples ;(next-view -45) “should be” “an empty canvas” ;(next-view 6) “the ball at its third location” ;(next-view 180) “should be” “half a ball at the bottom of the ; screen”

  20. STEP 3: next-view ;Skeleton: ;(define (next-view t) ; …t…) ;Function: (define (next-view t) (place-image (/ WIDTH 2) ; will drop the ball in the (horiz.) center t WORLD0 ) )

  21. Steps 4-6: Finishing the Animation Again, we use the predefined functions for our last three steps: • (big-bang WIDTH HEIGHT .1 0) • (on-redraw next-view) • (on-tick-event next-model)

  22. A Completed Animation #4 ;The World is a number which is represented by the ball’s location. (define (next-model t) (cond [(= t HEIGHT) (end-of-time "The ball has landed.")] [else (+ t 2)])) (define (next-view t) (place-image (/ WIDTH 2) ; will drop the ball in the (horiz.) center t WORLD0)) (big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

  23. Question and Answer • The ball doesn't comes to rest on the ground, but drops into the ground, as if it were led. Can you stop the ball so that it rests exactly on the ground? • Replace HEIGHT by (- HEIGHT 40) in the first line of the next-model function.

  24. A Completed Animation #4.1 ;The World is a number which is represented by the ball’s location. (define (next-model t) (cond [(= t (- HEIGHT 40)) (end-of-time "The ball has landed.")] [else (+ t 2)])) (define (next-view t) (place-image (/ WIDTH 2) ; will drop the ball in the (horiz.) center t WORLD0)) (big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

  25. A Completed Animation #4.2 ;The World is a number which is represented by the ball’s location. (define (next-model t) (cond [(= t (- HEIGHT (* ½ (image-height )))) (end-of-time "The ball has landed.")] [else (+ t 2)])) (define (next-view t) (place-image (/ WIDTH 2) ; will drop the ball in the (horiz.) center t WORLD0)) (big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

  26. III. Summary and Practice

  27. In summary… • A conditional function is defined the same way as any other function, except that the function body is now a conditional expression. • This is true regardless of the data type. • With conditionals we can now program things that we couldn’t before. • This includes making the ball in our animation stop at the bottom of the screen.

  28. CW6 – part 1 • Do Exercise 5.1.1 on paper using the function definition below. Then type your answers into the Definitions Window commented out with semicolons. (Assume this function was written in the Simply Scheme language mode.) • a) Evaluate (reply ‘(How Are You?)) by hand. • b) Write out four more examples for this function. (define (reply s)   (cond     [(equal? s ‘(Good Morning)) ‘Hi]     [(equal? s ‘(How Are You?)) ‘Fine]     [(equal? s ‘(Good Afternoon)) ‘(I Need A Nap)]     [(equal? s ‘(Good Evening)) ‘(Boy Am I Tired)] ) )

  29. CW6 – part 2 • Do Exercise 4.4.1 in DrScheme, using the Design Recipe (version 2). • [Develop a function interest. Like interest-rate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of interest that the money earns in a year. The bank pays a flat 4% for deposits of up to $1000, a flat 4.5% per year for deposits of up to $5000, and a flat 5% for deposits of more than $5000.]

  30. Submitting CW6 Submit the following to the Digital Dropbox by 11:59pm tonight: • Your Definitions Window with the filename “CW6[section][LastName]Def.scm” • Your Interactions Window with the filename “CW6[section][LastName]Int.scm” • For example, if I were in Section 01, my filenames would be “CW6-01WittensteinDef.scm” and “CW6-01WittensteinInt.scm”.

More Related