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.

Slides:



Advertisements
Similar presentations
Objects in Alice: Positioning and Moving Them By Jenna Hayes under the direction of Professor Susan Rodger Duke University July
Advertisements

Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010 modified July 2011.
As you come in…  Sign in (in back) and pick up  Badge  Name Card – write your first name LARGELY on back  Log in:  Launch/Start Alice  Any questions?
Princess & Dragon – Version 2 By Michael Hoyle under the direction of Professor Susan Rodger Duke University July 2012 Part 2: Using Methods.
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Princess & Dragon Part 2: Teaching a Dragon to Fly—Methods & Properties By Elizabeth Liang under the direction of Professor Susan Rodger Duke University.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 4.3 Class level methods (versus previous world-level methods)
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Lecture 8 Lab 4: It’s up! (Thursdays we post next week’s) – Plan out what you want, bring an implementation strategy diagram – Key Lab: If you’ve been.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Wizard Game: Class-Level Variables in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Lesson 4: Percentage of Amounts.
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
As you come in…  DOWNLOADS FOR TODAY:  CarGameTeacherStarter.a2w  Online student textbook.
Checking for Collisions Ellen Yuan Under the direction of Professor Susan Rodger at Duke University June 2014.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Making a Timer in Alice.
Lecture 5: Finishing 4.3 and onto 5.1 Preparing for the Midterm: Written Explanations Technology and Society Assignment (5% - 3 in term) – How does the.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Checking for Collisions: Alternative Method Erin Taylor Under the Direction of Susan Rodger July 2015 Duke University.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
Tutorial on Kangaroo visiting his animal friends - parameters, events.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
CONFIDENTIAL1 Good Afternoon! Today we will be learning about Number stories & expressions Let’s warm up : Simplify. Follow the order of operations:- 1)
Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
What we will do today Learn about functions in Alice.
Functions Alice.
An Introduction to Alice (Short Version) – Extras! Yossra Hamid Under the Supervision of Professor Susan Rodger Duke University, June 2014 This is a continuation.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1,
Mathematical Expressions, Conditional Statements, Control Structures
Chapter 6.2 Execution Control with If/Else and Boolean Functions.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
1 Quiz Template: Using the ‘ask user’ functions By Deborah Nelson Duke University Under the direction of Professor Susan Rodger July 2009.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Tutorial on Kangaroo visiting his animal friends - parameters, events Susan Rodger Duke University June 2009.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
An Introduction to Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University, June 2009 Updated June 2014 by Ellen Yuan.
Order of Operations Tutorial By: Melissa Griswold Lesson Examples Assessment Use the navigation buttons on the left to begin. If you are just beginning.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
How Tall Are You? Introducing Functions for Alice 3 By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
Functions Sec 51 Web Design.
Learn about functions in Alice
Creating your Function
Simple Control Structures
Functions Sec 8-11 Web Design.
Arithmetic Expressions
Using Functions in Alice
How Tall Are You? Introducing Functions
Functions Alice.
Functions Alice.
Functions Alice.
Functions Alice.
Presentation transcript:

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

Chapter 6.1 Expressions and Functions

Pick the best description of the purpose of methods and functions in Alice MethodsFunctions APerform actions in the world Return a value B Perform actions in the world C Change the state of the world numerically D Perform actions in the world

A.Rotate in place, because the move method is called incorrectly B.Roll along, but not in a realistic fashion C.Roll along in a realistic fashion What does the code below do? It makes the ball… (assume the ball is 1 meter wide)

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

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

H Hard-Coded: Not realistic Ball’s size important Use Expression to Represent math Hide Messy Expression In Function

Use Expression to Represent math

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!

First: Writing Expressions Whose Values Control Action Let’s make sure we know the difference between expressions and instructions

How many of the underlined “items” result in values? A)1B) 2C)3D) 4E) Don’t Know

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 * AND: It’s Purple!

Transactive Discourse: No monologue-ing! Ask for FeedbackParaphraseJustify Do you agree with and understand me? So, what you said was… Why do you say that?

Use Expression to Represent math Hide Messy Expression In Function 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)

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

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 parenthesis select math / other 4 – Click on the arrow RIGHT next to the last 4, select math - 2

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

Alice specifics: Where is the center? Bee Center: Exact middle of height, width, depth Tulip Center: At base of stem

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 meters C)0.35 metersD) It’s not possible to tell E) I don’t know

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

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

Write expression to move to stand with feet on ground Standing perfectly, Feet on ground Feedback Paraphrase Justify

Putting it all together To 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

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

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 pyramid a 2 + b 2 = c 2 height*height + width*width Length up side =

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 pyramid a 2 + b 2 = c 2 a b c

Demo Notes: (note I have started the guy in the correct place) 1) Try to walk up pyramid using pyramid’s height 2) 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!

The function we wrote

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 Feedback Paraphrase Justify

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 Feedback Paraphrase Justify