Alice+Tut3+Variables


 * ~ home ..... Getting Started ..... ||~ Tutorials and Commands ..... ||~ resources ||

Just like the weather, liable to change... for today's forecast tutorial, we will be looking at variables.

1. Open Alice from the Applications folder, and cancel the welcome page to start in a default world.

Use the green **ADD OBJECTS** button to bring in an object of your choice into your world.




 * 1) After you have closed the gallery by clicking the green and red DONE button, you can display the methods of your object by clicking the object you just added in the world frame found on the upper left portion of the screen.



4. Variables are used in a programming language to assign values. Click on the properties tab, beside methods, to display the existing variables.

5. Click on **create new variable** to define a variable to store the age of your object. Let us name the variable age, make it a number type, and assign its initial value to 0.

6. Once you have pressed OK, The variable will appear in your properties area. The “123” beside it indicates it is a number. Also notice the name is Age and initial value is 0.



7. We will create another variable to store the year your object was born,called BornOn, with an initial value of 0.

8. We will create one more variable, which will be our objects height, called HeightInCm, with an initial value of 0.

9. Let's add 2 more variables, YearlyIncome, and NetWorth, both numbers, and with an initial value of zero. These can be used to calculate your object's net worth by multiplying its yearly income by its age.



10. To input a different value for your property, drag the **age** variable to **my first method** and then use the “**ask user**” functionality to query for a new value. To do this, first select the age variable and drag it over to the “Do Nothing” area world methods area. Notice the variable (along with its object name is now shown in the first line of the method.



11.To be able to ask the user for input we can use an existing function available to all worlds. (Functions are similar to methods in Alice except that they return a variable). To see the functions available for the world, **click on the world object** in the upper left corner and then **click on the functions tab** in the lower left corner. Use the scroll bar to display the “**ask user**” related functions.



12.Since we want to request a number (age), let's select "ask user for a number" and drag it over right past the “set value to” area. When dragging objects you will notice **acceptable drop locations are flagged in green**. Once you drop it into place, you will be asked for the text to be displayed. Enter “What is your age?”.





14. TESTING TIME!!!!! To do this click on the **Play** button. You should see your object asking you for your age. After you enter the value the little box goes away.



15. Now we are going to do the same for Yearly Income, but make sure you remember to click back on your object to display the variables of the object you chose. Drag the yearly income variable right below the age variable, and use the ask user function in the world object to ask "What is your yearly income?"

16. Check your work to see if your object asked both questions.

17. Next, we need to be able to compute the net worth value by multiplying the age by the yearly income. ( this assumes, your object didn't spend any money, got paid every year... for whatever it does for a living).

the calculation for net worth is: Networth = Age * YearlyIncome

18. We are going to add Networth to our list of variables, just make sure you have your object selected, to find it in properties! Once it has been dragged over, click on the drop down arrow next to the “NetWorth set value to 0” statement to reveal additional options.



19. Notice the expressions option has the hare variables listed. Select the [object name].Age



20. Clicking on the [object].age drop down button reveals a math option where you can select multiplication, and then additional options to select [objects name].YearlyIncome. The math expression was visually built by using only drop down options.

21. To test out the equation, you will need to use the print function. At the bottom of the world.my first method you see several methods available. Locate the **print** method and drag it below the [objects name].NetWorth line. When you drag the print method in you will need to select what you are printing. In this case, select object-> expressions-> [object name].Networth.

22. Let's do a test and see what happens! if you get the correct answer underneath your world, and it's the correct answer, you did it!