Functions Alice.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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: Simple Control Structures Part 1 – Conditional Execution Alice.
Introducing While loops (and random numbers too) Alice.
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.
Tips & Techniques 6 Random Numbers and Random Motion Alice.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Execution Control with If/Else and Boolean Functions Example: Single Condition Alice.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
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.
Execution Control with If/Else and Boolean Functions Sec 52 Web Design.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Alice: Functions Alice Chapter 6 September 19, 2005.
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Creating An Animation Program Part 2 Alice. Method A segment of program code (instructions) that defines how to perform a specific task.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Execution Control with If/Else and Boolean Functions Alice.
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.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Programming: Putting Together the Pieces Built-in Questions 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.
Programming: Simple Control Structures
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
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.
Lists Alice. Collections In some animations, several objects must perform the same actions Example: A marching band, where the band members are performing.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Programming: Simple Control Structures Sec 46 Web Design.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
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.
Learn about functions in Alice
Programming: Simple Control Structures
Creating your Function
Programming: Simple Control Structures
Functions Sec 8-11 Web Design.
Programming: Simple Control Structures
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Functions Alice.
Programming: Simple Control Structures
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Presentation transcript:

Functions Alice

Functions in Alice In Java, we created methods like public void makeSquare() A void function is like a method in Alice A non-void function returns information In Alice, this is a function

How a function works A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Types of Functions The type of a function depends on the type of value it returns. a number a specific object a color a Boolean (true or false) other property values…

A new example A common task in sports game programs is to bounce a ball. To illustrate how this is done, let's bounce the ball over the net. (The ball has been positioned 1 meter from the net.) The ball should move up and forward and then down and forward in a pattern that looks something like this: Note: This looks easy – but do not be deceived!

Design Storyboard A design for a world-level method: World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward toyball move down

Demo ToyballOverNet-V1 ToyballOverNet-V2 Concepts illustrated in this example: Movement of an object is sometimes relative to another object. In this example, a built-in height function is used to determine the height of the net. Then the ball moves up enough to clear the net. the toyball's up and down movements are relative to the ground. An orient To method (alternatively stand Up) is used because it is not easy to tell "which way is up." Traditional lecture: demo version 1 and then orient the ball to the ground to show the solution. Active learning: demo version 1 and then have students orient the ball to the ground.

Rolling the ball Another sports game action involves rolling a ball along the ground. We want a realistic motion rather than a slide. The ball must simultaneously move and roll. realisticRoll Do together move ball forward 1 meter turn ball forward 1 revolution

Demo Ch06Lec1ToyballRoll-V1 Ch06Lec1ToyballRoll-V2 Our design assumed that the ball's motion is relative to the ground. But the ball turns relative to it's own sense of direction. Ch06Lec1ToyballRoll-V2 AsSeenBy ground can be used to make the ball turn relative to the ground.

Revising the approach The ball is made to roll 1 revolution. Suppose we want the ball to roll a certain distance (say 3 or 4 or even 10 meters) along the ground. How many times should the ball turn 1 complete revolution?

Number of revolutions The number of revolutions depends on the size of the ball The number of revolutions is distance/( Pi * diameter) But there is no built-in function to return the number of revolutions We will write our own! one revolution four revolutions

Parameters We want to return the value computed as distance/( P * diameter) Obviously, what is needed is the ball’s diameter the ball object has a built-in width function the distance the ball is to travel can be sent as a parameter to the function

Demo Ch06Lec1ToyballRoll-V3 Concepts illustrated in this example A function must have a return statement to send back a computed value. In this example, a math expression is created to compute a number value. The order of evaluation is indicated by the use of parentheses, as in traditional math expressions.

Calling the function test values Active Learning: Students run the world several times, with different test values. test values We should run the animation with several test values to be sure it works as expected. What happens if you use a negative value?

Levels of Functions As with methods, functions can be either class-level or world-level. (The function just presented was class-level.) The guidelines for class-level methods also apply to class-level functions: No references to other objects. No references to world-level functions you have written (built-in world-level functions are fine to use).

Assignment Read Chapter 6 Section1, Functions