Unformatted text preview:

CollectionsCollectionCollection as an Abstract ClassSlide 4Abstract class as typeFinding an elementFind an ElementTransform a CollectionNonuniform Collection ProtocolCollection ProtocolsCollection MenagerieSlide 12OrderedCollectionSlide 14OC::do:OC::add:Slide 17Using OrderedCollections(continued)IntervalSlide 21Interval do:Slide 23Interval at:Interval at:put:SetHashClassic Set BugsSequenceableCollectionSlide 30Set bugsSet do:Set includes:Slide 34PowerPoint PresentationClassic Collection BugsConclusionCollectionsInheritancePolymorphismObject-oriented Programming and Design - Johnson & Yoder - Day 32Collection74 subclasses of Collection in Squeak 3.10Some are abstractSequenceableCollectionArrayedCollectionMost are concrete - Array, Bag, Dictionary, Interval, String, Set, SymbolObject-oriented Programming and Design - Johnson & Yoder - Day 33Collection as an Abstract ClassCollection has no instance variables.Collection defines as subclassResponsibilitydo:add:, remove: (Changeable)at:, at:put: (Sequenceable)Object-oriented Programming and Design - Johnson & Yoder - Day 34Collection as an Abstract ClassTemplate methods defined in terms of do:select:, collect:, inject:into:, detect:ifAbsent:, sizeObject-oriented Programming and Design - Johnson & Yoder - Day 35Abstract class as typeAll collections understand same protocoldo: iterateselect: subcollectioncollect: transformed collectioninject:into: reduce collection to valueincludes: does receiver contain it?detect: find an elementObject-oriented Programming and Design - Johnson & Yoder - Day 36Finding an elementnumbers do: [:each | each isOdd ifTrue: [^firstOdd := each]].… firstOdd ... numbers do: [:each | each isOdd ifTrue: [firstOdd ifNil ifTrue: [firstOdd := each]]]firstOdd := numbers detect: [:each | each isOdd]Object-oriented Programming and Design - Johnson & Yoder - Day 37Find an Elementdetect: aBlock ifNone: exceptionBlock "Evaluate aBlock with each of the receiver's elements as the argument. Answer the first element for which aBlock evaluates to true."self do: [:each | (aBlock value: each) ifTrue: [^each]].^exceptionBlock valueObject-oriented Programming and Design - Johnson & Yoder - Day 38Transform a Collectioncollect: aBlock | newCollection |newCollection := self species new.self do: [:each | newCollection add: (aBlock value: each)].^newCollectionObject-oriented Programming and Design - Johnson & Yoder - Day 39Nonuniform Collection Protocoladd: and remove: are defined by collections whose size can change---Set, OrderedCollection, Bag, etc.ArrayedCollection and Interval do not implement add: and remove:at: and at:put: are defined by SequenceableCollection and Dictionary, but not by Set or Bag.Object-oriented Programming and Design - Johnson & Yoder - Day 310Collection ProtocolsCollection do:, size, select:, collect:, inject:into:, includes:, detect:ChangeableCollection add: addAll: remove: removeAll:SequenceableCollection at: at:put first last , copyFrom:to:Object-oriented Programming and Design - Johnson & Yoder - Day 311Collection MenagerieArray, StringSeq.SymbolSeq., read-only, uniqueOrderedCollectionSeq., Changeable., a sequenceSet, BagChangeableObject-oriented Programming and Design - Johnson & Yoder - Day 312Collection MenagerieDictionaryat: and at:put: with any object as keyIntervalSeq., numeric, read-onlyRunArraySeq., compact encodingObject-oriented Programming and Design - Johnson & Yoder - Day 313OrderedCollectionInstance variables:Instance variables:firstIndex, lastIndexfirstIndex, lastIndexA changeable sequenceable collection.A changeable sequenceable collection.Supports add:, remove:, at:, at:put:Supports add:, remove:, at:, at:put:Object-oriented Programming and Design - Johnson & Yoder - Day 314OrderedCollectionat: anInteger anInteger isInteger ifFalse: [ ^...].(anInteger < 1 or: [anInteger + firstIndex - 1 > lastIndex])ifTrue: [^...]^ super at: anInteger + firstIndex - 1Object-oriented Programming and Design - Johnson & Yoder - Day 315OC::do:do: aBlock firstIndex to: lastIndex do: [:index | aBlock value: (self basicAt: index)].size^ lastIndex - firstIndex + 1Object-oriented Programming and Design - Johnson & Yoder - Day 316OC::add:add: newObject "Include newObject as one of the receiver's elements. Answer newObject."^ self addLast: newObjectObject-oriented Programming and Design - Johnson & Yoder - Day 317OC::add:addLast: newObject "Add newObject to the end of the receiver. Answer newObject."lastIndex = self basicSize ifTrue: [self makeRoomAtLast].lastIndex  lastIndex + 1.self basicAt: lastIndex put: newObject.^ newObjectObject-oriented Programming and Design - Johnson & Yoder - Day 318Using OrderedCollectionsSuppose that list is an ordered collection. We want newList to be the reverse of list.solution 1newList := OrderedCollection new.list do: [:each | newList addFirst: each]Object-oriented Programming and Design - Johnson & Yoder - Day 319(continued)solution 2newList := OrderedCollection newWithSize: list size.index := list size.list do: [:each | newList at: index put: each. index := index - 1]Object-oriented Programming and Design - Johnson & Yoder - Day 320IntervalInstance variables: start, stop, step1 to: 10001 to: 1000 by: 31 to: 1000 do: [:each | sum  sum + each]Object-oriented Programming and Design - Johnson & Yoder - Day 321IntervalNumberto: stop^ Interval from: self to: stop by: 1Object-oriented Programming and Design - Johnson & Yoder - Day 322Interval do:do: aBlock | n end |n := 0.end := self size - 1.[n <= end]whileTrue: [aBlock value: start + (step * n).n := n + 1]Object-oriented Programming and Design - Johnson & Yoder - Day 323Interval do:do: aBlock| aValue |aValue := start.step < 0ifTrue: [[stop <= aValue]whileTrue: [aBlock value: aValue.aValue := aValue + step]]ifFalse: [[stop >= aValue]whileTrue: [aBlock value: aValue.aValue := aValue + step]]Object-oriented Programming and Design - Johnson & Yoder - Day 324Interval at:at: anInteger (anInteger >= 1 and: [anInteger <= self size])ifTrue: [^start + (step * (anInteger - 1))]ifFalse: [^self subscriptBoundsError: anInteger]Object-oriented Programming and Design - Johnson & Yoder - Day 325Interval at:put:at: anInteger put: anObject "Provide an error notification that storing into an Interval is not allowed. "self error: 'you can not store into an interval'Object-oriented Programming and Design - Johnson & Yoder - Day 326SetA hash table. nil means an unused entry.Instance variables: tally3 19


View Full Document

UIUC CS 598 - Inheritance Polymorphism

Download Inheritance Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?