CS320 Lab 3 Object oriented programming Keep your copy of the CS320 Lab Manual open at the Smalltalk page while using Smalltalk. The best way to learn smalltalk is to do the experiments below. You can do these labs in three ways - (1)You can call Smalltalk from UNIX by 'st' and input the same commands by hand. (2)Alternately you you can use 'vi' to look at this file and then use '!n!st' to send `n` lines to be interpreted by 'st'. (3)You can also open two windows (if you use XWindows), one with this file and one running 'st'. The Copy and Pste between the two... An almost complete listing of all objects and their messages is in /u/faculty/dick/lib/smalltalk/methods for online reference. In 'st' you could try this where W is a word: 'grep -i W /u/faculty/dick/lib/smalltalk/*' unixCommand to get help on that word. .X 1 Numbers, Binary and Unary Messages in Smalltalk Your task is to figure out how Smalltalk interprets simple expressions. It is not the same as any other common programming language. Start the interpreter (st) and type in the following commands and note what happens. 1 1 + 2 1 + 2 (1 + 2) 1 + 2 sqrt 2 sqrt * 2 sqrt 2 * 2 sqrt 1 + 2 * 3 2 * 3 + 1 3.14159 * 3 squared 2 even and: 1 odd 2 even and: [ 1 odd ] 12 \\ 13 12 / 13 12 // 13 5 factorial 1<2 1+2>2 (1+2)>2 1>2+3 1>2>3 1*2*3*4*5 Integer methods Number display .X 2 Keyword Messages The key to understanding what happens is that all the expressions are an object followed by a series of binary and unary messages. The following have "keyword messages" being sent to a number. 2 between: 1 and: 3 2 between: 3 and: 1 123 gcd: 99 321 rem: 31 321 min: 31 321 max: 31 1 exp 1 exp ln 1.2345 rounded 1.2345 truncated -1.2345 truncated 1.999 truncated 10 factorial ln 12345 asString 1 exp between: 1 and: 3 Write down what you think the syntax of Smalltalk expressions might be. After doing this check out /u/faculty/dick/cs320/smalltalk/syntax... .X 3 Not an experiment but work to do with a friend. Get a copy of /u/faculty/dick/cs320/smalltalk/syntax and search for the syntax of expressions. Make copy and delete one word or string form each definition and mail the result to a friend (mailx friend 1) ifTrue: [ 'sqrt(2) > 1' print ] ( 2 sqrt > 1) ifFalse: [ 'sqrt(2) < 1' print ] ( 'ape' < 'zebra' ) ifTrue: [ 'ape' print ] ifFalse: [ 'zebra' ] In other labs I've asked you to write 'min' and 'max'...but Smalltalk has already got these defined for all objects in Class Magnitude: 1 min: 3 'cat' max: 'dog' So in this lab have a look at Magnitude viewMethod: #min: Then try to guess what the #max: method looks like. The book describes how Little Smalltalk's Boolean work... The 'viewMethod:' message can be sent wth the symbol for a message to a class - and it will show you how objects in that class respond to the message: Boolean methods Boolean viewMethod: #ifFalse: Boolean viewMethod: #ifTrue: True viewMethod: #ifTrue:ifFalse: False viewMethod: #ifTrue:ifFalse: Make notes on the syntax! .X 11 Structure of a Method Look at the code for the #factorial message Integer viewMethod: #factorial You can change a method by editting it...just input a similar command and you will be moved into 'vi' with the current version - edit and exit in the usual way (:wq or ZZ). To save a modified version of Smalltalk for future sessions use smalltalk saveImage and it will ask you where to put it. You can add a method to a class C with a simple 'C addMethod' command which gets you into 'vi' with a blank file. You need to put in a new method. The book has an example of the to:do: and the to:by:do: messages that send a block to an integer (page 470). Unfortunately we don't have to:do: and to:by:do: in Little Smalltalk - instead 1 to: 10 do: [ :i | (i+1)*i/2 ; print ] we write (1 to: 10) do: [ :i | (i+1)*i/2 ; print ] So... add to:do: as a method of Number. Hint: (1)KISS (2) Number addMethod Think about what you would like to happen after inputting 'add' help Add a method to the String class to handle the help message. Use String addMethod and 'vi' and output somethings as a response. Example: An idea might be to use messages that query the Smalltalk image itself. Another possibillity is to search one or more text files for relevant stuff: String addMethod enters 'vi' - tap i and input: help ('grep -i ', self, '/u/faculty/dick/lib/smalltalk/*') unixCommand Exit with ZZ Some other possibillities are to addMethod called help to the Classes: Symbol, Message, Class, Object,....Char. .X 12 A second useful method is to add a debugging print operator to Object. So that x db will print out x with a recognisable format and then returns x so that it can be embedded in other expressions: 5 db factorial db sqrt db In fact - if there is anything you hate then you can probably fix it. DISCLAIMER: Unless the code is too complex or Prof Budd has a hidden optimisation... .X 13 Structure of a Class The files /u/faculty/dick/src/smalltalk/*.st define all the Classes and Objects that can be preset into the initial Smalltalk image. Brouse through these noting how a Class is declared with variables (unique to each of its Objects but shared by all methods of one object) and then the Methods are added... The graphics and stdwin Classes are omitted in the current version. The original versions (or Oregon-al?) are in *.st.org so you can see the fixes and features I have patched in by: cd /u/faculty/dick/src/smalltalk; diff basic.st.org basic.st for example. Here are some new Classes of Objects you can easily add to Our Smalltalk: New SuperClass Notes Point Magnitude Has two variables x and y plus messages that retrieve and set these:- x,y,x:,y:,x:y: Perhaps with scaling and vector addition. Integer addMethod @ aNumber ^( Point New x: self y: aNumber ) Complex Number Like points has x and y variables but has a complete set of +-*/ exp ln arg abs... operations. Should use Float and have a message generallity that outputs a number greater than `Float generallity`. .X 14 ADTs in Small Talk Look at the methods of the Smalltalk classes called List and Set. By experimentation find out what is the difference between a Set and a List. Write notes or documentation defining the difference from the users point of view (as an ADT in other words). .X 15 Smalltalk source code The source code and documentation for Tiny SmallTalk is in /u/faculty/dick/src/smalltalk. Study the contents of this directory. Make a list of file names, sizes, and types (perhaps use `file * >>$HOME/files` and `wc -l * >>$HOME/files`). Determine what each of the files are for. Add the purposes in $HOME/files. Look in files for answers to questions like: Where did this implementation come from? What versions are there? What steps are taken to make a usable version? Hints. The original file was called 'smalltalk.tar.Z' This means it was a compressed "tape" archive. 'Q' (on 'blaze') gave me a listing and the uncompressed archive. 'tar xf filename' then extracted the files in the 'src' directory. '*.ms' files are documentation in a form that can displayed or printed on many printers (or typeset). The 'nroff' program (on most non-AIX UNIX) was used to translate the 'ms' files into the 'cat' files which can be viewed using 'less'. or printed'. .X 16 Find out how Prof T Budd divided up Smalltalk into modules. Look at the content of the source directory trying to get a "feel" for the way the author worked. Note answers to questions like: What guidelines did he follow? What do you think of the style? .X 17 Review your notes on Ada and the $CS320/ada/lab, focussing on generics as a simple object based feature. .X 18 Ada makes it possible to specify a data type in abstract and then provide alternate `bodies` which provide the same functionallity but with different performance properties. The problem of providing some simple 'integer sets' has been tackled in the following Ada files /u/faculty/dick/cs320/examples/intset1.adb /u/faculty/dick/cs320/examples/intset2.adb /u/faculty/dick/cs320/examples/intset3.adb /u/faculty/dick/cs320/examples/intsets.ads /u/faculty/dick/cs320/examples/tintsets.ada /u/faculty/dick/cs320/examples/primes4.ada Study these files and figure out which is the specification, which are the bodies. experiment with copying these files to your $HOME directory and compiling primes4.ada. There are many orders you might try but only some make sense: Here is one of them: intsets.ads intset1.adb primes4.ada After the above you can now compile: intset2.adb primes4.ada and then intset3.adb primes4.ada How does the resulting programs behavior change? If you change, intset[123].adb are you forced to recompile primes4.ada? If you change, intset[123].adb are you forced to relink primes4.ada? If you change, intsets.ads are forced to either recompile or relink primes4.ada to get an uptodate version? .X 19 How do intsets, esets and osets differ in $CS320/ada/*set* .X 20 Smalltalk. The following code gives the value of the nth harmonic number: harmonic ^((1 to: self) inject: 0 into: [ :x :y | x+(1/y) ]) This should be a method added to Number. Do this and then try: 2 harmonic 5 harmonic 10 harmonic 20 harmonic Then edit the harmonic method: Number editMethod: #harmonic so that what was x+(1/y) is now x+(1.0/y) and then save and exit 'vi'. Which version is faster? Why? ---------------------------------------------------