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

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Class-level Methods Alice. World / Class Method World method A general method that may refer to multiple objects; not closely associated with any particular.
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.
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.
Alice in Action with Java
Chapter 2 Review Questions
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Alice in Action with Java
Alice in Action with Java
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Alice in Action with Java
Calvin and Hobbes Teach Properties and Functions Created by Daniel MacDonald under the direction of Professor Susan Rodger Duke University June 2013.
Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Programming Logic Program Design. Objectives Steps in program development Algorithms and Pseudocode Data Activity: Alice program.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Chapter 1 - Getting to know Greenfoot
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exam scores by range 3-1.
Creating Buttons – Lesson 71 Creating Buttons Lesson 7.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
Introduction to Alice Alice is named in honor of Lewis Carroll’s Alice in Wonderland Slides Credit: Joel Adams, Alice in Action CS120 Lecture August,
Flow Control: Repetition with For Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September.
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1,
Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Methods and Data Programming with Alice and Java First Edition.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
Alice in Action with Java Chapter 5 Lists and Arrays.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Methods (part 2) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 03 4 September 2012.
Video in Macromedia Flash (Optional) – Lesson 121 Video in Macromedia Flash (Optional) Lesson 12.
Flow Control: Repetition with While Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Alice in Action with Java Chapter 4 Flow Control.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture August 2012.
Alice in Action with Java Chapter 4 Flow Control.
Integrating Components and Dynamic Text Boxes with the Animated Map– Lesson 101 Integrating Components and Dynamic Text Boxes with the Animated Map Lesson.
Alice in Action with Java Chapter 1 Getting Started with Alice.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
Functions (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Alice in Action with Java Chapter 2 Methods. Alice in Action with Java2 Objectives Build world-level methods to help organize a story into scenes and.
var variableName:datatype;
Programming Logic and Design Seventh Edition
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.
Parameters Section 8-8 Web Design.
Chapter 3: Variables, Functions, Math, and Strings
Chapter 3: Variables, Functions, Math, and Strings
Programming Logic Alice.
Alice in Action with Java
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Parameters Alice.
Alice in Action with Java
Alice in Action with Java
Parameters and World-level methods
Checking for Collisions: Using Functions in Alice
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Parameters Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Presentation transcript:

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

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

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 Java4

5 Variables

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

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

8 Example 1: Storing a Computed Value (continued)

9 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

10 Example 1: Storing a Computed Value (continued)

11 Example 1: Storing a Computed Value (continued)

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

Alice in Action with Java13 Example 1: Storing a Computed Value (continued)

14 Example 1: Storing a Computed Value (continued)

15 Example 1: Storing a Computed Value (continued)

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

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

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

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

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

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

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

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()

24 Example 1: Old MacDonald Had a Farm (continued)

25 Example 1: Old MacDonald Had a Farm (continued)

26 Example 1: Old MacDonald Had a Farm (continued)

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

28 Example 2: Jumping Fish! (continued)

Alice in Action with Java29 Example 2: Jumping Fish! (continued)

30 Example 2: Jumping Fish! (continued)

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

32 Property Variables (continued)

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

34 Property Variables (continued)

35 Property Variables (continued)

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

Student To-Do’s Reading: –Alice In Action, Chapter 3 HW01 Due Monday 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 Java37