3. A Simple Counter
Short Description
The first part of this exercise series is a tutorial that will guide you in building … Although protocols can have any name, Smalltalk programmers follow …
Website: www.iam.unibe.ch | Filesize: 100kb
Content
Dynamic Object-Oriented Programming with Smalltalk
HS 2007
Prof. O. Nierstrasz
Marcus Denker, Philipp Bunge
In this exercise, you will write your first complete program. The program is very simple, the goal of
the exercise is to get used to the Squeak environment and the basics of the language. From the Squeak
by Example book, you should read the chapters 5-7.
3. A Simple Counter
We want you to implement a simple counter that follows the small example given below.
|counter|
counter := SimpleCounter new.
counter increment; increment.
counter decrement.
counter value = 1
The first part of this exercise series is a tutorial that will guide you in building the counter example.
Creating your own class
In this part you will create your first class. A class is associated with a category (a folder containing the
classes of your project).
The steps we will do are the same ones every time you create a class, so memorize them well. We
are going to create a class SimpleCounter in a category called DemoCounter. Figure 1 shows the
result of creating such a category.
Figure 1: The category is created.
1Dynamic Object-Oriented Programming with Smalltalk
HS 2007
Prof. O. Nierstrasz
Marcus Denker, Philipp Bunge
Creating a Class category
In the System Browser, get the context menu on the left pane (alt-click on the mac) and select add item.
The system will ask you a name. You should write DemoCounter. This new category will be created
and added to the list.
Creating a Class
Creating a class requires five steps. They consist basically of editing the class definition template to
specify the class you want to create.
1. Superclass Specification. First, specify the superclass of the class you are creating. The class
definition template should show Object by default, which is exactly what we want.
2. Class Name. Next, you should fill in the name of your class by replacing the word NameOfSubClass
with the word SimpleCounter. Take care that the name of the class starts with a capital letter
and that you do not remove the # sign in front.
3. Instance Variable Specification. Then, you should fill in the names of the instance variables of
this class. We need one instance variable called value.
4. Class Variable Specification. As we do not need any class variable…
Get the file Download here
Related Books:Related Searches: exercise series, object oriented programming, class category, bunge, left pane
Comments
Leave a Reply