View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Lab2 - Sp2000

Lab 2 - Muppets & Objects

What you're going to do:

In this lab, you will use Squeak 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: Creating the Muppet Classes

To create a new class you need to open a System Browser. Click the left button on the gray background area, select "open", and select "open browser". A new window should appear, which has divided into 5 subwindows ("panes"): four panes across the top and one big one at the bottom.

The leftmost pane is a list of categories. Each category contains a bunch of related classes. Create a new categary called MuppetProject by clicking the middle button in that pane, choosing "add item", 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:

Leave the Object specificaton alone, so that the superclass of Muppet will be Object.

The result should look something like this:

     Object subclass: #Muppet
         instanceVariableNames: 'name greeting'
         classVariableNames: ''
         poolDictionaries: ''
         category: 'MuppetProject'

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 Two: 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, choosing "additem" by center-clicking in the protocol list, and typing in accessing at the prompt.

A method template will appear in the text pane. To add each new method, you will replace the top line with the message name, place a 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 two methods for accessing the name variable, as follows.

First, create a name method by changing the template in the text pane to look like this:

Now, use middle-button-menu to choose accept.

Click on name in the rightmost pane, deselecting it, so that the method template reappears. Edit the template to create a new method as follows:
     name: aName
     name := aName.

Again, accept it. You now have two methods in the accessing protocol,

Create a protocol for initializing, and place the following method inside it:
       greeting := 'I am an abstract muppet'.

Create a protocol for greeting, and place the following method inside it:
       Transcript cr. 
       Transcript show: greeting; cr.
       Transcript show: 'I am ', name; cr.

Open a workspace to test your code in, using the "open" menu from the background menu. Type the following code into the workspace:
     | muppet |
     muppet := Muppet new initialize.
     muppet name: 'Jim Henson'.
     muppet greet.

Select the text with the mouse, click the center button, and choose "do it". Do you get what you expect?

Step Three: 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.

Did you get the expected greetings? If not, try again before going on the the next step.

Step Four: Making the Muppets interact

Now, let's make the muppets interact. Write a method in the Muppet class that:

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 am Oscar the Grouch!

If you didn't get this result, go back and try again.

Step Five: Turning in the code

When you have finished, follow the posted turnin instructions to turnin your lab.

Link to this Page