M206 CHAPTER 23 EXERCISE

  1. Write code to create a set called vowels and add to it the five vowels ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ . Write a dialog that asks the user to input a letter and which displays the message: ‘Vowel’ or ‘Not a vowel’ depending on whether the letter is in this set. Convert any uppercase input to lowercase.

  2. Write code to output the letters in the set vowels with one space between each.

  3. Create a dictionary called euroCapitals where the keys are the capital cities of Eire, France, Italy and Spain and the values are the names of the countries. Write code to ask the user to input a capital city and to return the country if it is in the class euroCapitals otherwise to output the message answer: ‘Not in class’.

  4. Write code to output as a series of dialog boxes all the capital cities and their countries in the class euroCapitals. If you just wished to output the countries what code would you write?

  5. Consider the following expression series:

          aSet := M206Set new.
          aSet add: 'a'; add: 'b'.
          bSet := M206Set new.
          bSet add: 'c'; add: 'd'.
          cSet := aSet add: bSet

    As a result of evaluating this series, aSet will reference the set ('a' 'b' ('c' 'd')).

    1. Describe each of the elements of the set referenced by aSet.
    2. How many elements does the set referenced by cSet contain?
    3. Write down a Smalltalk expression whose evaluation would enable you to confirm your answer to b.
    4. Describe the object referenced by cSet.
    5. Write down and describe the union of the set ('a' 'b') and the set ('c' 'd').

Your answers to the above should have convinced you that you cannot simply use add: in order to implement union:. The strategy you need to adopt is to iterate through each element of aSet and bSet in turn and to add each to a new set which will become the union of the two sets.

M206 CHAPTER 24 EXERCISE

We wish to set up a means of partitioning a set of numbers into two subsets according to whether they are a) less than a specified integer or b) greater than or equal to the specified integer. Thus if the specified integer was 4, the set (1 2 3 4 5 6 7) would be split into the two subsets (1 2 3) and (4 5 6 7). We will do this by creating a new class called TestClass. Each member of TestClass has an instance variable called subsets which is itself an array with two elements both of which are sets. In the example just described, the first element of the array would be (1 2 3) and the second (4 5 6 7).

  1. Assuming we have created the class TestClass and its instance variable subsets, specify the instance method which sets the value of subsets to an array by completing the following:

    subsets: anArray
          "Sets the value of the instance variable subsets to anArray"
          <your statement>

  2. Write a message sequence to add all those elements of a set called myNumbers of real numbers which are less than 4 to the set called set1 and all the remaining numbers to the set called set2.

  3. Develop this sequence into an instance method for the class TestClass which will take any set of numbers and any specified partitioning number, create the array containing the two subsets as elements and assign this to the subsets instance variable of the object in the class TestClass. Complete the following template:

    partition: aSet with: aNumber
    "Partition the set aSet into two subsets, one with all numbers less than aNumber, the other with the remaining elements of the set. Create an array from these two subsets and assign it to the subsets instance variable."

          | set1 set2 tempArray |
          set1 := Set new.
          set2 := Set new.
          tempArray := Array new: 2.
          <your statements>

  4. We define the instance method subsets as follows: subsets "Answers the value of the subsets instance variable"

          ^subsets

    Create an instance method called subsetsShow which will output via dialogue boxes the two sets contained in the instance variable subsets.



M206 CHAPTER 26 EXERCISES
Ordered and Sorted Collections.

A supermarket stores its food in a warehouse. The warehouse records the name of the food item, its location and its "sell-by" date. A stock-check is carried out each evening in the supermarket and a list compiled of food needed to re-stock the shelves. This list is sent to the warehouse who prepare a pick list for which the criteria is that the oldest stock must be picked first (ie. that stock with the closest "sell-by" date).

  1. Given the following warehouse stock list discuss what classes would be appropriate to model the holding of stock in the warehouse. (Hint: think about what you are holding in the warehouse and how you will hold it). Draw a diagram to illustrate how these classes simulate the warehouse and stock held. (Note for the purposes of this example the warehouse has infinite capacity, ie. there is no upper limit on locations):

    Item

    Location

    Sell By

    Carrots

    E70

    1 Jul 98

    Potatoes

    C10

    7 Jul 98

    Beans

    A34

    15 Jul 98

    Cabbage

    Z45

    10 Jul 98

    Beans

    W32

    3 Jul 98

    Carrots

    T82

    10 Jul 98

    Cabbage

    P02

    4 Jul 98

    Carrots

    X31

    5 Jul 98

    Potatoes

    U77

    3 Jul 98

     

  2. If the following specific protocol is required by the warehouse model:

    receipt: aStock at: aLocation withSellBy: aDate

    "Models the receipt of new stock into the warehouse. Creates a new instance of StockItem setting its instance variables to aStock stored at aLocation with a sell-by date aDate. This instance is then added to the warehouse collection. Answer aStock."

    pick: aStock ifAbsent: aBlock

    "Remove aStock from the warehouse collection to re-stock the supermarket shelves. If multiple elements match aStock, then pick the one with the nearest sell-by date. Answer the location of aStock."

    Discuss the additional protocol required by your classes to support the warehouse model. Make a list of the class and instance methods that you may need.

  3. Outline the code for the 2 specific methods above and the instance creation methods for an instance of Warehouse.

  4. What type of class could be used to record substitute stock for items out of stock?

HINT

You may find it useful to fill in a table such as that which follows before or whilst you are completing the exercises.

 

Set

Dictionary

Array

OrderedCollection

SortedCollection

Accessing

 

 

 

 

 

 

Adding

 

 

 

 

 

 

Removing

 

 

 

 

 

 

Testing

 

 

 

 

 

 

Iterating

 

 

 

 

 

 

Static size

 

 

 

 

 

 

unique elements

 

 

 

 

 

elements of same class

 

 

 

 

 

Return to the OU Home Page


Copyright © Peter and Pauline Curtis and Eric Lomas
Most recent revision: 24th May, 2001