Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Space Man Sam: Grammar Mistakes By Aleis Murphy Duke University, Under the direction of Professor Susan Rodger July 2010.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Introduction to Computing Science and Programming I
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Wizard Game: Class-Level Variables in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
5.05 Apply Looping Structures
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Making a Timer in Alice.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Summer Computing Workshop. Session 2 Input in Scratch  Multi-Character input - This is used when the user is prompted to enter a number or word.  Problems.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Microsoft ® Office Outlook ® 2003 Training See and share multiple calendars CGI presents:
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Animating Objects in Groups: Using Arrays and Lists By Ruthie Tucker under the direction of Professor Susan Rodger Summer 2008.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
Making a Timer in Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University July
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Grade Book Database Presentation Jeanne Winstead CINS 137.
1 Quiz Template: Using the ‘ask user’ functions By Deborah Nelson Duke University Under the direction of Professor Susan Rodger July 2009.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
Ready, SET, go! By Melissa Dalis Professor Susan Rodger Duke University July 2011.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
A Simple Quiz for Alice 3.2:
IF statements.
Intro to CS Monday, August 29
Intro to CS Monday, August 24
Let's Race! Typing on the Home Row
A Simple Quiz for Alice 3.2:
Loops CIS 40 – Introduction to Programming in Python
Introduction to TouchDevelop
under the direction of Professor Susan Rodger
Presentation transcript:

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop

Session 4

Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs later in the program. There are many different types of variables in Alice, but we will only be covering three in detail: Numbers – A number variable can contain almost any value. (e.g. 1, 0.01, -5,+/- 10 million if not more) String – A string variable can contain letters, words, or sentences.(e.g. “s”, “string”, “This is still a string”) Boolean – A Boolean variable contains a true or false value.

Number Variable Overview – A Video Game Outlook Incrementing/Decrementing Number Variables – This is useful when decreasing or increasing variables by a specified number. (e.g. a player losing lives in a game or increasing a player’s score). Setting Number Variables – This tool is used to set the variable’s value to a specified number. (e.g. setting the score to zero after the game is restarted or setting the players lives to max after the game is restarted) Using Number Variables – Programmers use variables to store data for later use. Problems/Type differences – Similar to the issues related with input, different types of data may have unexpected outcomes.

World Variables and Object Variables Variables can either be world variables or object properties. World Variables - Use these variables when all objects will be sharing one variable. For example, in football, it would be inefficient for each player to keep track of the time remaining in the quarter using their own clock, it makes sense to have one “global” clock that everyone can see. Object Properties (Variables) – Use these variables when an object requires its own variable. A good example might

Incrementing/Decrementing Number Variables As discussed in session 3, it is possible to use a variable inside of a while loop in order to make it behave like a regular loop. The steps required to construct this also help us understand how to increment variables. Create a new project and get a loop ready to repeat twice. Now drag a camera “move” block and place it inside the loop and choose a direction and distance. Now drag a while loop and place it below the previous loop. It doesn’t matter if you select true or false, we will be changing that promptly. Go to the properties tab of the camera object and create a new variable named “index,” make sure it is a number variable and the value is 1.

Variables in the Object Details Area There are three things we can realize about a variable when looking at it in the object details area. By this icon we can observe that this variable is of the type “number”. String and Boolean variables have a similar representation. This represents the name of the variable. It is possible to change the name by right clicking it and then entering a new one. Here we can see the value the variable contains when the program starts. It is possible to change this value by clicking the arrow next to it.

Incrementing/Decrementing Number Variables Make sure the value of the variable is 0 when the program starts. Now drag a < operator from the world’s functions, place it into the conditional part of the while loop, mouse-over expressions, mouse-over camera.index, and select 2. Since this while loop will execute “while the index variable is less than 2” and the index variable never changes inside the loop, we need to increase it by one each time. This gives it the same effect as a regular loop. Drag the index variable and place it inside of the while loop where the “Do Nothing” is located. Now choose to increment that variable. Alice can only increment/decrement variables by 1 but it is possible to construct a snippet which will increment/decrement a variable by a value other than one.

