Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop Session 7 Pre-Project Design an Alice program that not only determines the mean of a collection of numbers supplied by the user stored in an array, but also finds the median and range. What is a List? A list is very similar to an array. It contains multiple variables of the same type. Arrays have a definite size which must be set before the program starts. Lists are different and allow the programmer to add elements to the list dynamically. This means a list can grow in size while the program is running. Say we wanted to ask the user for as many numbers as they want to enter. With an array, eventually we would run out of space. However, a list grows as the user adds data. This gives us a little more flexibility when programming. Lists Go ahead and make a list by creating a new variable. Change “array” to “list” and check the box to the left of list (the same way we did to create an array). Now that we have a list created, drag it to your method to display the list’s methods. The below menu will be displayed. Keep the menu up so we can follow along: Insert Methods for Lists This is an “insert at beginning of list” method which can add items to the beginning of the list. The first box represents the number to be inserted into the list and the second box shows which list will be affected. The “insert at end of list” method is similar to the previous method except it inserts an item at the end of the list. The boxes are exactly the same. This is an “insert at position” method which allows the list to act similar to an array. Like arrays, lists in Alice index their elements. This block allows us to insert something anywhere we want in the list depending on the index value. The first box represents the number being inserted, the second represents the position (index) at which we want to insert the item, the 3rd box is the same as before. Remove Methods for Lists This is a “remove item from beginning of list” which removes the first item from the list; the second item then becomes the first. This is a “remove item from end of list” method. It does exactly what it says. This is a “remove item from position” method which, for example, removes item from index 2. Item 3 becomes item 2, item 4 becomes item 3, etc. This is a “remove all items from list” method which allows the user to clear the list. Dynamic Size of Lists The following example will show the dynamic capability of a list’s size. We will use a loop to take input from the user. Drag a while loop to your method, place an “ask user for yes or no” function in the conditional part of the while loop. Now enter something such as “Would you like to add a number to the list?” Dynamic Size of Lists We want to insert an item at the beginning of the list. To do this, drag the list inside the while loop and choose the appropriate method. Place an “ask user for a number” function inside the number part of the previously placed method. The default question will work fine. This loop will allow the programmer to prompt the user to enter as much input as desired and store the input into a list. Step Through a List Now let’s use a loop to step through the list. To test this we will make the camera say each number. Drag a loop under the while loop and select anything for now. Drag the list into the number area of the loop to replace what was previously chosen. This will bring up the list’s functions. Select the “size of list” function (refer to slide notes for more details). Step Through a List cont. Now drag a camera “say” method into the loop. Place a “what as a string” function (world object) as the string parameter in the say method. Select anything for now. Drag the list and place it into the function “what as a string.” From this menu, mouse-over the “ith item from list” function, mouse-over expressions and choose index. Let’s test! For All In Order Loop Alice has conveniently created a loop specifically designed to step through a list. The “for all in order” loop does exactly that. Delete the current for loop and everything it contains. Drag a “for all in order loop” and place it where your previous loop was located. Mouse-over expressions and choose your list. For All In Order Loop Place a “camera say” method inside our “for all in order loop,” mouse-over expressions and select “item_from_list.” Test your program. Notice it steps through the list exactly as the previous loop did. Either loops will work in a situation like this. For All Together Loop A “for all together” loop is similar to a “do together” container. Like the “for all in order” loop, a list is needed. If we use this loop with our previous example, it would print all the words in the list to the screen at the same time. This seems useless when used like this but if we wanted multiple object to do something at the same time, we could put them into a list and use this loop. To better understand this, let’s do a small example. Create a new project and add 5 bird objects. We plan to make all 5 birds fly away at the same time. For All Together Loop We could use a “do together” container and place the “fly away” methods of the bird objects inside it but using a “for all together” loop may be easier. Create a new list of type object and name it “birds.” This list will hold our birds we previously created. Create new items for the list and assign the birds previously created to the items of the list (remember, the list must be of type object for us to hold objects in it). For All Together Loop Now place a “for all together” loop in your program, mouse-over expressions and choose your list. Drag one of the bird objects’ “fly away” method and place it in the “for all together” loop (not the fly method, the fly away method), mouse-over expressions, mouse-over item_from_birds, and select 0.25. (the number is a parameter for this method signifying how fast to make the bird fly away) For All Together Loop Let’s test this project. Notice all birds fly away at the same time. Discussed earlier was a way of doing the same thing with a “do together” container. Think about how this might be accomplished. Project 7 Design an Alice program that not only determines the mean of a collection of numbers supplied by the user, but also finds the median and range. This time, use lists instead of arrays. If you have completed the project, feel free to have the program find the mode as well. Day 4 Questions What three types of Boolean logical functions do we cover? Boolean logic deals with true or false values. ( true/__false) Where are Boolean expressions most commonly used? Can two blocks run at the same time? If so, how could this be accomplished? What is the difference between an array and a list in Alice? If you wanted to store 20 numbers supplied by the user, should an array or list be used? Is it possible to rearrange(sort) items in a list? In the following array, find and write down the value with an index of 3. [2, 41, 16, 7, 10] Answer Key What three types of Boolean logical functions do we cover? And, or, not Boolean logic deals with true or false values.( X true/__false) Where are Boolean expressions most commonly used? conditional branches/ while loops Can two blocks run at the same time? If so, how could this be accomplished? Yes, through the use of a “do together” block What is the difference between an array and a list in Alice? Array cannot change size and is faster, a list is dynamic and is slightly slower If you wanted to store 20 numbers supplied by the user, should an array or list be used? since the size is known ahead of time an array should be used Is it possible to rearrange(sort) items in a list? Yes In the following array, find and write down the value with an index of 3. [2, 41, 16, 7, 10] 7