CS 2390 Lab # 1: Building Muppet Objects
For more information on labs, see the "FAQ".
STABLE offers step-by-step help in completing this lab.
In this lab, we will use Smalltalk to create a small hierarchy of objects
that are person (or muppet) like in that they have names and can greet you.
You will create one abstract class called Muppet and two concrete
classes called FrogMuppet and GrouchMuppet.
By the end of this lab you should:
You have until Thursday (July 3) at 3:00 pm to turn in this lab. No late assignments will be accepted!
Procedures for running Smalltalk or VisualWorks on the Rich Sums:
From the FILE menu, choose settings and enter in the following line under Source
/remote/sun4m/sun4m/lib/visual/image/visual.sou
You should receive a message that a changes file is being created for you.
On your screen will appear:
Smalltalk uses all three buttons of the mouse extensively. In general, you use the left button to do selection from the launcher and moving the cursor. Clicking the center button will get you a menu of actions that can be done from that window (or the specific pane of that window). Clicking the right button will allow you to manipulate the window itself.
To create a new class you need to open a System Browser, which is in
the Browse menu -- choose All Classes.
The leftmost pane of the System Browser is a list of categories. Each
category contains related classes. Create a new one called MuppetProject
by clicking the middle button in that pane, choosing Add, and typing
'MuppetProject in the prompter window.
The category MuppetProject will be added to the category list, and a template for defining a new class appears. Now, create the Muppet class by:
When you think you have got it, tell Smalltalk to accept the code
-- click in the text pane with the middle button, and choose accept.
Assuming you have made no syntax errors, the class should appear in the
second pane of the browser. If you have made syntax errors, correct them
and try again.
Now create the FrogMuppet and GrouchMuppet classes. What is the superclass of these classes? Do they have any instance or class variables?
Now you will define the behavior of the Muppet class, which will
be inherited by the FrogMuppet and GrouchMuppet classes.
The third list in the System Browser is for protocols, which are groups
of related methods. Create a protocol for the Muppet class by selecting
that class, than choosing Add by center-clicking in the protocol
list, and type accessing at the prompt. (Protocols are categories
for methods.)
A method template will appear in the text pane. You will replace the
top line with the message name, place the comment in the double quotes,
and insert the text at the bottom. After editing the template for the new
method, you accept it to tell Smalltalk to compile it.
Create the following two methods for accessing the name variable.
(1) Change the template to look like this.
name
^name
Now, use middle-button-menu to choose accept.
(2) Now, click once on the protocol list on accessing (to de-select it), then again (to select it and make the template appear). Edit the template so that it reads:
name: aName name := aName.
Again, accept it.
Create a protocol for initializing, and place the following method inside it:
initialize greeting := 'I am an abstract muppet'.
Create a protocol for greeting, and place the following method inside it:
greet Transcript cr. Transcript show: greeting; cr. Transcript show: 'I am ', name; cr.
Test your person code by typing the following in a Workspace (either
the system one or a new one from the Tools menu). After typing the code,
select it with the mouse, click the center button and choose Do It.
| muppet | muppet := Muppet new initialize. muppet name: 'Jim Henson'. muppet greet.
Do you get what you expect?
Frogs and Grouches differ from ordinary Muppets in their greeting. For
each class create initialization protocols. Create an initialize
method for both classes that changes the greeting. The FrogMuppet greeting
should be 'Hi Ho', and the GrouchMuppet greeting should by 'Go Away'.
Test your muppets with the following code:
| frogMuppet grouchMuppet | frogMuppet := FrogMuppet new initialize. grouchMuppet := GrouchMuppet new initialize. frogMuppet name: 'Kermit the Frog'. grouchMuppet name: 'Oscar the Grouch'. frogMuppet greet. grouchMuppet greet.
Now, let's make the muppets interact. Write a method in the Muppet class that:
As discussed in class, you will probably need to create a name
method to access the object's name.
When you execute the following code:
| frogMuppet grouchMuppet | frogMuppet := FrogMuppet new initialize. grouchMuppet := GrouchMuppet new initialize. frogMuppet name: 'Kermit the Frog'. grouchMuppet name: 'Oscar the Grouch'. frogMuppet greetByName: grouchMuppet. grouchMuppet greetByName: frogMuppet.
You should see:
Hi Ho, Oscar the Grouch! I am Kermit the Frog!
Go Away, Kermit the Frog! I don't care! I am Oscar the Grouch!
Go to the system Browser and select the MuppetProject category. Choose the middle-button menu item File Out and type a file name (muppetproject.st) when prompted. Send the file to your TA by using the mail command.
For more information on turning in code, look at the
Lab FAQ.
cat muppetproject.st |elm -s "TI,Lab1,<your studentNumber>" cs2390@prism.gatech.edu
For example:
cat filename | elm -s "TI,Lab1,0123456789 cs2390@prism.gatech.edu
Don't enter in <>!!! You'll get a receipt next time we run the TurnIn system (Wed. night or Thursday morning.)
When you are done, EXIT FROM VISUALWORKS! It's very important that you explicitly exit from VisualWorks, using the File menu. Also, be sure NOT to Save, just Exit.
News Page | CS2390 Sum'97 Home Page | MMC | STABLE
Questions/comments/concerns to guzdial@cc.gatech.edu
Page last updated 6/23/97; 4:06:43 PM