Download presentation
Presentation is loading. Please wait.
Published byMarcus Stone Modified over 8 years ago
1
Functions (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 05 11 September 2012
2
2 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.
3
3 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
4
4 Alice Tip: Using the Vehicle Property (continued)
5
5
6
6 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.
7
7 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
8
8 Example: Retrieving an Attribute from an Object (continued)
9
Alice in Action with Java9 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
10
10 Example: Retrieving an Attribute from an Object (continued)
11
11 Example: Retrieving an Attribute from an Object (continued)
12
12 Example: Retrieving an Attribute from an Object (continued)
13
13 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
14
14 Functions with Parameters (continued)
15
Alice in Action with Java15 Functions with Parameters (continued)
16
16 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
17
17 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.
18
Student To Do’s Readings: –Alice in Action, Chapter 3 (This week) –Alice in Action, Chapter 4 (Starting Friday) This week’s Alice HW (Due Monday): Problem 2.5 (loopDeeLoop) Problem 3.9 (shrink the trolls) –Be sure to use an Object parameter as the target. –Use Object methods for both! –Comment your code (part of the HW grade). 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.