Computer Programming 01
Computer Programming 01
Programming
using GNU Smalltalk
Canol Gkel
Last updated: 18.10.2009
Cover photo by: Tibor Fazakas over www.sxc.hu
Content on this book is licensed under a Creative
Commons Attribution 3.0 License or, at your choice,
under the GNU Free Documentation License 1.3, with
no invariant sections, front-cover texts, or back-cover texts
To the ant I accidentally crushed yesterday...
Canol Gkel
I
Preface
Computers are machines composed of hardware and software written for the
hardware. Whether you are an amateur user who uses his/her computer just to surf
the Internet or an average user who uses computer for daily tasks, you may need to
write a program for a specific duty. Maybe you are just a curious user who wants to
dominate the box in front of him before it starts dominating you. Programming is
the key word here because you make the computer do what you want by
programming it. Programming is like putting the soul inside a body.
This book intends to teach you the basics of programming using GNU Smalltalk
programming language. GNU Smalltalk is an implementation of the Smalltalk-80
programming language and Smalltalk family is in general a little bit different than
other common programming languages like C or Java. So, we will apply a different
approach to teach you programming compared to other computer programming
books out there.
You will see chapters of this book are mostly short compared to other programming
books. This is because of mainly two reasons. First one is my laziness. Second one is
that Smalltalk is a small and orthogonal language. By small, we mean there are fewer
programming concepts you should learn compared to other languages. Smalltalk is
built on a few carefully designed rules and concepts. By orthogonality, we mean
there are very few exceptions on Smalltalk compared to other languages. Because of
this two reasons you will learn almost the whole language in less than 100 pages.
This doesn't mean the things you can do with Smalltalk is limited. In contrast, this
small set of rules and orthogonality gives you great flexibility so that the only limit is
your imagination. Also, one of the greatest strength of Smalltalk is its powerful and
rich library which gives you most of the tools you need out-of-the-box. GNU
Smalltalk adds even more tools to this valuable toolbox. But because we will
concentrate in the core language in this first edition of the book we are going to
show you only the tip of the iceberg, namely, only the most important and most
frequently used functionality of the library.
If you are an experienced programmer who wants to learn Smalltalk, then you will
be surprised by the elegance of this carefully implemented language. Andrew S.
Tanenbaum says: "Fight Features. ...the only way to make software secure, reliable,
and fast is to make it small.". Smalltalk is certainly designed by scientists with this
mentality in mind.
II
We will often use this kind of box to speak to experienced programmers, referring a
person who has knowledge of another programming language than Smalltalk.
Newcomers do not have to read the contents of this box.
This book can also be used by experienced programmers. Actually, this book is the one
you should read because, most probably, GNU Smalltalk is pretty different than what you
have seen so far and this book teaches you Smalltalk as if it is your first programming
language.
Font Conventions
We used some font conventions throughout the text so that you can differentiate
different kind of materials we are talking about, easily.
Beside the Times New Roman font we use for normal content, we used an italic text
to emphasize a first appearance, or a definition of a word.
We used a fixed sized font while mentioning about a code piece.
We used an italic fixed sized font while mentioning about a code part you are
supposed to put a different code according to your tastes, the context of the program
or your computer's settings etc.
The codes which are meaningful even as they are, are showed with a sweet purple
rectangle at their left side, like below:
Outputs (results) of complete programs are given with a purple background, like
this:
Output of a program
The input part of a program which is entered by user while the program is running
are showed in bold face at the output:
Please enter your name: Canol
When we mention about keys on your keyboard, we'll write them between angle
brackets like <CTRL>, <Enter> or <Backspace>1.
We also have some special boxes:
Note:
This kind of boxes contain important details, some additional information or suggestions.
This kind of boxes contain some hints for people who knows some other language but
reading this book to learn GNU Smalltalk. Beginner users or even experienced
programmers don't have to read or understand things written in this boxes.
1 We will use <Enter> throughout the book for both <Enter> key on PCs and the <Return>
key on Macintosh computers.
IV
Question:
This kind of boxes contain some special questions which are answered at the and of its
chapter.
V
Contents
Preface.....................................................................................................................I
Blocks................................................................................................................47
VI
Selective Controlling..........................................................................................49
Repetitive Controlling........................................................................................50
Review Questions...............................................................................................54
Encapsulation.....................................................................................................57
Inheritance.........................................................................................................57
Polymorphism....................................................................................................60
Creating Your Own Classes...............................................................................60
Creating Objects from Classes...........................................................................65
Example.............................................................................................................66
Extending and Modifying Classes......................................................................71
self and super.....................................................................................................76
Review Questions...............................................................................................82
Chapter 1...........................................................................................................95
Chapter 2...........................................................................................................97
Chapter 3...........................................................................................................99
Chapter 4.........................................................................................................101
Chapter 5.........................................................................................................106
Postface.............................................................................................................115