|
Smalltalk tutorial. Terminology. Simple message expressions. Using LearningWorks. Smalltalk Programming. Tutorial 1. Starting Smalltalk and Terminology |
|
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 | Feedback | Download Documents | ASCII Codes | M206 & Smalltalk Index |
|
Tutorial 1. Aims of the lesson
The lessonTerminology. The OU M206 course is full of terminology. The quicker you learn this terminology the easier the course will be. This exercise introduces the following main terms:- answers, argument, expression, method, method heading, method body, message, message expression, object, receiver. Try to understand this terminology as you read the lesson, and the the glossary at the foot of this page, Golden Rule 1. A very simple rule that
Example. This could be likened to a drill instructor getting a squad of soldiers to do something by issuing commands. In the following example many of the terms are given. At this stage try to obtain an overall picture of the use of these terms. |
|
Example of objects sending and receiving a message. An army drill instructor commands his B squadron to right turn by shouting "B Squad right turn". Similar other commands are left turn and about turn. In Smalltalk this command could be written as bSquad turn: right. Don't worry if you do not understand the following sentence. Assuming that a method named turn: has been written and that bSquad is the name of an instance of class Squadron, that understands the message turn: right. bSquad is the receiver of the message turn: right. turn: right is the message . Made up of a keyword turn: and an argument right. Turn: is also a message selector. This means that there is a method named turn: and because turn:ends with a : it is a keyword. It also means that turn:must have an argument. Expressions within the method would deal with the different acceptable arguments, such as right, left and about. The method turn: would return an answer, this may be the default answer, the receiver i.e. bSquad, or 1 of the 3 arguments, i.e. directions that are acceptable to the method, or suitable error message to the user if an unacceptable argument is sent. right is 1 of 3 acceptable arguments for the keyword turn:. |
Practical 1You will require one of the Smalltalk programs listed in table 1, on your computer. M206 LearningWorks is the one provided by the Open University for students taking M206 Computing an Object-oriented approach. The others were available as a free download. See the Download free versions of Smalltalk page. Start your Smalltalk Program. Follow the instructions for your version of Smalltalk on how to start the program. You then need to find the window to use as per table 1 below. Table 1.
Entering a message expression.Depending on which of Smalltalk version you are using. Click on the correct window, as per table 1, to select it. If there is writing in the window scroll to the bottom and Click after the writing.Exercise T1-1. Enter the expression into the window by typing 2 + 3 Evaluate this expression.Follow the instructions in the table below, for your Smalltalkversion on how to evaluate expressions entered into the window. Table 2
The answer 5 should be displayed Where the answer is displayed will vary depending on the Version used. If using M206 LearningWorks the answer will be in the Answer Pane. In Smalltalk messages are sent to objects The message + 3 , is sent to the receiving object 2 , which is called the receiver2 is the method selector, which means that + is the name of the message, and refers to a method with the same name. The method contains the Expressions that is executed when the message is sent to the receiver + is a Binary method. More on this later The following Workspace animation shows the above process for M206 LearningWorks
|
Smalltalk MethodsThere are 1000's of already written Smalltalk methods that are classified into different groups called Classes. There are 7 slightly different + methods depending on the Class of the receiver. Because the receiver 2 is an integer, then the + method in the integer Class is automatically used by Smalltalk. Classes are introduced in chapter 6 of the OU course. These methods can be examined using a Class browser. This is done later in the course. If +is looked up using the Class browser the following is seen.
Examination of the method. NOTE: Athough a brief description of the method and its Expressions,is given below. At this stage it is not necessary for you to understand how the code works. Understanding takes time. What is important is to be able to understand the comment in the method, when a comment is available. The comment is the "wording within the double quotes". As you progress through the course you will write or amend methods. Assume 2 + 3 is being evaluated. Remember that
|
| Section of Method | Explanation |
| +aNumber | Method heading consisting of the name + and argument aNumber which in our example will refer to 3 |
| "Answer the sum of the receiver and the argument, aNumber." |
The method body consists of the comment and the code. A descriptive comment, stating what the method does, not how it does it.. Comments are always within double quotes, and are disregarded by the compiler. As you continue with the OU course, it is important to read these comments. It may take a while before the style in which they are written, becomes easily readable. Answer is the term used in Smalltalk, to say what will be returned by the method or expression In plain English this is saying "Return an answer (which will be 5) after adding 2 and 3 |
| ^aNumber sumFromInteger:self | The code for the method. I do not expect
you to understand this code in this lesson. But note.
|
Remember
Note. Usually in TMAs when you are asked to supply code, a comment is also required.
What the code does.
The value of the comment is of immense importance. It states what the method does rather how it does it.
Simple MessageOf course, an object can do nothing by itself. In Smalltalk, you send messages to objects to make things happen. Messages are similar to function calls in other languages. For example, look at this Smalltalk expression, composed of a single message: 20 factorial If evaluated in the workspace. The answer would be displayed in the answer pane and would be a very large integer: 2432902008176640000 A simple message expression that can be evaluated, can be of the following types
Far more complex expressions can be also be evaluated. You will be doing this in Smalltalk tutorial 6. |
Glossary / terminologyThis Glossary is quite heavy reading, but it contains many of the main key words that are essencial for you to understand. This understanding will take some time to aquire. You should re-read this Smalltalk tutorial and this glossary from time to time, as the principles of Smalltalk programming in sink in. The main principle of Smalltalk is very simple ... You send a message to a receiving object, to make that object do something. Note: The message causes a method of the same name to be executed. |
|||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Term | Meaning | ||||||||||||||||||
| Answers | See Message answer | ||||||||||||||||||
| Argument | An argument contains additional information that
Binary and Keywordmessages
require. Unary messages do NOT have arguments.
The argument is past to the method that corresponds to the message. A variable in the header of the method will refer to this argument. Workspace example...anAccount balance: 100anAccount is the receiver. balance: is a keyword message selector. balance: 100 is the message. 100 is the argument. The method balance:balance: anAmount
"Set balance of receiver to anAmount (a number).
Answer the receiver."
balance := anAmount
See method in this Glossary for more information
on this method |
||||||||||||||||||
| Binary message | The selector in a binary message is composed
of one or two non-alphanumeric characters. Examples: Arithmetic Binary selectors + - * / . E.g. 2 + 3 Comparison selectors=<==<=> >=E.g. ( aNumber < anotherNumber ) The , (comma) character is binary selector used to concatenate(join together) objects . An example of the concatenate message with string literal objects is 'Hello', ' World' Which would answer with the string literal 'Hello World'. See Keyword and Unary |
||||||||||||||||||
| Class | A class describes the behaviour of a set of objects of the same kind | ||||||||||||||||||
| Comment | Commentsare ignored by Smalltalk and are
not executed. Comments are enclosed in double quotes. A comment
can be placed anywhere in the program code but the initial comment
should describe what the method does. Other comments can be used to explain how code works. |
||||||||||||||||||
| Evaluate | An expression can be evaluated i.e. compiled and executed. An answer is returned. | ||||||||||||||||||
| Expression | See Message expression. | ||||||||||||||||||
| Getter | See getter message and getter method. | ||||||||||||||||||
| Keyword message | A keyword is a selector with one or
more arguments. The keyword always ends with a colon,
there is no space between the keyword and the colon. The colon
is followed by a space, then an argument. E.g. with a single argument
plus: 3 at:put: is an example of a keyword requiring 2 arguments. E.g. 'ABDDE' at: 3 put: $C. The arguments are 3 and $C. Note when referring to the selector at:put: there are no spaces. How in use (see example) the keyword selector, at:put:, is split up with a space and argument following each colon 'ABDDE' at: 3 put: $C. See also Binary and Unary messages. Tutorial 3. at:put: |
||||||||||||||||||
| Message |
The message printstring
|
||||||||||||||||||
| Getter and Setter Methods | Objects have instance variables (Called attributes
in the early weeks of the M206 course). These instance variables
contain information about the object.
The names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:
|
||||||||||||||||||
| Getter Methods |
Getter message or get message, asks for information.
E.g. refer to the corresponding getter method. |
||||||||||||||||||
| Getter message |
A getter method or get methodis one of the 2 assessor pair methods. Getter methods
The English equivalent is "This is a question to aHoverfrog, What is your current height ?" |
||||||||||||||||||
| Setter Message |
Setter message or set method, refer to the corresponding setter method. The setter method always has a colon after the name, this colon is followed by a space then a parameter E.g. Frog1 position: 2 The English equivalent is "Frog1 make position 2 you current position" |
||||||||||||||||||
| Setter method | A setter method or set method is the
other of the 2 assessor pair methods.
|
||||||||||||||||||
| Message answer | When a message is sent to a receiving object, an
answer is always returned. The message answer may or may not be
used. If an answer is not explicitly coded in the method using
the caret ^, then the default answer is the receiving object.
Suitable message answer object may be used subsequently as the receiver or argument of another message. |
||||||||||||||||||
| Message expression |
|
||||||||||||||||||
| Message selector | Selector is the word given to the name of a message.
|
||||||||||||||||||
| Method | A method contains the code that is
executed when the message with the corresponding name
is sent to an object.
Workspace example...See Argument in this Glossary for information on the following lineanAccount balance: 100The message balace: 100 has been sent to the receiving object anAccount. This message is saying in very plain English,"Make the balance of anAccount equal 100". In Smalltalk jargon this would be "Set the value of the attribute (instance variable) balance, of the object refered to by anAccount, to 100." Lets look at the method balance:balance: anAmount
"Set balance of receiver to anAmount (a number).
Answer the receiver."
balance := anAmount
Examination of this method...
|
||||||||||||||||||
| Object |
Everything in Smalltalk is an object. As long as you remember this a sentence may make more sense if, in your mind, you remove the majority of occurances of the word 'object' from that sentance. E.g. The above is easier to read as: The Open universities course M206 in emphasising the point that every thing in Smalltalk is an object over uses the word to an extent that it almost confuses the student.
Smalltalk
The Open universities course M206 in emphasising the point that every thing in Smalltalk is an object over uses the word to an extent that it almost confuses the student. Tip: E.g. The above is easier to read as:
|
||||||||||||||||||
| Protocol | The set of messages an object understands. Each object responds to a particular set of messages (its protocol). |
||||||||||||||||||
| Receiver | The object which receives a message. Also
known as the receiver object.
anAccount balance: 100In the above expression anAccount is the receiver of message balance: 100 |
||||||||||||||||||
| Setter | See set message and set method. | ||||||||||||||||||
| Unary message | Unary messages do NOT have arguments. Examples
of Unary expressions 123 printString '123' asNumber See Binary and Keyword messages |
||||||||||||||||||
|
| Home | Next Tutorial 2. Precedence | Top
More sites by John McGuinn . HTML and Web Design | C Programming Leeds 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
Copyright © John McGuinn 2000 - 04 |
|||||||||||||||||||