        |
Variables
As in algebra, and life, variables are things that may vary.
In programming, you use variables to keep track of intermediate
computation, or to represent objects that you are modeling and how
their attributes change, or, well, to keep track of anything.
A variable can take on different types of values. These are
called data types. Common data types include integers, real
numbers, strings (character sequences), Boolean (a value of "true" or
"false"), and in Squeak, variables can have a color as a
value, a sound, as well as several other types of values.
You will notice that whenever you create an object in squeak, either
by painting it, or dragging it from the supply bin, it automatically
comes with a whole bunch of variables that describe it.
- Drag an ellipse from the supply bin.
- Ppen up a viewer for it by first cmd-click (mack) or alt-click on the ellipse until the halo of icons appears (you should have taken the handles tutorial and know how to do this). Click on the light blue eyeball icon on the left side of the halo - this is the viewer for the object, and you will be using it a lot.
- The viewer may have several viewing panes. Each pane can hold different kind of information, depending on what category of information you've chosen. The default is the "basic" category. It should look something like this:
- If you do not see this, click on the category title (in the picture above, that is what says "basic") and pulldown the menu until you see "basic".
Notice in the "basic" pane things such as "Ellipse's x", which is a
variable telling the x-coordinate of the Ellipse. Notice how this
changes as you move the ellipse. Notice also that you can move the
ellipse by clicking on the up or down arrows next to the value, or by
clicking on the value and typing a new value in its place (and hitting
the return key). Notice also the little arrow that points from the
value to the variable name "Ellipse's x", as in:
Ellipse's x -- 257
The arrow is one way to tell that this is a variable, and that it's
current value is 257.
Take the time to explore all of the various panes. You can tell which
the variables are by looking for the arrow. Notice that "Ellipse's
color" is a variable and it's value is actually a color, that
"Ellipse's isUnderMouse" is a variable that has a "Boolean" value -
either "true" or "false", depending on whether the mouse is over the
Ellipse. (Try it now and see how the value changes.)
Different types of objects come with different sets of predefined variables.
You should be aware of the differences, for example, between
objects that you drag out of supplies
(like ellipses, rectangles, other shapes), and
sketches that you draw. (For example, supply objects have a "color"
variable, but sketches don't.) Some objects have variables that are
specific to them. For example, a joystick has a "leftRight" and
"upDown". A holder, which is a container in which you can drop things
(go pull out of these in supplies or widgets) has a variable called
"cursor" which gives a number indicating which of the various objects
it contains is the "current" one. More about holders later.
Another comment: in object-oriented programming, such as in Squeak,
variables are "owned" by objects. It makes sense to create variables
associated with the object that depends on them. A car might have a
variable called speed, for example. It makes sense to create the
variable within the viewer for "car" - thus giving the car "ownership"
of that variable. We could open up a viewer for "world" (alt-click or
cmd-click on the main world window to open up a viewer for it), and
create car1speed, car2speed, etc., variables for the speeds of
different vehicles that we create. But this is awkward. Things are
more "modular" if each car object has its own variable "speed".
Actually, the advantage is that we can create one car, equip it with a
"speed" variable, and then create copies of it, which will all then
automatically get their own "speed" variables. It makes sense for
each object to own the variables that describe their various
attributes current state, as
it will similarly make sense for objects to have associated with them
scripts that describe their behaviors.
Without a context however, we will assume in this presentation
that our object is just an
"Ellipse".
Here are the things you need to know about variables:
How to create a variable.
Click on the "v" next to the name of the object at the top of viewer
pane. If your version of Squeak
does not have a "v", then click on the tiny menu icon next to the
object's name, and select "create instance variable", or somesuch.
Give the variable a name. There. That was easy.
How to change the data type.
After a variable is created, a pane should open up showing
"variables". If not, open one by selecting "variables" from the
pull-down menu. Next to the variable name is a menu icon. Select
"change value type" or "change data type" from that menu, and select
the desired type of variable.
How to display its value.
In the same menu as above, you can select "simple watcher", or
"detailed watcher". (Though "detailed" may not be available depending
on the variable data type). Pull out one of each, drop it in the
world, and see what happens. "Simple" just shows the value.
"Detailed" also shows the name. You can embed a simple watcher into
other objects, and when the variable changes, so will the watcher.
Notice also that the same menu lets you adjust the number of
decimal places shown, which you may want to do before you get a
watcher. (Although you can adjust the decimal places of the watcher
later, and separately.)
How to assign it a value:
- manually.
Depending on the type of variable, this is done in different ways.
If the variable has type "number", then you can click on its value so
that it is highlighted, then type the new numeric value, making sure
to hit "return" or "enter". Or, you can click on the up or down
arrows to increment or decrement the value. If the variable has type
"color", you click in the color currently displayed and can then
select a new color. You can also use the eyedropper that appears to
pick a new color from an object on your screen. If the variable has
type "player", you click on the "dot" (which is the initial default
value given to a variable of type player when it is created), and then
click on the object that you want the variable to be assigned to. The
variable then becomes synonymous with the name of the object...
for example, if variable "myobject" is assigned to a sketch you
created called "mycar", then executing the command
"myobject forward by 5" will move the car forward by 5.
- under program control.
Within a script, you can assign or reassign a variable. To get tiles
that correspond to such an assignment statement, drag the green arrow
near the variable into the script. You can then change the value
assigned inside of the script... which will not change the value of
the variable until the script is executed.
How to use the value in another assignment statement.
Sometimes you want to use the value of a variable
in a computation, not reassign it. For example, in our conversion of
celsius to fahrenheit, we used a variable "celsius" in a script
that adjusted the fahrenheit variable to become 9/5 fahrenheit + 32.
Within a script, assume you have already dragged an assignment
statement (by the green arrow) for a variable "fahrenheit" that you
want to change.
It looks something like:
Ellipse's fahrenheit -- 23
The value to the right of the arrow (23) is whatever the value
of the variable was when you dragged the assignment statement into the
script. You can now change the value by replacing it
with other tiles, in this case, you would simply grab "celsius" (not
by the green arrow!), and drop it over the number on the right side of
the assignment statement for fahrenheit within the script. (NOTE:
this explanation should really be done with pictures.)
Ellipse's fahrenheit -- Ellipse's celsius
Notice the arrow to the right of "celsius". Clicking on that arrow
expands the tile to allow for compound arithmetic expressions.
Because smalltalk has a funny order of arithmetic (right to left),
while it would
seem we'd like to create a tile that reads:
Ellipse's fahrenheit -- Ellipse's celsius 9/5 + 32
this will result in the wrong value.
Instead, you can achieve the desired result in two steps:
Ellipse's fahrenheit -- Ellipse's celsius 9/5
Ellipse's fahrenheit increment by 32
(Go play and figure out where the "increment by" came from, and
determine the other things you can do to a variable.)
How to test the value.
Often we would like some statements within a script to execute
only under certain conditions. (More on this in the notes on
Control) If you tear off a "test" from
the icon next to the (X) in the upper right of a script window, you
will get a template that says
TEST
YES
NO
If you want to test, say, that a varible has a certain value, simply
drag a tile with the variable name (NOT THE ARROW) to the right of
"TEST". For example, suppose you wanted to test that whether a
variable called "score"
was greater than 10. When you drop the "score" tile next to "TEST",
The variable name tile will expand to something like "score 5".
You can then change "<" to ">", and 5 to 10.
|