Student Originated Software 1997-1998
Fall Quarter

A Software Engineering Course at
The Evergreen State College

Student Originated Software 1997-8

Object-Oriented Programming (Smalltalk)

Lecture 2a

last time:


  • programming language paradigms....
  • selecting a programming language for a project....
  • selecting a language _to teach_
  • distinguish concept, ability, skill
  • why OO?
  • why smalltalk?


objects
  • what's an object: code + data
  • methods encapsulate variables
  • invoking methods?
  • objects send, receive messages;
  • sender, receiver;


what's a class?
defining objects - classes and instances
inheritance, overriding, polymorphism (next week)
using oo models for software development - modeling the real world!


the language....
naming conventions - height, myHeight; Book, aBook; Employee, anEmployee
literals (constants) 8r377, 16rFF,1.586e5,$a,#(1,2,3),#[1,2,3]
variables
sending messages
every message expression has a value.
unary, binary, keyword; left to right.
list addFirst: newComponent
list removeLast
ages at: 'Brett' put:3
address at: 'Peggy'
HouseholdFinances spend:32.50 on: 'utilities'
1+2*3
2*theta sin
(2*theta) sin
frame scale: factor max:5
frame scale: (factor max:5)
bigFrame width:smallFrame width*2
p.22

combining messages (chaining, cascading)
(book openAtPage: 1+2*3) print
most methods return self, usually by default, but not always!

block - a deferred sequence of messages
- evaluated when the block receives the message

aBlock := [a:=0].
aBlock value

MyBlock := [:anObject | anObject print].

anotherBlock := [:parm1 :parm2| |tmp|
tmp := parm1 incorporate:parm2.
tmp := rehash.
]

executing blocks
MyBlock value: 'This is a string sent to print'.
value, value:, value:value:, value:value:value:
valueWithArguments:argArray (> 3 arguments.

classes attributes (class and instance variables)
methods (class and instance methods)

Object subclass #SpendingHistory
instanceVariableNames: 'cashOnHand expenditures'
classVariableNames: ' '
poolDictionaries: ' '
category: 'Spending'

that's all there is to the language!

----------------

the smalltalk virtual machine
  • interprets code as it comes in
  • "the image" -- where code and object instances are stored
    the Smalltalk Virtual Machine (your image is its memory)
    the development environment (your classes + class library)
  • files - virtual image
  • sources visual.sources
  • (base) image visual.im
  • (your) image myvisual.im
  • changes file mycode.changes
  • file-ins mycode.st

-----------

advice:
  • smalltalk learning curve
  • be prepared for culture shock (not the waterfall model!)
  • start off small
  • explore and work interactively
  • be prepared to throw code away
  • get some help --
  • ask the lab aids
  • work with your partner!
  • (come to help sessions - if/when we have some)
  • check out the smalltalk FAQs and listservs


----------------

about visual works (minimal subset)
  • launcher (aka main window, transcript)
  • workspace
  • browser (to look at classes)
demo - in class
  launcher, main window (aka transcript)   workspace

select function (editing), operate function (undo, do it, inspect SAVING YOUR IMAGE....file-out-as

#(1 2 'three') "inspect it"

| array1 array3 array2 |
array1 := #(1 2 'three').
array3 := array2 := array1.
array1:= #[6 7 8].
array2:= #[3 4 5].
array1 inspect.
array2 inspect.
array3 inspect.

----------

Application Development

The system browser!

its panes: class categories, classes, protocols, methods,

in Lab - Today, we will do Chapter 6 & 7.
add a new method to an existing class (Number)

diff:aNumber
"return the absolute difference between me and aNumber"
| temp |
temp := self - aNumber.
temp < 0
  ifTrue:[^temp negated]
  ifFalse:[^temp]

diff:aNumber
"return the absolute difference between me and aNumber"
^(self - aNumber) abs

some useful browsers and menu operations.....
  system browser
  class browser
  hierarchy
  implementors of

---------

A small application (Spending)
new category, with classes & methods).

self new initialize
self new setInitialBalace:anAmount
^self new setInitialBalace:anAmount

setInitialBalance: anAmount
cashOnHand := anAmount.
expenditures := Dictionary new.

totalSpentOn: reason
(expenditures includesKey: reason)
  ifTrue:[^expenditures at:reason]
  ifFalse:[0]

spend:anAmount on:reason
expenditures
  at: reason
  put: (self totalSpentOn: reason) + anAmount.
cashOnHand := cashOnHand - anAmount.

printOn:aStream

super printOn:aStream.
aStream space.
aStream nextPutAll: 'balance:'
cashOnHand printOn: aStream.
expenditures keysAndValuesDo: [:reason :amount |
aStream cr.
  reason printOn: aStream.
  aStream tab.
  amount printOn: aStream]

invoking the app:
  mySpendingHistory initialBalance:600
  mySpendingHistory spend:1000 on: 'rent'.

Saving your source code
  file out as ... Spending.st

using the file browser, 'spawning' a new (file) broswer

[ SOS Home Page | Case Study | Year Long Projects | OOP ]


For more information contact
[ Evergreen Home Page | Academic Programs ]


Created by: SoSwEbGrOuP
E-mail: ringert@evergreen.edu