+ All Categories
Home > Documents > Chapter 6: (Expressions,) Functions, and If/Else

Chapter 6: (Expressions,) Functions, and If/Else

Date post: 23-Feb-2016
Category:
Upload: oya
View: 31 times
Download: 0 times
Share this document with a friend
Description:
Chapter 6: (Expressions,) Functions, and If/Else. First we had animations They “ran”/played the same way every time. Neat, but a bit boring Then we had events The user can directly control what happens. But, per event, what happens is the exact same thing (set of instructions) every time - PowerPoint PPT Presentation
Popular Tags:
31
Chapter 6: (Expressions,) Functions, and If/Else • First we had animations – They “ran”/played the same way every time. – Neat, but a bit boring • Then we had events – The user can directly control what happens. – But, per event, what happens is the exact same thing (set of instructions) every time • What if you want your world to react based on other characteristics? – What if you want your plane to burst into flame if it tries to fly into the ground? • Before we get to if statements, we need functions – to allow us to calculate when or how things should happen
Transcript
Page 1: Chapter 6: (Expressions,) Functions, and If/Else

Chapter 6: (Expressions,) Functions, and If/Else

• First we had animations– They “ran”/played the same way every time.– Neat, but a bit boring

• Then we had events– The user can directly control what happens. – But, per event, what happens is the exact same thing (set of

instructions) every time• What if you want your world to react based on other

characteristics?– What if you want your plane to burst into flame if it tries to fly into the

ground?• Before we get to if statements, we need functions – to allow us to calculate

when or how things should happen

Page 2: Chapter 6: (Expressions,) Functions, and If/Else

Chapter 6.1 Expressions and Functions

Page 3: Chapter 6: (Expressions,) Functions, and If/Else

Pick the best description of the purpose of methods and functions in Alice

Methods FunctionsA Perform actions in

the worldReturn a value

B Return a value Perform actions in the world

C Perform actions in the world

Change the state of the world numerically

D Change the state of the world numerically

Perform actions in the world

Page 4: Chapter 6: (Expressions,) Functions, and If/Else

A. Rotate in place, because the move method is called incorrectly

B. Roll along, but not in a realistic fashionC. Roll along in a realistic fashion

What does the code below do?It makes the ball…

(assume the ball is 1 meter wide)

Page 5: Chapter 6: (Expressions,) Functions, and If/Else

Can a function have parameters?

A. No, the goal of a function is to compute/return a value

B. No, a function should always compute/return the same value, so it cannot be varied by a parameter to the function

C. Yes, a function can compute/return a value that can be varied based on the parameters to the function

Page 6: Chapter 6: (Expressions,) Functions, and If/Else

Chapter 6: (Expressions,) Functions, and If/Else

• What if you want your world to react based on other characteristics?– What if you want your plane to burst into flame if

it tries to fly into the ground?• Before we get to if statements, we need functions – to

allow us to calculate when or how things should happen

Page 7: Chapter 6: (Expressions,) Functions, and If/Else

HHard-Coded:Not realistic

Ball’s size important

Use Expression toRepresent math

Hide MessyExpression

In Function

Page 8: Chapter 6: (Expressions,) Functions, and If/Else

Use Expression toRepresent math

Page 9: Chapter 6: (Expressions,) Functions, and If/Else

Sanity Check:

A. Yes, it makes sense that the big ball “turns” less than the small ball

B. No, we’ve messed something up, the small ball should “turn” less than the big ball

C. I don’t believe you – you did the math wrong, the way it’s written the big ball WILL turn more!

Page 10: Chapter 6: (Expressions,) Functions, and If/Else

First: Writing Expressions Whose Values Control Action

• Let’s make sure we know the difference between expressions and instructions

Page 11: Chapter 6: (Expressions,) Functions, and If/Else

How many of the underlined “items” result in values?

A) 1 B) 2 C)3 D) 4E)

Don’t Know

Page 12: Chapter 6: (Expressions,) Functions, and If/Else

An expression cannot stand alone• Expressions are a part of an instruction tile

– Not an instruction all on their own– They evaluate to a number like 2. What kind of instruction is

“2”?– They GUIDE how an instruction tile behaves

• Just like expressions in English– Parenthetical expressions are PART of an English sentence (not

something you say by itself)– The fruit fly, for example, can breed up to ten times in one

hour*– I was out all night and, consequently, didn’t finish my homework

*http://www.uhv.edu/ac/newsletters/writing/grammartip2006.08.29.htm

AND: It’s Purple!

Page 13: Chapter 6: (Expressions,) Functions, and If/Else

Transactive Discourse:No monologue-ing!

Ask for Feedback Paraphrase JustifyDo you agree with and understand me?

So, what you said was…

Why do you say that?

Page 14: Chapter 6: (Expressions,) Functions, and If/Else

Use Expression toRepresent math

Hide MessyExpression

In Function

Mathematical Expressions: Use them and Hide them away in Functions

Page 15: Chapter 6: (Expressions,) Functions, and If/Else

Mathematical Expressions: Use them and Hide them away in Functions

• You just need to be able to translate from “math” format (written) to Alice code

• Order of operations is taken care of – Alice fully parenthesizes

2 + 5* 4 / (4 - 2)

2 + 5* 4 / (4 - 2)

Page 16: Chapter 6: (Expressions,) Functions, and If/Else

In Alice: Often you will create expressions with unnecessary parentheses: It’s OK

