CS 2390 Lab # 1: Building Muppet Objects

WHAT WE'RE GONNA DO:

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:

LAB GRADING POLICY:

You have 24 hours from the start of the laboratory to mail your lab to your TA. No late assignments will be accepted!


STEP ONE: ENTERING SMALLTALK:

Procedures for running Smalltalk or VisualWorks on the Baird Sums:

  1. Login using your gtnumber and password.
  2. Make a directory for you to work in for this Quarter. ie. mkdir cs2390
  3. Change Directory into the one you just created. ie. cd cs2390
  4. Run Visual Works Smalltalk by typing: visual &

Setting up VisualWorks

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.

STEP TWO: CREATING THE MUPPET CLASSES:

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?

STEP THREE: ADDING MUPPET FUNCTIONALITY

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 := 'Oh'.

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?

STEP FOUR: ADDING SUBCLASS FUNCTIONALITY

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.

STEP FIVE: MAKING THE MUPPETS INTERACT

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!

STEP SIX: TURNING IN THE CODE

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 relevant command

2390a:
cat
muppetproject.st | mail -s "LAB#1- YOUR NAME" jy17@prism

2390b:
cat
muppetproject.st | mail -s "LAB#1- YOUR NAME" tee@cc.gatech.edu

2390c:
cat
muppetproject.st | mail -s "LAB#1- YOUR NAME" gt4994a@prism.gatech.edu

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.