csc 160 computer programming for non-majors lecture #11: conditionals ii prof. adam m. wittenstein...

30
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Lecture #11: Conditionals Lecture #11: Conditionals II II Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/

Post on 20-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

CSC 160CSC 160Computer Computer

ProgrammingProgrammingfor Non-Majorsfor Non-Majors

Lecture #11: Conditionals IILecture #11: Conditionals II

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

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…

I. Another Conditional FunctionI. Another Conditional Function

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).

;Purpose: To check the guess of a random number.

;Contract: number number -> sentence

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 three intervals it is in.;We print out a different number for each of the intervals.

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 three intervals 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)

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)

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)

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)

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)

Exercise 5.1.2

II. Conditional Functions and II. Conditional Functions and AnimationsAnimations

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)

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.

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.

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…)

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; ))

STEP 2: next-model

;Function:

(define (next-model t)

(cond

[(>= t HEIGHT) (end-of-time "The ball has landed.")]

[else (+ t 2)]))

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”

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.) centertWORLD0

))

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)

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.) centertWORLD0))

(big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

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.

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.) centertWORLD0))

(big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

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.) centertWORLD0))

(big-bang WIDTH HEIGHT .1 0) (on-redraw next-view) (on-tick-event next-model)

III. Summary and PracticeIII. Summary and Practice

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.

CW6 – part 1CW6 – part 1 Do Exercise 5.1.1 on paper using the function Do Exercise 5.1.1 on paper using the function

definition below. Then type your answers into the definition below. Then type your answers into the Definitions Window commented out with semicolons. Definitions Window commented out with semicolons. (Assume this function was written in the (Assume this function was written in the Simply Simply SchemeScheme language mode.) language mode.)

a) Evaluate (reply ‘(How Are You?)) by hand.a) Evaluate (reply ‘(How Are You?)) by hand. b) Write out four more examples for this function.b) Write out four more examples for this function.

(define (reply s)(define (reply s)   (cond   (cond     [(equal? s ‘(Good Morning)) ‘Hi]     [(equal? s ‘(Good Morning)) ‘Hi]     [(equal? s ‘(How Are You?)) ‘Fine]     [(equal? s ‘(How Are You?)) ‘Fine]     [(equal? s ‘(Good Afternoon)) ‘(I Need A      [(equal? s ‘(Good Afternoon)) ‘(I Need A Nap)]Nap)]     [(equal? s ‘(Good Evening)) ‘(Boy Am I Tired)]     [(equal? s ‘(Good Evening)) ‘(Boy Am I Tired)] ))

))

CW6 – part 2CW6 – part 2

Do Exercise 4.4.1 in DrScheme, using the Do Exercise 4.4.1 in DrScheme, using the Design Recipe (version 2). Design Recipe (version 2).

[Develop a function [Develop a function interestinterest. Like interest-. Like interest-rate, it consumes a deposit amount. Instead of rate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of the rate, it produces the actual amount of interest that the money earns in a year. The interest that the money earns in a year. The bank pays a flat 4% for deposits of up to bank pays a flat 4% for deposits of up to $1000, a flat 4.5% per year for deposits of up $1000, a flat 4.5% per year for deposits of up to $5000, and a flat 5% for deposits of more to $5000, and a flat 5% for deposits of more than $5000.]than $5000.]

Submitting CW6Submitting CW6

Submit the following to the Digital Dropbox bySubmit the following to the Digital Dropbox by11:59pm tonight:11:59pm tonight:

Your Definitions Window with the filename Your Definitions Window with the filename “CW6[section][LastName]Def.scm”“CW6[section][LastName]Def.scm”

Your Interactions Window with the filename Your Interactions Window with the filename “CW6[section][LastName]Int.scm”“CW6[section][LastName]Int.scm”

For example, if I were in Section 01, my filenames For example, if I were in Section 01, my filenames would be “CW6-01WittensteinDef.scm” and “CW6-would be “CW6-01WittensteinDef.scm” and “CW6-01WittensteinInt.scm”.01WittensteinInt.scm”.