Learn about functions in Alice

Slides:



Advertisements
Similar presentations
CS320n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4-3-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger.
Advertisements

Class-level Methods Alice. World / Class Method World method A general method that may refer to multiple objects; not closely associated with any particular.
Class-level Methods and Inheritance Part 1 Alice.
Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
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.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Class-level Methods and Inheritance MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Creating An Animation Program Alice Web Design Section 8-4.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Alice: Functions Alice Chapter 6 September 19, 2005.
CS329e – Elements of Visual Programming Implementing Programs Mike Scott (Slides 2-2)
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.
Alice Program Design and Implementation. Scenarios and storyboards The previous magician example illustrated a simple storyboard which depicts a scenario.
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.
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1,
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
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.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
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.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Obj: to recognize built-in functions and expressions HW: Read Section 3.1 and do HW4 Edmodo article due Sunday at 8pm Edmodo feedback due 8pm 10/17 Do.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Functions Sec 51 Web Design.
Professor Susan Rodger
Creating your Function
Variables and Inheritance Part 2
Functions and Expressions
Functions Sec 8-11 Web Design.
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
CS320n –Visual Programming
Refining our Animations
Classes, Objects, and World-level Methods
Class-level Methods Alice.
Arithmetic Expressions
Parameters and World-level methods
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
How Tall Are You? Introducing Functions
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Class-level Methods and Inheritance
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Class-level Methods and Inheritance Part 2
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Variables and Inheritance A More Complex Example
Presentation transcript:

Learn about functions in Alice What we will do today Learn about functions in Alice

Functionality A function asks a question and give an answer Receives value(s) Performs computation on value(s) Returns (sends back) a value

Types of functions The type of a function depends on the type of value it returns a calculated value (a number) a specific object a color etc. 42

Built-in/user defined functions A built-in function is a previously existing function. You do not need to create Built-in functions. A User defined function is a function that you need to design and implement. You are adding functionality to the class when you add a user defined function. Distance between center points. 4

Built-in functions Used one of Alice’s built-in functions skateAround method for the cleverSkater asked for the distance between the advanced skater and the object, target. (In the specific example shown, target was a penguin.) Distance between center points.

Example How do we bounce a ball? Let’s bounce a ball over a net Ball is 1 meter from the net to start Ball should move up and forward, then down and forward Note: Looks easy – but do not be deceived!

Design Storyboard A possible storyboard To reach the top of the net, we know the ball should move forward 1 meter (we positioned the ball 1 meter in front of the net) but how far upward should the ball move to clear the net? World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward toyball move down

Height Can use the built-in height function to determine the height of the net and move the ball up that distance Demo – ballOverNetWork – what happens?

Problem The ball does not bounce over the net The problem – cannot tell “which way is up” from the perspective of the ball

Solution We think “up” and “down” relative to the ground – so can orient the ball (and net) with the ground Now, ball will bounce over the net

Realistic Motion Turn the camera to look at the bounce from the side. Is it realistic? The default style for motion is “gently” begin and end gently can get more realistic by making forward motion abrupt, up motion end gently, and down motion begin gently

Adjusting Style adjusting the style of motion makes the bounce more realistic

Rolling the ball How do we roll a ball along the ground? Want a realistic motion rather than a slide The ball must simultaneously move and roll.

Demo: A first attempt

Revising the Approach The ball is made to roll 1 revolution. What if we want the ball to roll a certain distance? How can we make the ball roll the correct number of revolutions to cover a given distance along the ground?

Math to the rescue! The number of revolutions depends on the size of the ball The number of revolutions is distance / (Pi * diameter) There is no built-in function to return the number of revolutions Must write it!

Parameters We want to return the value computed as Distance / Pi * diameter Obviously, what is needed The ball’s diameter The ball object has a built-in width question The distance the ball is to travel Can be sent as a parameter to the question

Parameters/Arguments Arguments is the Object/value passed to the function Parameter is the alias (for the argument) used by the function. 18

numberOfRevolutions function distance is the parameter

Building a numberOfRevolutions function Step 1 Step 2: Drag distance parameter to replace 1 in return

The numberOfRevolutions function Step 3: Select math and divide. Select other and key in 3.14

The numberOfRevolutions function Step 4: Select the 3.14. Pick math and 3.14 * Select 1

The numberOfRevolutions function Step 5: Replace the 1 with the width of the ball from the functions

Demo: Calling the Function 10 is a test value. We should run the animation with several test values to be sure it works as expected. What happens if you use a negative value?

Creating a Roll Method Create a class level method for realistic roll problem: amount of time to roll. (Demo) Elements of Visual Programming Functions

Fixing Realistic roll Set duration of to function of distance. Try 2 meters per second Fixes one problem, but what happens with negative numbers? What is result of -5 / 2? Fix, using a world level method that finds the absolute value of a number

Levels of functions As with methods, you can write functions as either class-level or world-level. (what was the function we just wrote?) Guidelines for class-level methods apply to class-level functions: No references to other objects No references to world-level functions Built-in world-level functions are ok

Classwork today

Labs Car Pyramid Make sure all tires rotate the appropriate amount Have the mummy approach the pyramid Have the mummy scale the side the pyramid Have the mummy stop at the top