Duke University Department of Computer Science
Download the Alice World for this example.
In this example, we see how variables are used to store data for future use. The string type is used because types must be converted to a string in order to be "said" by someone, so using only strings simplified the example.
When you play the world, Alice (the character) will ask you for some information. The information is then stored in variables, and Alice repeats that information at the very end, demonstrating that she "remembered it all." Okay, that's not quite the whole story. When you open the world, you'll get a message box where you have to type something and press enter. Keep doing this until there are no more dialog boxes, then hit play. (Hint: This is a bug in Alcie).
This example has only one method, which features four variables to store the information. The method is called when the world starts, and performs all the actions that you see when you hit play:
| world.my first method ( )
userName = default string , userBirthDay = default string , userHometown = default string , userSchool = default string
|Do in order|
|aliceLiddell say Hello. My name is Alice! duration = 1.5 seconds|
|aliceLiddell say I'd like to ask you a few questions. duration = 1.7 seconds|
|userName set value to ( ask user for a string question = What's your name? )|
|userBirthDay set value to ( ask user for a string question = When's your birthday? )|
|userHometown set value to ( ask user for a string question = Where's your hometown? )|
|userSchool set value to ( ask user for a string question = What school do you attend? )|
|aliceLiddell say Let's see if I have this right:|
|aliceLiddell say ( Your name is joined with ( userName joined with ( , your birthday is joined with ( userBirthDay joined with ( , you come from joined with ( userHometown joined with ( , and you are enrolled at joined with ( userSchool joined with . ) ) ) ) ) ) ) ) duration = 10 seconds|
This code begins by having Alice give her introductory comments, and immediately begins the questioning process with the Alice built-in function "ask user for a string." As each prompt is displayed, the answer is recorded in a variable. Then, the final line of the method tells Alice to repeat all of this informating and concatenating several strings and variables.
Professor Rodger's Homepage
Main Tutorial Website
Alice Materials at Duke