CS 342 Lecture -*- Outline -*- * collections classes (section 7.1.5, page 280) ** the hierarchy and overview (page 281) all use list as representation (except List, of course) like our stack example, in instance variables of rep 2 ways to use another class as client as subclass ** Collection (p. 282) *** spec (type) suppose x is a collection, what can you do with it? could do something with each element can add: to it (usually) can ask for its size can ask if it's empty examples of client code in class: size, isEmpty, includes write something nonEmpty oddSized put 1 in it get set of its elements *** impl (code) an "abstract class", no initCollection, mkCollection no instance variables what benefits does this provide to subclasses code for size, isEmpty, includes: ** Set (page 283) *** spec what can you do with a set? everything you can do with a collection nothing new write: singleton union intersection *** impl a concrete class, has initSet, mkSet note instance variables inherited what's the invariant for the representing list? what does size do? ** KeyedCollection (p. 285) *** spec think of it as a table (mutable map from keys to values) what can you do with a KeyedCollection? everything can do with a collection use at: to get value for a given key use at:put: to change mapping in domain currentKey gives key associated with last first or next does first, next give elements in order of keys? no, there may be *no* order on the keys only assumes keys respond to = examples of client code in class: at:, includesKey:, indexOf: write: print all elements and keys find set of keys that map to a given value replace all elements by 0 get the 3rd element change the ith element to i *** impl relies on at:put: and currentKey and inherited ops ** Dictionary (p. 285-6) *** spec generic association of keys to values defines associationAt: to get association (pair of key-value) exported? write: create a dictionary ... find set of keys that map to a given value *** impl what is type of table? currentKey? look at first, next at:put: ** SequencableCollection (page 287) *** spec keys are consecutive integers, first & next give in order defines firstKey, lastKey (bounds of keys) write: print odd numbered elements set all the odd numbered elements to the even numbered ones sum all the elements of one of ints *** impl abstract redefines at: to take advantage of order ** List *** spec car, cdr car:, cdr: like rplaca and rplacd in LISP (mutators) add: modifies the front of the list removeFirst, takes off front *** impl look at initList, why is there always one cell? ** Array *** spec what is at:put: like in Pascal? write: reverse of array *** impl what is type of instance variables?