Closer Look Look at the regular loop we have in our methods editor, there should be a button which you should click. This can help us better understand that both of these loops are identical. Loop/While Comparison – “index from 0” means the index starts at 0 (This number can be changed by clicking the arrow next to 0).“Up to but not including” translates to “while index is < number.” Incrementing by 1 is easy enough to understand. Drag a camera move block to the while loop and choose the opposite direction from the move block previously chosen. Choose the same distance you chose previously. Now let’s test our program. If you chose a large distance, it may take longer for the camera to move but it should move one way using the first loop and move the exact opposite way with the other loop. This is to show these two loops can be constructed to behave the same way. Now try to construct another loop using a while loop that is identical to the previous while loop except it decrements the index variable. (Hint: You may need to play with values in the conditional part of the while loop.

Setting Number Variables Let’s now save the world and create a new one. The next example will be very simple because it is so similar to an example done in session 1. Add an object and create a variable for that object named “distance.” Choose 0 for the initial value. Drag the variable to the methods editor, mouse-over “set value” and choose 1 for now. Drag a “ask user for a number” block and place it in the box after set value to, choose other and type “Enter a distance for the object to move.” When the program starts, the “distance” variable will be set to whatever the user’s answer is. Now drag a move block from the object’s methods tab, choose a direction and distance to move. Now replace the distance you just chose with the “distance” variable. Let’s test our program and see what happens.

String Variables Words can also be used and stored into variables. Examples could be asking someone's name, or what their favorite color is, or if they would like to continue with the program. The example we will construct will modify the previous program to also ask the user if they would like the object to continue moving, if so, to enter the word “continue.” This will require us to create a new variable named “answer.” Even though a world variable isn’t required here, let’s go ahead and make this variable a world variable. When it asks for a value, select other and type “continue.” Drag a while loop under the current snippets already present, select true. Now drag the “answer” variable we just created and place it in the conditional part of the while loop, mouse-over ==, and select “continue”. If “continue” isn’t present, choose other to input it yourself. Now drag the object “move” block to the methods editor and place it inside the while loop. Drag the “answer” variable from the properties tab of the world object and place it under the move block but still inside the while loop and set the value (doesn’t matter what the value is for now). Drag an “ask user for String” block and place it in the box after the words “set value to” in the set answer block (world.answer), type your own question, it could be something like “Enter continue for the object to move again.”

String Variable Test When the program reaches the while loop, it first checks if the “answer” variable is equal to “continue.” This is why we set the initial value of the answer variable as we did. Once in the while loop, the object moves, then the answer variable is set to the user’s response to your question. Again, the conditional in the while loop checks if the “answer” variable is equal to “continue” and acts as it did before.

Problems that could Arise In the program we have created, when the user is asked a distance to move, try entering a word or letter instead of a number. Test this and see what happens. The cause? Alice is type sensitive meaning it has different types which are incompatible with each other. Usually Alice will not allow us to mix types; for example, if we were to create a number variable, we couldn’t assign a string value to that variable. However, if the user enters a number value when asked for a string, Alice can’t really stop them from doing it but it will be stored as a string instead of a number. In Java or C++, this could cause an error that could easily be fixed, but the designers of Alice accounted for the error so it most likely will continue running. However, you may have unexpected results. The good thing about entering a number instead of a word is Alice will actually print the numeric value as a word. This could be quite useful.

Boolean Variables Boolean variables can be placed in the conditional section of a while loop and in the predicate of an if/else structure. Right click the while loop and click disable. Drag another while loop and place it above the disabled one. Create a world variable named “boolean” of type Boolean and set the variable directly above the enabled while loop and choose true. Drag a “ask user for yes or no” block and place it into the “set” block; for the question type “Would you like the object to move again”. Now drag the Boolean variable and place it in the conditional part of the enabled while loop. If the value of this variable is true, the while loop will repeat forever, if it is false, it will never enter the while loop. Now drag the move block from the disabled while loop and place it in the enabled while loop. What do you think will happen when the program starts? If the user answers yes, the object will move forever, if the answer is no, it won’t move at all. This is exactly how a Boolean variable works in programming as well.

Project 4 Create a Alice script that averages multiple numbers. You can either ask the user to enter how many numbers they would like to average and use a loop, or use a while loop and wait for the user to enter “continue.”(There are many other ways to do this, feel free to be creative, this is only two ways of giving the user a way to stop the program.) Create an Alice program that asks the user to enter 2 numbers, stores them as variables A and B, then switches the values in the variables.