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:

Step One: Entering Smalltalk:

After logging onto the system, enter Smalltalk using the command

	visual &

You should receive a message that a changes file is being created for you. Three windows will appear on your screen.

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. 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 Browsers menu in the launcher.

The leftmost pane of the System Browser is a list of categories. Each category contains related classes. Create a new one called Muppets by clicking the middle button in that pane, choosing Add, and typing 'Muppets' in the prompter window.

The class Muppets will be added to the category list, and a template for defining a new class has appeared. Now, create the Muppet class by:

When you think you have gotten 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.

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.

name
	^name

name: aName
 	name := aName.

Create a protocol for initialization, 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''m ', name; cr.

Test your person code by typing the following in a Workspace (either the system one or a new one from the Utilities 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. Here is what you do: 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: Further Exploration

Try typing the following code into the workspace.

frogMuppet inspect.
grouchMuppet inspect.

This causes the inspector window to open, allowing you to see the pieces of your object.

Modify the Muppet greet method to generate a breakpoint and open the debugger by adding the message:

	self halt.

into the code. Look over the call stack and other features of the debugger.

If you have the time, you add more functionality to the muppet objects. Can you cause them to interact?

Step Six: Turning in the code

Go to the system Browser and select the Muppet category. Choose the middle-button menu item File Out and type a file name (muppet.st) when prompted. Send the file to your TA by using the relevant command

cat muppet.st | mail smk@cc.gatech.edu
OR
cat muppet.st | mail jyan@cc.gatech.edu
OR
cat muppet.st | mail guzdial@cc.gatech.edu