37
Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Embed Size (px)

Citation preview

Page 1: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Variables(Alice In Action, Ch 3)

Slides Credit: Joel Adams, Alice in Action

CS120 Lecture 04

7 September 2012

Page 2: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

2

Objectives

• Learn and use variables three ways:– Method (local): defined for use within a method

• e.g. to store values to be used later

– Parameter: variables passed to a method• to make methods more general and useful

– Object/Class (property): used to store an object property

• Understand the concept of variable scope.

• Understand Datatypes: Boolean, Number, String, Object

Page 3: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

3

Variables

• Variables: named containers for storing/retrieving data

• Three uses of variables– Method (local): defined for use within a method– Parameter: variables passed to a method– Object (property): used to store an object property

• Tools for creating method variables and parameters– Buttons in the upper-right corner of the method box

• Tool for creating object variables– Button under the property pane of the details area

Page 4: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Values and Datatypes

• Data in the computer is represented by values:– A value– A datatype

• Examples:– Numbers: 0, 1, 3, 4.5, 3.14, -125– Booleans: True, False– Strings: “Hello World”, “John”– Objects: whiteRabbit, troll2 (references to objects)

• Variables are storage locations for values.

Alice in Action with Java 4

Page 5: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Alice in Action with Java 5

Variables

Page 6: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

6

Method/Local Variables

• Defined using create new variable button

• Information needed to define a variable– Name: refers to a location in memory– Type: refers to kind of data stored; e.g., a number– Initial value: starting value of specified type; e.g., 1

• Method variables are local (valid only in the method)

• Common uses of method variables– Computing and storing values for later usage– Storing values entered by a user

Page 7: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

7

Example 1: Storing a Computed Value

• User story objects: nativeGirl and horse• Objective: move nativeGirl closer to horse • Strategy for moving the girl the right distance

– Define variable storing distance from the girl to horse– Ask the girl how far she is from the horse– Store the girl’s reply in the variable– Use variable’s current value in a move()message

• Preparing to program with user-defined variables – Position nativeGirl and horse in a new world– Review types: Number, Boolean, Object, Other

Page 8: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

8

Example 1: Storing a Computed Value (continued)

Page 9: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

9

Example 1: Storing a Computed Value (continued)

• Creating the first local variable– Click the create new variable button– Define Number type named distanceToHorse– Drag distanceToHorse to the editing area – Set the variable value using a placeholder value of 1– Click on functions pane for nativeGirl object– Drag distanceInFrontOf()onto placeholder– Specify the entire horse as the object argument

Page 10: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

10

Example 1: Storing a Computed Value (continued)

Page 11: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

11

Example 1: Storing a Computed Value (continued)

Page 12: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

12

Example 1: Storing a Computed Value (continued)

• Using distanceToHorse in the move() method– Drag the move()message into the editing area– Select Forward->expressions->distanceToHorse

• A test of method shows the girl too close to horse

• Adjusting the distance that the girl moves– Click the list arrow next to distanceToHorse– Select math->distanceToHorse-->.5

• Use functions and variables over trial-and-error– Behavior will adapt to repositioning of objects

Page 13: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Alice in Action with Java 13

Example 1: Storing a Computed Value (continued)

Page 14: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

14

Example 1: Storing a Computed Value (continued)

Page 15: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

15

Example 1: Storing a Computed Value (continued)

Page 16: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

16

Example 2: Storing a User-Entered Value

• User story built around the Pythagorean Theorem– skaterGirl requests the lengths of two sides– User passes edge1 and edge2 values to skaterGirl– skaterGirl computes hypotenuse and displays value

• Overview of implementation– Create skaterGirl method computeHypotenuse() – Declare three Number variables for triangle sides– Add two say()statements for interactive dialog– Add set() method for edge1– Incorporate NumberDialog into set()parameter list

Page 17: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

17

Example 2: Storing a User-Entered Value (continued)

Page 18: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

18

Example 2: Storing a User-Entered Value (continued)

Page 19: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

19

Example 2: Storing a User-Entered Value (continued)

• Overview of implementation (continued)– Add set()method for edge2– Incorporate NumberDialog into set()parameter list– Add set()method for hypotenuse– Drag and drop Math.sqrt()over placeholder value– Pass a mathematical expression to Math.sqrt()– Define String variable named hypotString – Convert hypotenuse to string and store in hypotString– Add say()method to display results– Display concatenates dialog and hypotString value

Page 20: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

20

Example 2: Storing a User-Entered Value (continued)

Page 21: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

21

Example 2: Storing a User-Entered Value (continued)

Page 22: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

22

Parameters

• Argument: value passed to an object via a message

• Parameter: variable that stores an argument

• Example using the say() message– The parameter is a single variable of String type– One possible argument is “Oh, hello there!”

• You may pass multiple arguments to messages– Example: roll() message requires two arguments

Page 23: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

23

Example 1: Old MacDonald Had a Farm

• Objective: display song lyrics for four animals

• Use of parameters helps avoid repetitious coding

• Two parameters needed: animal and noise

• Overview of implementation – Create new world with scarecrow before fence– Create singVerse()method for scarecrow– Add two parameters, animal and noise – Add String types, firstLine and doubleNoise– Incorporate parameters and variables in say()methods – Call singVerse()messages in singOldMacDonald()

Page 24: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

24

Example 1: Old MacDonald Had a Farm (continued)

Page 25: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

25

Example 1: Old MacDonald Had a Farm (continued)

Page 26: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

26

Example 1: Old MacDonald Had a Farm (continued)

Page 27: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

27

Example 2: Jumping Fish!

• User story: fish jumps, traces an arc, returns to water• Fish object used is an instance of Pinkminnow• Basic elements in the design of jump()method

– One parameter: distance– Local var: height, halfDist, startStopDist, angle– Approximate height of jump as 1/3 of the distance moved– Assume starting/stopping point is 1/2 distance of jump– Set the pitch angle to .1

• The jump()method is implemented in Figure 3-35 • Program should be tested with multiple distance values

Page 28: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

28

Example 2: Jumping Fish! (continued)

Page 29: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Alice in Action with Java 29

Example 2: Jumping Fish! (continued)

Page 30: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

30

Example 2: Jumping Fish! (continued)

Page 31: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

31

Property Variables

• Also called instance variables or object variables or class variables

• Property variables are defined within an object – Use the properties pane of the object’s details area

• Values in an object variable are not shared • How to create and use a property variable

– Open a new world and add a wizard object– Click on wizard properties – Click create new variable– Define String type (in Other list) called myName– Initialize myName to Jim

Page 32: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

32

Property Variables (continued)

Page 33: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

33

Property Variables (continued)

• How to create and use a property variable (continued)– Duplicate the wizard using copy button – Close the Add Objects window– Click on the second wizard in the object tree– Click the properties tab in the details area– Change the second wizard’s name from Jim to Tim

• Testing the identity of the wizards– Use say()messages to display each wizard’s name– Argument used: “My name is “ + wizard.myName

• Check object for predefined properties; e.g., opacity

Page 34: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

34

Property Variables (continued)

Page 35: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

35

Property Variables (continued)

Page 36: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

36

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

Page 37: Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012

Student To-Do’s

• Reading:– Alice In Action, Chapter 3

• HW01 Due Monday 9/10 @ lab time (do both)– Create your own story or movie scene– Add scenes 1 and 3 to Wizard/Trolls story

• Lab on Monday– We’ll practice with methods and parameters.– Bring your HW to demo during the lab time.

Alice in Action with Java 37