CIS LECTURE 20

 

Smalltalk:

Note: this lecture was performed via demonstration, using a projection screen and the actual Smalltalk environment. This explains the somewhat cryptic nature of the resultant notes!!!

 

As always, the posted notes are NOT to be treated as an alternative to attending the lectures!!

 

Once downloaded, use Vw.exe for windows

 

Comments denoted with double quotes, i.e. "comment"

 

To calculate factorial, type:

 

250 factorial          "highlight this, then 'right click' and select 'show it' to display on

                                screen"

 

"to view classes, select:"

 

browse classes

     :c\

         stexpress

              docs

 

"for tutorial, select:"

browse classes

     :c\

         stexpress

              tutorial

 

"to go to classes, select:"

add subclass

 

"or go to methods, select:"

add methods   "type 'Stack'"

 

 

"once in the Smalltalk environment, you can create your own methods"

 

push:x     "where 'push' is the sub-routine and 'x' is the variable"

 

              self AddLast:x   "adds variable 'x' to the end of stack, 'self' is like 'this' in C++"

 

pop          "removes last item from stack"

 

              self removeLast

top          "returns top stack item"

 

          ^self last    "the symbol ' ^ ' is a dereferencing operator"

 

empty        "checks for empty space"

 

          ^self isEmpty

 

"go to classes"

 

file out     "save in tutorial"

 

S := Stack new         "highlight and right click 'show it'"

 

S empty                    "highlight and 'show it', the program outputs:"

True

 

S push : 2                 "highlight and 'do it', it won't display"

S push : 3                 "highlight and 'show it', the program displays:"

Stack (2 3)

 

S top                         "show it"

3

 

S push : 1                  "show it"

Stack (2 3 1)              " displays contents of Stack"

#(1 3 5 7)                  " the symbol ' # ' is for an array, select 'show it'"

3                              "CHECK THIS!!"

 

#('array' 'of' 'strings')

3                                                                 "number of elements in string"

 

'now is the time' at 6          "determines char value at position 6"

$6                                       "in Smalltalk, $ is a char"

 

#(1 2 3), #(4 5 6)                "concatenates"

(1 2 3 4 5 6)

 

"in Smalltalk, the symbol ' ! ' is the end of line character"

 

"the function 'sum array' is a variation of factorial"

 

|max a b|                 "this denotes local identifiers"

 

//         "this is the symbol for division"

 

/          "this is for fractions, which is supported by Smalltalk"

 

"for recursion, select:"

magnitude

      integer

        new method

 

mult:b               "recursive multiply"

   b > 1

      ifTrue  [ ^self + (self mult(b - 1))].

^self

 

"good luck, the management"