Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."— Presentation transcript:

1 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition by Tony Gaddis Chapter 3: Variables, Functions, Math, and Strings

2 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Programs may need to store data when running and it stores that data in a variable A variable is a named storage location in the computer’s memory The following types of data (or variables) can be stored: –local variables –parameter variables –class-level variables –world-level variables 3-2 3.1

3 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variable Types Local variables belongs to a specific method used only by the instructions that the variable belongs to when a method stops, the variables cease to exist Parameter variables used to hold an argument that is passed to a called method Class-level variables variable that belongs to a specific object World-level variables variable that belongs to the world 3-3 3.1

4 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating Local Variables Local variables belong to a specific method (such as world.my first method) Variables require 3 things: –name –type –initial value 3-4 name type value 3.1

5 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Names and 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 3.1

6 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Tile appearance shows the type of variable Creating the tile is called variable declaration 3-6 3.1 variable area

7 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 –Set instructions can be created to store (or set) different values –Request to set a value occurs when variable tile dropped into Methods Editor 3-7 3.1

8 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variable Task…… -Complete Tutorial 3-1 Creating and using a variable on page 116-117 -Complete tutorial 3-2 Creating a set instruction for a variable on pages 118-119 3-8

9 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Functions Each object has a set of primitive functions List of functions is categorized –Boolean logic –Math –Random –String –Ask user –Mouse –Time –Advanced math –Other 3-9 3.2

10 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Asking the User for Input World has three primitive functions that ask for user input Each function is displayed in a dialog box 3-10 3.2 Is it Yes or is it No?

11 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Primitive Object Functions Each object has primitive functions different from the world’s primitive functions –Proximity –Size –Spatial relation –Point of view –Other 3-11 3.2

12 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task…. Create the tutorial on page 122 which is called Calling an ask user function on pages 122- 125(You will use Tutorial 3-2 the jumping penguins for this task) Create tutorial 3-4 called Using a proximity function on pages 127 -129 3-12

13 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Creating Math Expressions Math Operators are used to create math expressions Math expressions perform a calculation and return a value Operands appear to the right and left of an operand payRate * hoursWorked –“payRate” and “hoursWorked” are operands OperatorDescription +Addition -Subtraction *Multiplication /Division 3-13 3.3

14 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task…….. Complete the Using Math to avoid collisions within Tutorial 3-5 on pages 132-138 Keep Tutorial 3-5 open and add the Nerd and the methods that are located on page 139. Remember to first create a variable called name, then drag the variable into the method to assign an initial value, then you will use the worlds function to ask user for a string and finally use the Nerds method to say what was typed. Show Mr. Smith when finished with both tasks 3-14

15 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Strings and Text A string is a sequence of characters Strings represent: –names –addresses –messages –etc. 3-15 3.4

16 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Asking the User to Enter a String World’s primitive function can ask use to enter string (text) The function returns the text the user enters Joining separate sets of strings is called concatenation –“Hello” joins with the user’s name 3-16 What’s your name? Hello, Jo! 3.4

17 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Joining Strings String Concatenation –Joins string a with string b –Strings a and b are arguments –Does not change the values of strings a or b…but combines the two strings Strings are NOT just text… but any alphanumeric character Can convert numbers to a string for use as well 3-17 3.4

18 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task…. Complete the tutorial on page 141 3-6 called Converting a Number variable to a string. Add the following into tutorial 3-6 Ask the user to enter in three numbers(remember you will need a variable to store each of these). Next average the three numbers that were entered and display the results by Bob telling the user the average of what they entered. Show Mr. Smith when finished with both tasks. 3-18

19 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Text can be added to Alice worlds 3D text option is available at the end of the local gallery 3-19 3.4

20 Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Skills Check…… Students will choose three of the following to complete from pages 146 – 148. (50 points each) -Exercise 1,3,4,5 Students should complete the following challenge programs(100 points each) 7&9 3-20


Download ppt "Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition."

Similar presentations


Ads by Google