|
Web Log Stats Lesson 2
|
|
|
When we start writing and testing Smalltalk code, we will need a way to
verify that VisualWorks is behaving the way we expect it to. We need to
"see what we're doing". Besides developing a GUI or writing data out to
an external file, this lesson provides a quicker and more convenient way
to display output.
|
|||||
|
This lesson explains 4 basic ways of providing feedback in VisualWorks.
|
|||||
|
The four basic ways of how VisualWorks provides feedback (displays output) are:
Figure 2-1. The VisualWorks Workspace |
|||||
|
1.
In the Workspace, enter the following:
4 squared
2.
Highlight this text, <Operate-Click> and select Print It.
Note that 16 will appear just to the right. For a mathematics refresher, a number squared is a number times itself, in this case 4 times 4.
Figure 2-2. The Print It option within the VisualWorks Workspace 3. Highlight the text again, <Operate-Click> and select Inspect it. A new window will appear (called appropriately enough the Inspector window). 4. In the left pane of the Inspector window, click (highlight) the word self. Note that in the right pane you will see the value of self which is 16. If you are curious as to the hexadecimal, octal or binary values of 16, simply click on those terms in the left pane. The term self will always default to the decimal value for numbers.
Figure 2-3. The VisualWorks Inspector Window 5. Close the Inspector window. 6. No introduction to a new language would be complete without writing the infamous "Hello World" program. Here is how to do that using the System Transcript. Enter the following in the Workspace window:
Transcript show: 'Hello World'
7.
Highlight the text again, <Operate-Click> and select Do It.
Note that the words Hello World have now appeared in the display area of the main VisualWorks Launcher window. As discussed earlier in Lesson 1, this area is called the System Transcript (or just simply the Transcript) and is used primarily for debugging by most Smalltalk programmers.
Figure 2-4. Hello World in the VisualWorks System Transcript 8. Now let's write the infamous Hello World program using a simple dialog box. Enter the following in the Workspace window:
Dialog warn: 'Hello World'
9.
Highlight the text again, <Operate-Click> and select Do It.
Note that a dialog box appears with the words Hello World in the middle.
Figure 2-5. Hello World in a simple dialog box 10. Click OK. |
|||||
|
Smalltalk BasicsIn the last two examples, you actually wrote and executed Smalltalk code. On the surface, the "statements" probably looked like normal programming commands that you might have seen in other languages, such as a COBOL Display statement or a Visual Basic Print command. But in Smalltalk, that's not the case.The difference bewteen Smalltalk and other languages is that Smalltalk is classified as a "pure object-oriented" language. What this means is that the only way to get something to happen within Smalltalk is by sending messages to objects. That last sentence cannot be emphasized enough - the only way to get something to happen within Smalltalk is by sending messages to objects. For example, in the expression 4 squared, the term squared is not a "command" or "function" in the Smalltalk language per se. It would be explained this way: The number 4 is a particular instance of the SmallInteger class. The SmallInteger class, which is a subset of the class Integer (by the way, integers are whole numbers), knows how to do certain things with whole numbers and squared just happens to be one of them. So the 4 object (instance of the SmallInteger class) was sent the message squared. Smalltalk then said (to itself), "I now have enough information to make something happen. The message squared is being sent to the object 4 and I will execute that message (which in this case is to multiply 4 times 4 and return 16).If that sounded a bit confusing, it's probably because you never had it explained in object-oriented terms before. It is highly recommended that you proceed to the primer on object-oriented principles. This will provide a further explanation of classes, objects and messages. From this point on, the tutorial will start to use object-oriented (OO) terms in explaining the code samples, so a basic understanding of OO principles is required for those explanations to make any sense. |
|||||
|
Proceed to the object-oriented principles primer
|
|||||
|
SummaryYou should now be able to:
|