Smalltalk Programming
Tutorial. Classes. Class Hierarchy. Sending messages to ByteString
class (i.e. Strings).
Tutorial 3. Strings |
|
Home
Tutorials | 1. Starting Smalltalk, and Terminology | 2. Precedence Rules | 3. Classes - Strings | 4. Class Browser | 5. Variables | 6. Error Messages | 12. Control Structures | 16. Dialogs | 23. Collection class | 39. OpenGUI | Information | Smalltalk Books | Student Software & Books | Hints & Tips | Sites | Download Smalltalk | Coloured code | Download Documents | ASCII Codes | Smalltalk Index |
|
Contents
|
InstructionsIn order to follow this tutorial Click on the following link, which will open a separate window, print out the single page, then close the page. Collection Class Hierarchy. This page has a diagram of of the Collection Class Hierarchy that you should have available to help in understanding this tutorial.
The OU ( Open University ) M206 Computing an Object-oriented approach course, talks of attributes in the early part of the course. Later the correct term instance variables is used. I think that students who have programmed in other languages will grasp Smalltalk terminology better thinking of instance variables rather than attributes. Methods are also very similar to functions used in other languages. Classes
Class Inheritance HierarchyA diagram of the class hierarchy looks like a tree root structure, and it represents the subclass and superclass relationship between classes of the hierarchy . The diagram below, on the left represents a very small section of this hierarchy.
every subclass should be a specific kind of its superclass. This is reflected in the inheritance hierarchy by classes higher in the hierarchy representing more general (common) characteristics, while classes lower in the hierarchy representing more specific characteristics, that is, superclasses specify general characteristics which their subclasses inherit and refine to provide specific characteristics. Subclass
SuperclassIf B is the subclass of A, then A is the superclass of B.
StringsStrings were mentioned in tutorial 2.Strings are
Question 1 What distinguishes a string from a comment? Go to Answer 1
Most books on programming show you how to create a program that will put the words Hello World onto the screen. In Smalltalk obtaining 'Hello World' as an answer to evaluating a message expression is easy. All you have to do is evaluate 'Hello World', how, see below. Exercise. 1a
String class
|
Workspace CommentsIn tutorials 1 and 2 you learnt how comments are used in methods.Comments can also be used in the workspace. You will probably devise a system of your own of how and when you will use comments in the workspace. These comments can help you when returning to a Lb. after a break, or to check something out. Use Workspace comments in the following exercises. Ideas for comments.
InspectingInspecting is an important part of developing an expression, it allows the programmer to make checks on what has actually happened. This is more useful when an unexpected result occurs.Inspecting objects provides additional information about that object such as its state. From now on carry out inspections of objects. In order to understanding the inspecting process better, and the information obtained from inspecting objects. Do lots of them.
There are 2 way of inspecting.
Exercise 1b. Inspecting 'Hello World' using the Inspect button.
Before you evaluate 'Hello World' count and remember the number of characters in the string. Now evaluate 'Hello World' Then click on the "Inspect last message answer" button. I will explain the Inspector window after you try the alternative way of bringing it up Exercise 1c. Inspecting 'Hello World' using the inspect message.
Evaluate 'Hello World' inspect
Inspector window
Note the following Inspector Windows vary slightly depending on the class they are depicting. A description for the classes ByteString and Frog follow:
Exercise 1d. Inspecting the Inspector Window. More information can be obtained from this window. Before you do this exercise note that classes are arranged in a hierarchical system similar to that of windows and its folders. The top level is the Object class, this is similar to the C; root folder (directory). At this stage you are only going to get a "feel" of this hierarchical system. The system looks like the roots of a tree, with the Object class being the trunk. What you should remember is that a lot of information can be obtained from the Inspector. Usually the first window contains the information required. But as you will see more information is there and can be obtained by double clicking on various sections of the window.
Double click on ByteString in the Class pane. (Rerun Exercise 1b if you have closed the inspector window.) Do not delete the new inspector window, Inspector on that appears, because further exercises will be done with it. Look at the following in this window.
|
Hierarchy - Exercises.Lets look at what has been mentioned so far.
In the next Exercise you are going to replace the X's with the actual class.
Object
X
X
X
X
X
X
ByteEncodedString
ByteString
The following exercise will allow you to get an insight into the hierarchy of classes. Because in this exercise you are navigating from the 'root tip' towards the 'tree trunk' the navigation is easy, there is only 1 superclass for any one subclass. Travelling in the other direction, the navigation can be harder, because a superclass may have several subclasses, each of these multiple subclasses gives you an alternative route though the network of 'roots' which end in many 'root tips'. For such a journey either a map of the system is required, or you need specific directions. I have provided a map for the area we are working in. You should have printed it out if you followed the instructions at the start of this tutorial. If you have not printed the Collection Class Hierarchy, click here, print then close the window. Please note that ByteEncodedString class does not appear on this "map", because ByteEncodedString is not visible in the class browser, the source of the map. Exercise 1d. Hierarchy- introduction.
Go to Solution to ex 1d
Sending messages to ByteStringIn the table below are listed the message selector's that you are going to use.
Question 2. Which Message selector should not be in the table. Go to answer After the next exercise, you will learn why you need to know the class(es) a message selector can work with. Go to Answer 2
Question 3. From the table above: Which message selectors are keyword messages, and which are binary? Go to Answer 3
The information shown in the above table was obtained by using the class browser, how to do this is explained in Tutorial 16. This is the type of information required, in order to select a message to use, therefore it is provided above for use in the following exercise.
Exercise. 2 In the Workspace experiment by sending messages from the table above, to the receivers listed in the table below. Your aim is to make the required answer, appear in the answer pane. When you have done this, fill in the Enter message expression column with the required message expression in the table below
Question 4 Because '78.9' asNumber answered with 78.9 You may have thought that '78.9' asInteger would have answered with 78. Why does '78.9' asInteger produce an exception notifier. Go to Answer 4
You may have thought that 'run' at: 2 put: $a would have answered with 'ran' The comment "Store the Character in the field of the receiver indicated by the index. Fail if the index is not an Integer or is out of bounds, or if the argument is not a Character, or if the code of aCharacter does not fit in one byte." Does tend to suggest this, but what is missing from he comment is what the method answers with. In the next tutorial, tutorial 4, we will have a look at this method. Note this Tutorial should be available shortly | Previous Tutorial 2. | Next Tutorial 12. Decisions and Loops | Top of Page | |
AnswersAnswer 1. In Smalltalk a string starts and ends with a single quote. E.g. ' Hello World', and a comment is placed in double quotes " This is a comment " Return to Question 1.
Answer 2. The message selector asInteger should not be in the table. The method asInteger is found in the class Character, which is in the Magnitude class hierarchy. Return to Question 2
Answer 3. HELLO WORLD at: and at:put: are keyword message selectors. , (Comma) is a binary message selector. All the rest are unary message selectors. Return to Question 3
Answer 4. Although asInteger is a method in the classes Number, Integer and Character. Instances of ByteString do not have the message asInteger in their protocol. Therefore a semantic error occurred, resulting in the Exception notifier. Return to Question 4
|
Solutions to ExercisesSolution to Ex 1d. Object
Collection
SequenceableCollection
ArrayedCollection
CharacterArray
String
ByteEncodedString
ByteString
Continue with next section
Solution 2a.
Continue
| Previous Tutorial 2. | Next Tutorial 12. Decisions and Loops | Top of Page | |
Sites by John McGuinn . HTML and Web Design | C Programming
Leeds & the UK Tourist Information & guide my home town
Relax in the sun.
Benidorm Tenerife San Marino apartments to rent
Holidays and Short Breaks Manchester Airport
Leeds my home town City of Leeds information Leeds and Bradford International Airport
Leeds Web Design Web
Designers Hosting Domain
names Web site Promotion
Copyright © John McGuinn 2000 - 04