Functions (Alice In Action, Ch 3) 17 July 2013 Slides Credit: Joel - - PDF document

functions alice in action ch 3
SMART_READER_LITE
LIVE PREVIEW

Functions (Alice In Action, Ch 3) 17 July 2013 Slides Credit: Joel - - PDF document

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


slide-1
SLIDE 1

1

Functions (Alice In Action, Ch 3)

Slides Credit: Joel Adams, Alice in Action

CS 101 Lecture 17

17 July 2013

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.
slide-2
SLIDE 2

2

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

Alice Tip: Using the Vehicle Property (continued)

slide-3
SLIDE 3

3

5

Alice Tip: Using the Vehicle Property (continued)

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.

slide-4
SLIDE 4

4

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

Example: Retrieving an Attribute from an Object (continued)

slide-5
SLIDE 5

5

Alice in Action with Java 9

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

Example: Retrieving an Attribute from an Object (continued)

slide-6
SLIDE 6

6

11

Example: Retrieving an Attribute from an Object (continued)

12

Example: Retrieving an Attribute from an Object (continued)

slide-7
SLIDE 7

7

13

Functions with Parameters

  • Functions can use parameters to store arguments
  • Adapting skaterGirl 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

Functions with Parameters (continued)

slide-8
SLIDE 8

8

Alice in Action with Java 15

Functions with Parameters (continued)

16

Summary

  • Variable: named container for storing and retrieving

values

  • Types of variables: method variables, parameters,
  • bject variables
  • Data types: Number, Boolean, Object, String,…
  • Parameter: container for an argument
  • Argument: data passed to a method or function
slide-9
SLIDE 9

9

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.

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