CSci 170
Smalltalk Lab

You may want to print out this page since you'll have at least two windows open on your desktop.

This is just a small lab to get you better acquainted with Smalltalk. You'll be following through a couple of the sections of the GNU Smalltalk Tutorial. This tutorial is something like a text to highlight important aspects of Smalltalk for people who know something about programming already.

Go to the on-line GNU Smalltalk User's Guide at http://www.gnu.org/software/smalltalk/gst-manual/gst.html, and bookmark that page. You won't need the whole thing today. Go to the link 5 Tutorial at http://www.gnu.org/software/smalltalk/gst-manual/gst_30.html.

To start Smalltalk, get a command line prompt and enter after the prompt

You should get the prompt which is the Smalltalk prompt. The -q isn't necessary, but it supresses printing statistics about the computation. Alternatively, you can invoke Smalltalk under the K menu (lower left corner of screen), under Development > Other Development Tools > GNU Smalltalk (but that doesn't include the -q which supresses statistics).

As described in the tutorial, you can give the command

(the symbol after N is a lowercase L, so printNl means print a new line), to get the output Use printNl to print anything. The ! is sometimes read 'bang' and terminates a Smalltalk instruction. The tutorial explains how to print out evaluated expressions, like which should print 16.

Try this

As discussed in class, Smalltalk treats all the binary mathematical operators with the same precedence, and they're evaluated from left to right (i.e., left associative).

Arrays in Smalltalk

Following along the tutorial, you'll see how first creates a variable x, assigns it an array of length 20, assigns 99 to x[1], and prints out that value. What do you get if you ask to print x itself?

Sets

The classes Set and Dictionary are built in to Smalltalk. Create a new set x as described in the tutorial, and add a few things to it, numbers like 5, 6, 7, and strings like 'red', 'five', and 'toad'. Print it out. Create a new variable y, and make it a new set. Add a few things to y, like 8, 'word', and x. Print out y. What happens if you add the same thing to a set twice?

Note that you may find that sometimes you get a claim that there's a parse error. That's likely because you forgot a bang (!), or there was some kind of syntax error. Perhaps all you have to do is enter a bang on a separate line to clear that.

Now that you've got x as a member of y, you could add y to a member of x. It works. However, don't try to print out either if you do that!

As mentioned in the tutorial, remove is the opposite of add. Check out that it works. See what happens if you try to remove an element of a set that's not there.

Dictionaries is Smalltalk

An array in Smalltalk is indexed by integers; a set is unordered; but a dictionary is indexed by anything—an integer, a string, an object of any kind. Check to see how the example in the tutorial works. It's worthwhile actually entering the instructions and seeing the results; it fixes the processes and concepts in your mind better than just reading the material.

The next section describes the Smalltalk dictionary itself. It's where the variables that you interact with the Smalltalk interpreter reside.

What else?

You might want to go through the next few sections on your own sometime. Section 5.3 in the tutorial reviews a little of what you already know about object oriented programming. The remaining sections get much further into programming. Sections 5.4 and 5.5 describes the syntax for creating new classes and specifying their methods, something that's rather important in programming. Sections 5.6 and 5.7 detail programming methods, with program flow, etc. A course in programming in Smalltalk would expand on 5.4 through 5.7. The remaining sections discuss more advanced topics.

Programming

There's no programming assignment for Smalltalk, but you may want to try your hand at it anyway. I suggest writing an elementary class with only a couple of instance variables and a few methods, just to get a good hold of the syntax. Perhaps something like an address program where you write the class Address with instance variables being 'name', 'address', and 'phone', and methods that include at least new, init, printOn:, and some access methods to get or set the name, address, and phone.

If you've succeeded in that, you may want to use that class more seriously in another class, AddressBook, which would hold a list of Addresses. You'd want a lookup method, also, create, delete, modify methods. And you'll have to decide what kind of data structure to use for AddressBook. Should it be a dictionary, or what. Keep in mind that the most frequently used method in an AddressBook would be lookup.

This file is located at

http://aleph0.clarku.edu/~djoyce/cs170/SmalltalkLab.html