• Demo (Part 1): Creating that value in Alice– Suppose we want to move a hare forward by

2 + 5* 4 / (4 - 2). Start off creating a move forward tile for the hare, and set the amount (initially) to 2 meters.

– Click on the arrow next to the 2 meters parameter, and (in the drop down menu)• Select math• Select 2 + then say other and enter 5

Page 17: Chapter 6: (Expressions,) Functions, and If/Else

In Alice: Often you will create expressions with unnecessary parentheses: It’s OK

• Demo (con’t): Creating that value in Alice– Click on the arrow RIGHT next to the 5, select math *

other and enter 4

– Click on the arrow after this parenthesisselect math / other 4

– Click on the arrow RIGHT next to the last 4, select math - 2

Page 18: Chapter 6: (Expressions,) Functions, and If/Else

Alice specifics: Where is the center?• When one object “move to”

another object, it’s center moves “on top” of the other object’s center– But centers vary by object

Page 19: Chapter 6: (Expressions,) Functions, and If/Else

Alice specifics: Where is the center?

Bee Center: Exact middle of height, width, depth

Tulip Center: At base of stem

Page 20: Chapter 6: (Expressions,) Functions, and If/Else

Read and Understand Expressions:The code below should make a bee fly to

perch PERFECTLY on the top of a tulip

If our tulip is 0.3 meters high and our fly is (very big) at 0.1 meters tall (high), how far UP will the bee move in the second instruction?

A) 0.2 meters B) 0.3 metersC) 0.35 meters D) It’s not possible to tellE) I don’t know

Page 21: Chapter 6: (Expressions,) Functions, and If/Else

In Discussion Someone Says:

This code will always cause the bee to move up 0.35 meters, no matter how you design your tulip and bee objects

A. I agree. The expression must always evaluate to 0.35 because that’s the way we designed it

B. I disagree. The expression will evaluate to different values based on the heights of our tulip and bee objects

C. It depends. We can’t tell if that’s true or not

Page 22: Chapter 6: (Expressions,) Functions, and If/Else

Why is this useful?Multiple Sized Tulip Demo

• We have a world with multiple tulips in it, they are of varying sizes.

• We want to USE THE SAME CODE (e.g. not have to write it multiple times) to have the bee fly to the top of any flower – We’ll do it once per flower

• Write a bee-class method– Parameter is what flower to fly to the top of

• Call that method three times

Page 23: Chapter 6: (Expressions,) Functions, and If/Else

Write expression to move to stand with feet on ground

Standing perfectly,Feet on ground

FeedbackParaphrase

Justify

Page 24: Chapter 6: (Expressions,) Functions, and If/Else

Putting it all togetherTo control some actions

REALISTICALLY we need to calculate values based on

objects in the world

We write EXPRESSIONS to represent the calculation required

Messy expressions can be hidden inside FUNCTIONS (sometimes needing parameters)

1

2

3

Page 25: Chapter 6: (Expressions,) Functions, and If/Else

Expression -> Function

• Demo:• Previously, we built up a kind of complex

expression that returns a numerical value

• Create a function to hide away complexity– World level function– Returns Number – Copy old expression to clipboard and drag back into

function

Page 26: Chapter 6: (Expressions,) Functions, and If/Else

Problem: How to make a RandomGuy walk up the side of a pyramid.

• Important 3-D facts: – Pyramid’s center is in middle of base– Height is the straight up and down height– Width/2 forms a triangle with height• Where the hypotenuse is the distance up the side of the

pyramida2 + b2 = c2

height*height + width*width

Length up side =

Page 27: Chapter 6: (Expressions,) Functions, and If/Else

Problem: How to make a RandomGuy walk up the side of a pyramid.

• Important 3-D facts: – Pyramid’s center is in middle of base– Height is the straight up and down height– Width/2 forms a triangle with height• Where the hypotenuse is the distance up the side of the

pyramida2 + b2 = c2

a

b

c

Page 28: Chapter 6: (Expressions,) Functions, and If/Else

Demo Notes: (note I have started the guy in the correct place)

1) Try to walk up pyramid using pyramid’s height2) Build expression to use as parameter to randomGuy.move forward

– Start with World function “square root of” pick a default value (like 1)

– Replace 1 with our expression left to right a bit at a time:• pyramid’s height*pyramid’s height*

(pyramid’s width / 2) * (pyramid’s width /2 )

3) Create a function to hide the complexity– Call it sideLength– It returns a number– Build the expression the exact same way, but replacing the 1

default in the return instruction/tile

PLAY: It works!

Page 29: Chapter 6: (Expressions,) Functions, and If/Else

The function we wrote

Page 30: Chapter 6: (Expressions,) Functions, and If/Else

What is the BEST description of why we would write a function?

A. There’s a set of complex actions that can be logically grouped together

B. There’s a complex calculation that produces a value needed to control the action

C. You need to calculate distances related to objects locations in 3-D space

D. I don’t know

FeedbackParaphrase

Justify

Page 31: Chapter 6: (Expressions,) Functions, and If/Else

Challenge Question:Methods have parameters, functions can too

The function “sideLength" is controlled by both the width and height of the pyramid. Why weren't those parameters to the function?

A. Beth messed up – they should have been parameters to the function, that was a bad design

B. We did pass parameters to the sideLength function – for example these:

C. We didn’t need to pass those values because sideLength is a function of the pyramid class

D. I don’t know

FeedbackParaphrase

Justify


Recommended