This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.

John McSweeney - picture detail

John McSweeney

May 12, 2008, 1:36 pm

M206 Computing: An Object-oriented Approach

Smalltalk Exercises - Collections

 

Clicking on the A graphic will open up a window displaying the answer to each question (switch off pop-up blocker if you have one enabled).

Question 1
The following creates a set of Chelsea footballers:

blues := Set new.
blues add: 'Ferreira';
      add: 'Terry';
      add: 'Tiago';
      add: 'Lampard';
      add: 'Robben';
      add: 'Carvalho'

A set of Portugese footballers is also created with:

portugal := Set new.
portugal add: 'Costinha ';
         add: 'Ferreira';
         add: 'Rui Costa';
         add: 'Tiago';
         add: 'Figo';
         add: 'Deco';
         add: 'Carvalho'

By using just the two variables and one message in the protocol of Set, write a simple expression that will return a set representing those Portugese footballers that play for Chelsea.

Link to answer for Q1. Left click and a new window will open

 

Question 2
(i) What is the textual representation of this set?
(ii) What is the textual representation of this set when it receives the printString message?

Link to answer for Q2. Left click and a new window will open

 

Question 3
The following code is used to create a set:

mountainSet := Set new.
mountainSet add: 'Matterhorn';
            add: 'Everest';
            add: 'K2'

Explain the consequences of evaluating the following expression:
mountainSet remove: 'Ben Nevis'

Link to answer for Q3. Left click and a new window will open

 

Question 4
Write an expression that will check the set referenced by mountainSet for the presence of the string 'Ben Nevis'. If the string is not present the user is notified by means of a dialogue box.

Link to answer for Q4. Left click and a new window will open

 

Question 5
Write a simple expression that will open dialogue boxes displaying all the elements in mountainSet.

Link to answer for Q5. Left click and a new window will open

 

Question 6
What class of object does deadPoets reference after evaluating the following?

deadPoets := Set new add: 'Milton';
                     add: 'Shakespeare';
                     add: 'Keats'

Link to answer for Q6. Left click and a new window will open

 

Next page » More collection questions

Back to « Iteration exercise