CS320 Laboratory Work on Object Oriented Programming Using Smalltalk Version 3 Details You can do these labs in two ways - you can use 'vi' to look at this file and the use '!n!st' to send `n` lines to be interpretted by 'st'. The smalltalk echo command makes st echo its input...this is therefore useful to put in front of a series of lab commands. Alternately you can be in Smalltalk and ask Smalltalk to edit existing methods... 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... 1. Numbers, Binary and Unary Messages Your task is to figure out how Smalltalk interprets simple expressions. It is not the same as any other common programming language. Start the interpretter (st) and type in the following commands and note what happens. smalltalk echo 1 1 + 2 1 + 2 (1 + 2) sqrt 1 + 2 sqrt 2 sqrt * 2 sqrt 2 * 2 sqrt 1 + 2 * 3 2 * 3 + 1 3.14159 * 3 squared 2 even 1 odd 2 even and: 1 odd 2 even and: [ 1 odd ] 12 \\ 13 12 / 13 (1/3)+(1/2) 12.0 / 13.0 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 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. smalltalk echo 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. Get a copy of ~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 with 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! 6. Structure of a Method The book shows you how to code factorial on page 473. Compare it with 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). 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. Another 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. A second usefule 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... 7. Structure of a Class The files basic.st, unix.st and local.st define all the Classes and Objects that are preset into the initial Smalltalk image on this system. 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... 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`. 8. ADTs in Small Talk Look at the methods of the Smalltalk classes called List and Set. By experimentation findout what is the difference between a Set and a List. Write notes or documentation defining the difference from the users point of view. 9. Simulation of Setting Up A Language Most systems that are obtained for nothing have small bugs or misfeatures in them. By research, trial, error, and guesswork figure out the author's probably mistake. Try to fix the bug.