CS 101 Lecture 17 Functions (Alice In Action, Ch 3) 17 July 2013 Slides Credit: Joel Adams, Alice in Action Objectives • Create functions — messages that return a value to their sender • Learn Alice techniques: the vehicle property for synchronizing the movements of two objects • Understand the concept of variable scope. 2 1
Alice Tip: Using the Vehicle Property • Vehicle – The thing on which an object “rides” – The default vehicle for an object is the world • Vehicle property is used to synchronize movements • Using vehicle property in program with girl and horse – Scene 4 initially shows the nativeGirl on a horse – Add set() method for nativeGirl ’s vehicle property – Set the value of the vehicle to the entire horse – Include a move() method below the set() method – Send playScene4() to my_first_method() and test 3 Alice Tip: Using the Vehicle Property (continued) 4 2
Alice Tip: Using the Vehicle Property (continued) 5 Functions • Messages used to retrieve information • May be defined by the user • Methods and Functions are essentially the same. – Both are a way give a name to a block of code so it can be reused (eg. flapWings) and generalized (singVerse). – Methods generally do something, but do not return any information – Functions return information. 6 3
Example: Retrieving an Attribute from an Object • User story involves program with twin wizards – Objective: enhance program by using titles with names – Jim uses The Enchanter, and Tim uses The Magus • Creating the title property – Define property variable myTitle with type String – Assign title values to each wizard ’s myTitle variable • Simplest way to access name and title – Pass myTitle concatenated with myName to say() – Problem: extra time needed to build the full name – Alternative: use a function to produce the full name 7 Example: Retrieving an Attribute from an Object (continued) 8 4
Example: Retrieving an Attribute from an Object (continued) • Defining a function to produce a full name – Select wizard object and then click functions pane – Click the create new function button – Declare function called getFullName as String type – Create variable called fullName – Set value of fullName to concatenated name and tile – Include fullName in the return statement • Defining a simpler function for the second wizard – Return myTitle concatenated with myName • Send both functions to my_first_method Alice in Action with Java 9 Example: Retrieving an Attribute from an Object (continued) 10 5
Example: Retrieving an Attribute from an Object (continued) 11 Example: Retrieving an Attribute from an Object (continued) 12 6
Functions with Parameters • Functions can use parameters to store arguments • Adapting skaterGir l program to use functions – Create Number type called calculateHypotenuse() – Add parameters Number , leg1 , and leg2 – Create local variable named hypot – Set value of hypot to mathematical expression – Incorporate new function into computeHypotenuse() • Comparing functions to methods – Functions return a value and appear in parameter list – Methods do not return a value and appear as statements 13 Functions with Parameters (continued) 14 7
Functions with Parameters (continued) Alice in Action with Java 15 Summary • Variable: named container for storing and retrieving values • Types of variables: method variables, parameters, object variables • Data types: Number , Boolean , Object , String,… • Parameter: container for an argument • Argument: data passed to a method or function 16 8
Summary (continued) • A method does not return a value to its sender • A method is sent from a place where a statement can appear • A function does return a value to its sender • A function is sent from a place where a value can appear • Object vehicle : thing on which an object “rides” • Variable scope: Where a variable can be accessed. 17 Student To Do’s • Readings: – Alice in Action, Chapter 3 (This week) – Alice in Action, Chapter 4 (Next Week) • This week’s Alice HW (Due Thursday night): • Problem 2.5 (loopDeeLoop) • Problem 2.9 (add scenes 1 and 3 to the wizard/troll animation). • Next week’s Alice HW (to be assigned – if you want to get a head start): • 3.3 (walking in square) • 3.9 (shrink the trolls) • Quiz 2 on Thursday 18 9
Recommend
More recommend