Goals: This lab will show you how to get started in Smalltalk. As we learned during Scott's talk, Smalltalk is run on a virtual machine. At Grinnell, we use a commerical version of the Smalltalk virutual machine called VisualWorks.
We will first introduce VisualWorks and then you people will get to try writing small programs -- "Hello, world" and a factorial program.
% visualworks /usr/local/visual/image/visual.im
Two small windows will pop up: the main window and a workspace.
The main window and the workspace window are shown below:
2. Writing some very simple expressions.
We will not be looking at all of the menu and button options in the main window, but right now you can use the workspace and practice writing some simple Smalltalk expressions.
3 + 4. To execute the expression, highlight it and then middle-mouse click. Select "print it" from the pop-up menu. Woo!
Transcript show: 'Hello!'. Select "do it" from the menu this time to execute.
Transcript show: 'Smalltalk is fun!'.
To open a System Browser, select Browse->All Classes from the main window or use the shortcut button . Notice that a System Browser is divided into four columns across the top half of the window, and the bottom half contains a text area.
The first column is the Categories column; the classes are first organized by categories, to make it easier to manage them (and there are hundreds of them). These categories are completely arbitrary, so you can reorganize the classes any way you want. After you select a category, a list of the classes in that category will appear in the the second column, the Classes column. When you select a class, the members of the Protocols column appear; these are just groups of methods. Select a protocol and finally, the methods of the class will show in the last column, Methods.
Have fun exploring VisualWorks on your own, but the help system doesn't seem
to work. Notice also, that when you quit VM it will prompt you
to "save" something -- type in
when you start VW again you just have to type
me.im and all of your work from the last session has been saved!
In this section, you will learn how to make a new class using the System Browser.
Object subclass: #HelloWorld instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Test'
print ^'Hello, World'
Hi := HelloWorld new. /* middle-click mouse and select "inspect" */ Hi print. /* middle-click and select "print it" */After you inspect the first part, VisualWorks will prompt you to declare
Hias a global or temporary variable. Choose global, so that when you run the second statement, you will not have to highlight the first statement.
Magnitude-Numbersin the first column. You should then see the subclasses ArithmeticValue, Double, FixedPoint, etc. in the second column. Now, click once on
Integer. Then, in the third column click
factorization and division. You will see several methods there already: an iterative factorial, gcd, icm, etc.
You are now ready to begin writing your first factorial program in Smalltalk. Go to the coding area in the lower half of the window and write:
recFact self < 0 ifTrue : [^0] ifFalse: [ self = 0 ifTrue : [^1] ifFalse: [^self * (self -1) recFact] ]