Unformatted text preview:

1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 72Smalltalk Control StructuresObjectBooleanTrue one instance - trueFalse one instance - falseObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 73Boolean operationsifTrue: trueBlock ifFalse: falseBlockifTrue: trueBlockifFalse: falseBlock& aBoolean| aBoolean2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 74Using Standard Control StructuresFind minimum of x and ymin := x < y ifTrue: [x] ifFalse: [y]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 75Boolean operationsNormal and - both arguments alwaysevaluated(0 < i) & (i <= 100)Short-circuit and - second argument onlyevaluated when necessary(i <= array size) and: [(array at: i) > 0]3Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 76Print i and i^2 for i between 1 and 10i := 1[i <= 10] whileTrue:[Transcriptshow: (i printString);show: ’ ’;show: (i squared printString); cr. i := i + 1]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 77Another way1 to: 10 do: [:i |Transcriptshow: (i printString);show: ’ ’;show: (i squared printString);cr]4Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 78Evaluating Blocksvalue no argumentsvalue: one argumentvalue:value: two argumentsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 79Implementing Standard ControlStructuresTrueifTrue: trueBlock ifFalse: falseBlock^trueBlock valueFalseifTrue: trueBlock ifFalse: falseBlock^ falseBlock value5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 80Implementing Standard ControlStructuresNumberto: stop do: aBlock| index |index := start[index <= stop]whileTrue: [aBlock value: index. index := index + 1]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 81Loops with BlockswhileTrue: aBlock^self valueifTrue:[aBlock value.[self value]whileTrue:[aBlock value]]6Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 82Control Structures forCollectionsCollections are data structures likeStrings, Arrays, Sets, Lists, Dictionaries,etc.Collection class has subclasses String,Array, Set, List, Dictionary, etc.“Enumerating” protocol lets you operateon entire collection, taking a block as anargument.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 83Control Structures for Collectionsnumbersdo: [:each | each printOn: Transcript].numbersselect: [:each | each > 100].numberscollect: [:each | each ^ 2]7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 84(continued)numbersinject: 0 into: [:sum :i | sum + i]names with: addresses do: [:eachName :eachAddress |letter name: eachName address:eachAddress; print]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 85Abstract Classclass with no instancestemplate for subclasses, not instances.defines standard interfaceImportant element of design reuseHard to invent good abstract classes8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 86Abstract Classes and TheirFunctionStreamObject and printingCollection and iterationObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 87Methods in an Abstract ClassAbstract methodmethod that MUST be defined bysubclassesself subclassResponsibilityTemplate methodmethod defined in terms of abstractmethods9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 88Stream - for writingnextPut: anObjectnextPutAll: aCollectionaCollectiondo: [:each | self nextPut: each]crself nextPut: Character crObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 89Using WriteStreams for PrintingWriteStream of charactersaStream :=WriteStream on: (String new: 20).aStream nextPut: aCharacter.aStream nextPutAll: aString.aStream space; cr; tab.aStream contents10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 90Using WriteStreams for Printing■ Create WriteStream of characters■ Print on it■ Get the printed string■ Never use the original string that theWriteStream is streaming over, becauseit is abandoned when the stream grows.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 91Printing■ Object defines two methods that enableevery object to print itself. Note that thecontents of a WriteStream grows as needed.■ Public method - printString■ Method implemented by subclasses -printOn:11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 92Printing in ObjectprintString"Answer a String whose characters are adescription of the receiver."| aStream |aStream :=WriteStream on: (String new: 16).self printOn: aStream.^aStream contentsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 93Printing in ObjectprintOn: aStream"Append to the argument aStream asequence of characters that identifies thereceiver."| title |title := self class name.aStream nextPutAll: ((title at: 1) isVowelifTrue: [’an ’] ifFalse: [’a ’]) , title12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 94PrintingClasses customize printing byredefining printOn: and can inheritprintString without change.So, when you create a new class,define a printOn: method for it.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 95Concatenation, (comma) is defined inSequenceableCollection and so isdefined for Strings, Arrays,OrderedCollections, etc.It concatenates the receiver with theargument.‘this’ , ‘ is’ => ‘this is’13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 96ConcatenationSequenceableCollection defines a rich set ofcopying operations, too many to list here.Never write a loop to copy elements to orfrom an array.WARNING: Don’t concatenate larger andlarger strings, use WriteStreams.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 97Printing a CollectionprintOn: aStream"Append to the argument aStream asequence of characters that identifies thecollection. The general format forcollections is “Collection-name ( elementelement element )” unless there are a largenumber in which case the listing istruncated with the words ...etc..."14Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 98(continued)|


View Full Document

UIUC CS 497 - Smalltalk Control Structures

Download Smalltalk Control Structures
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 Smalltalk Control Structures 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 Smalltalk Control Structures 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?