March 19, 2024

Smalltalk Exercises - Collections continued

 

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 7
The following code uses information taken from infamous air disasters:

airDisasters := Dictionary new.
airDisasters at: 'de Havilland DH-106 Comet'
             put: (Date newDay: 10 month: #January year: 1954);
             at: 'Boeing 747-121'
             put: (Date newDay: 21 month: #December year: 1988);
             at: 'Airbus A300B2'
             put: (Date newDay: 3 month: #July year: 1988);
             at: 'Aerospatiale Concorde'
             put: (Date newDay: 25 month: #July year: 2000);
             at: 'Hawker Siddeley Trident'
             put: (Date newDay: 18 month: #June year: 1972).

Write an expression that will display, one at a time, each of the dates of the disasters.

Link to answer for Q7

 

Question 8
(i) With reference to Q7, create a suitable collection class, referenced by the variable disasterPlanes, which will store, in alphabetical order, all the keys of the dictionary referenced by airDisasters.
(ii) Now write an expression, making reference to disasterPlanes, which will display a single dialogue box listing of all the planes, in alphabetical order.

Link to answer for Q8

Question 9
Write an expression series, which will count the number of vowels in the string 'Ultrarevolutionaries' and display this information in a dialogue box.

Link to answer for Q9

 

Question 10
The following code creates seven dictionaries for recording the names and heights of mountains across continents.

asiaMountains := Dictionary new.
asiaMountains at: 'Mount Everest' put: 8848.
asiaMountains at: 'Annapurna' put: 8091.
asiaMountains at: 'K2' put: 8611.
sAmericaMountains := Dictionary new.
sAmericaMountains at: 'Huascuran' put: 6768.
sAmericaMountains at: 'Aconagua' put: 6959.
nAmericaMountains := Dictionary new.
nAmericaMountains at: 'Mount McKinley' put: 6194.
nAmericaMountains at: 'Mount Waddington' put: 4016.
africaMountains := Dictionary new.
africaMountains at: 'Kilimanjaro' put: 5895.
africaMountains at: 'Mount Cameroon' put: 4095.
euroMountains := Dictionary new.
euroMountains at: 'Elbrus' put: 5642.
euroMountains at: 'Mont Blanc' put: 4810.
euroMountains at: 'Ben Nevis' put: 1344.
oceaniaMountains := Dictionary new.
oceaniaMountains at: 'Kosciuszko' put: 2228.
oceaniaMountains at: 'Mount Cook' put: 3754.
antarticMountains := Dictionary new.
antarticMountains at: 'Vinson Massif' put: 4897.
antarticMountains at: 'Mount Erebus' put: 3794.

"Each dictionary is added to an array."

arrayOfMountains := Array new: 7.
arrayOfMountains at: 1 put: asiaMountains.
arrayOfMountains at: 2 put: sAmericaMountains.
arrayOfMountains at: 3 put: nAmericaMountains.
arrayOfMountains at: 4 put: africaMountains.
arrayOfMountains at: 5 put: euroMountains.
arrayOfMountains at: 6 put: oceaniaMountains.
arrayOfMountains at: 7 put: antarticMountains.

Your task is to write a Smalltalk expression series, which will do the following:

  1. Create a suitable collection object, referenced by mountainsOfTheWorld, which will be used for adding each name of every mountain in arrayOfMountains. The names must be stored in alphabetical order.
  2. Display, via a series of dialogue boxes, the first three names stored in mountainsOfTheWorld
Link to answer for Q10

 

Question 11
A class Mountain is created to model some properties of a mountain. It has two instance variables, name and height, which reference a string and integer object respectively. The usual Smalltalk getter and setter messages have been provided in the instance protocol.
Seven sets have been instantiated along the lines of Q10, and Mountain instances are added e.g.

everest := Mountain new.
everest name: 'Everest';
        height: 8848.
asiaMountains := Set new.
asiaMountains add: everest.

Once again arrayOfMountains will reference an array, but in this case each of the stored objects will be a set rather than a dictionary.

Again you are asked to create a suitable collection object, referenced by mountainsOfTheWorld, which will be used for adding each Mountain instance that is stored in arrayOfMountains. However, this time you are required to order each of the stored Mountain objects according to their height, from highest to lowest.

Link to answer for Q11

 

Back to » Collection questions - 1

Back to « Iteration exercise

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page