-
Notifications
You must be signed in to change notification settings - Fork 1
/
Collection.oz
62 lines (55 loc) · 2.01 KB
/
Collection.oz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Collections
You are the programmer of a virtual library. In this virtual library, the user can put new books, get the last books from the library (and removes it), check if the library is empty and merge two libraries. In order to implement that, you will write a class named Collection.
A collection regroups values. In this exercise, you are asked to build a class named Collection. This class has to contain the following methods:
- init (initializes the collection)
- put(X) (inserts X in the collection)
- get($) (removes the last element put in the collection and returns it)
- isEmpty($) (returns true if the collection is empty and false otherwise)
- union(C) (performs the union of the current collection and the collection C. This means that each element of C must be put at the beginning of the current collection. After the call, C is left empty and the current collection contains the union.)
For the union method, think about using put. You can use an object's method inside this object's other methods. In the future, you might need to call this method on another object of the same class (which can be done in the traditionnal way), but in this case (and in many other cases), you'll probably need to call the method on the object itself. To do so you can use the keyword self:
{self put(X)}
Hint: Think about using a cell containing a list, as attribute.
*/
declare
class Collection
attr L
meth init
L := nil
end
meth isEmpty($)
@L == nil
end
meth put(Book)
L := Book|@L
end
meth get($)
local Book in
Book = @L.1
L := @L.2
Book
end
end
meth union(C)
local Book in
if {C isEmpty($)} then skip
else
{C get(Book)}
{self put(Book)}
{self union(C)}
end
end
end
end
Ca={New Collection init}
Cb={New Collection init}
{Browse Collection.methods.init}
{Ca put(1)}
{Ca put(3)}
{Cb put(2)}
{Cb put(4)}
{Cb put(6)}
{Browse {Cb get($)}}
{Ca union(Cb)}
{Browse {Ca isEmpty($)}}
{Browse {Cb isEmpty($)}}