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')).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.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).
subsets: anArray
"Sets the value of the instance variable subsets to anArray"
<your statement>
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>
^subsets
Create an instance method called subsetsShow which will output via dialogue boxes the two sets contained in the instance variable subsets.
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).
|
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 |
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.
|
Set |
Dictionary |
Array |
OrderedCollection |
SortedCollection |
|
|
Accessing
|
|
|
|
|
|
|
Adding
|
|
|
|
|
|
|
Removing
|
|
|
|
|
|
|
Testing
|
|
|
|
|
|
|
Iterating
|
|
|
|
|
|
|
Static size
|
|
|
|
|
|
|
unique elements |
|
|
|
|
|
|
elements of same class |
|
|
|
|
|
Copyright © Peter and Pauline Curtis and Eric Lomas
Most recent revision:
24th May, 2001