Variables and Functions Chapter 3 3-1. Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.

Slides:



Advertisements
Similar presentations
SimpleGameInAlice1 Barb Ericson Georgia Institute of Technology June 2008 Creating a Simple Game in Alice.
Advertisements

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.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Repetition Structures
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Functions and Visual Effects Chapter 6 Part 3. Built-in Functions We have been using built-in functions so far 2.
Alice: A Visual Introduction to Programming Chapter 1 Part 3.
Programming in Alice Chapter 2. Today’s Agenda Designing a Program Writing Methods Executing Instructions Simultaneously Comments Tips for Setting Up.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley This week: Whew!!! The last homework was tough! The homework for this week.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Input, Output, and Processing
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exam scores by range 3-1.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Variables & Random Number Generation.  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third 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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
5 Event Handling Interactive Programming Suggested Reading Interaction: Events and Event Handling, Supplemental Text for CPSC 203 Distributed this term.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
Creating a UFO Rescue Game in Alice
Programming in Alice Chapter 2.
Excel Tutorial 8 Developing an Excel Application
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Chapter 3: Variables, Functions, Math, and Strings
Chapter 3: Variables, Functions, Math, and Strings
2.5 Another Java Application: Adding Integers
Let's Race! Typing on the Home Row
Creating a UFO Rescue Game in Alice
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Building Java Programs
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
ICT Gaming Lesson 3.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions 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 and Functions Chapter 3 3-1

Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in a variable Types of data that can be stored o Local variables o Parameter variables o Class-level variables o World-level variables 3-2

Variable Types Local variables o Belongs to a specific method o Only used in that method o Variables cease to exist when method stops Parameter variables o Holds argument that is passed to a called method Class-level variables o Variable that belong to a specific object World-level variables o Variable that belongs to the world 3-3

Creating Local Variables Local variables belong to a specific method (such as world.my first method) Create variable by clicking on create new variable in desired method Variables require 3 things o Name o Type o Initial value 3-4 name type value

Names, Types and Initial Values Variable Names Must be unique within the method Should be meaningful and reflect the variable’s purpose Named using camelCase Variable Types Numbers, Boolean, Objects, Other (such as String, Color, or Sound) Initial Value Value initially stored in the variable 3-5

Variables Tile appearance shows the type of variable Numbers, Boolean, Objects, String Variable declaration - creating a variable 3-6 variable area

Variable Assignment Variables have an initial value Initial value held until a different value is assigned New values can be assigned while the method is running o Use Set instructions o Done by dragging variable tile into Methods Editor 3-7

Checkpoint What is a variable? What are three things that must be specified when creating a new variable? In what part of the Method Editor do variable tiles appear? What do the characters 123 appearing on a variable tile tell you? How do you create a set instruction for a variable? 3-8

Function Asks a question o Checks a condition o Computes a value Used to get information about… o Properties of objects  Is snowwoman's face red? o Relationship of one object to another  What is distance between lunar robot and rock? o A current condition  What keyboard key was pressed?

World Level Primitive Functions  List of functions categories o Boolean logic o Math o Random o String o Ask user o Mouse o Time o Advanced math o Other 3-10

Value What a function returns after it o Asks question o Performs computation Type of value depends on kind of function o In lunar example  What is distance of robot to rock? o Number value is expected to be returned  Whole number ex: 3 meters  Fractional value, ex: 1.2 meters

Types of Values Number o As in example Boolean o true, false String o “Slithy Toves” Object o snowman, helicopter Position in world o (0, 0, 0) – center of an Alice world

Asking User for Input  World has three primitive functions that ask for user input  Each function is displayed in a dialog box 3-13 Is it Yes or No?

Adding Question for Number

Class Primitive Object Functions  Objects have different primitive functions from the world o Proximity o Size o Spatial relation o Point of view o Other Object primitive proximity functions

Using Function distance to Built-in distance to function o Determines distance from center of one object to center of another object o Better than guessing distance to object An Alice function is not a "stand-alone" instruction o It is nested within another instruction

Checkpoint What makes a function different from a method? How do you see a list of the functions an object has? What are the three primitive world functions that ask the user to input a value? What are some class level functions? What does the primitive distance to function do? 3-17

Creating Math Expressions  Math Operators are used to create math expressions  Math expressions perform a calculation and return a value  Simple math expression o o Value returned is 14  Operands appear to the right and left of an operator o Example with variables “payRate” and “hoursWorked”  payRate * hoursWorked OperatorDescription +Addition -Subtraction *Multiplication /Division 3-18

Determining Distance Camera angle influences our perception of scene We need assistance with determining distance Example: How many meters does robot need to walk to reach rocks where alien is hiding? o Can use  pointOfView property  Primitive function

Avoiding Collisions When program is run, robot collides with rock o Distance between two objects is measured center- to-center One way to avoid a collision o Reduce calculated distance to object o Use expression to subtract small number (1 or 2) from distance

Expressions in Arguments Math expressions can be used in place of o Fixed value o To adjust value returned by function Click down arrow to get math operation o Adjusts distance to move to not collide with rock

Adding Math Expression in Alice 3-22

Subtracting Functions  Two meters subtracted from distance is arbitrary  To be more precise, subtract width of rock o But still not accurate  Resulting expression subtracts value of one function from value of another function

More Precision The most accurate way to determine distance and to avoid collision o Add half width of both objects subtract from distance  Be careful of excessive parentheses

Checkpoint What do the following operators do in most programming languages? o + - * / What does a math expression do? When you use the move to instruction to move one object to another object, why do they collide? o What are multiple methods you can use to avoid this? When can an object be driven into the ground or above the ground when using move to or move toward instructions? 3-25

Working with Strings and Text  A string is a sequence of characters  Strings represent o Names o Addresses o Messages o etc. 3-26

Asking User to Enter a String  World’s ask user function can ask user to enter string  Function returns the text that the user enters  Example displays what user entered in dialog bubble 3-27 Defines string variable called name Asks user for name Stores in variable name Puts contents of name variable in bubble

Joining Strings  String Concatenation o Joins string a with string b o Strings a and b are arguments o Does not change the values of strings a or b  World function o a joined with b  Drag a joined with b from string section in World function to where you want to join strings 3-28

Example of a joined with b 3-29 Hello joined with was only thing added

Converting Numbers to Strings Sometimes you need to display numbers to user Number needs to be converted to string first Use world functionWhat as a string

Example of what as a string 3-31

3D Text can be added to Alice worlds Found at end of the local gallery Pop up window appears after clicking on it Enter text and font style 3-32

Homework Read chapter 3 Do tutorials o Tutorial 3-1 Creating and using a variable, p 116 o Tutorial 3-2, Creating a set instruction, p. 118 o Tutorial 3-3, Calling an ask user function, p. 122 o Tutorial 3-4, Using a proximity function, p. 127 o Tutorial 3-5 Using math to avoid collisions, p 132 o Tutorial 3-6, Converting a number variable to a string, p. 141 Answer questions in handout Do lab assignments after handing in answered questions Due one week after assigned with 1 week grace