Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes.

Slides:



Advertisements
Similar presentations
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
Advertisements

Introduction to Programming in C++ John Galletly.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Principles of Object-Oriented Software Development The language Smalltalk.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse8.1 Syntax and Messages The syntax of Smalltalk is simple and uniform, but it can look strange at first sight! Literals: numbers, strings,
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Some Basic Points on Classes
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
Stéphane Ducasse 1 Syntax and Messages.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Introduction to LabVIEW Seth Price Department of Chemical Engineering New Mexico Tech Rev. 10/5/14.
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Intro to the Squeak environment. Getting Squeak Read instructions for working in labs linked from course web pages. –Save your Squeak.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Stéphane Ducasse 1 Singleton.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Stéphane Ducasse 1 Some Advanced Points on Classes.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Smalltalk Interpreted by Alan Frazier & Brian Sparks.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
Written by: Dr. JJ Shepherd
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.
OOP Basics Classes & Methods (c) IDMS/SQL News
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
Let’s Play Objects.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Procedural Abstraction Object-Oriented Code
Java Programming with BlueJ
Chapter 2 Squeak: Object-oriented design with multimedia applications
Introduction to Smalltalk
Let’s Talk about… Smalltalk.
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes –the environment

Stéphane Ducasse4.2 You do not have to know everything!!! “Try not to care - Beginning Smalltalk programmers often have trouble because they think they need to understand all the details of how a thing works before they can use it. This means it takes quite a while before they can master Transcript show: ‘Hello World’. One of the great leaps in OO is to be able to answer the question "How does this work?" with "I don’t care"“. Alan Knight. Smalltalk Guru => We will show you how to learn and find your way

Stéphane Ducasse4.3 Power & Simplicity: The Syntax on a PostCard exampleWithNumber: x “A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and key word messages, declares arguments and temporaries (but not block temporaries), accesses a global variable (but not and instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variable true false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks. It doesn’t do anything useful, though” |y| true & false not & (nil isNil) ifFalse: [self halt]. y := self size + super size. #($a #a ‘a’ 1 1.0) do: [:each | Transcript show: (each class name); show: (each printString); show: ‘ ‘]. ^ x < y

Stéphane Ducasse4.4 Some Conventions Return Values > 4 Node new -> aNode Method selector #add: Method scope conventions Instance Method defined in class Node: Node>>accept: aPacket Class Method defined in class Node (in the class of the the class Node) Node class>>withName: aSymbol aSomething is an instance of the class Something

Stéphane Ducasse4.5 DoIt, PrintIt, InspectIt and Accept Accept = Compile: Accept a method or a class definition DoIt = send a message to an object PrintIt = send a message to an object + print the result (#printOn:) InspectIt = send a message to an object + inspect the result (#inspect)

Stéphane Ducasse4.6 Hello World Transcript show: ‘hello world’ At anytime we can dynamically ask the system to evaluate an expression. To evaluate an expression, select it and with the middle mouse button apply doIt. Transcript is a special object that is a kind of standard output. It refers to a TextCollector instance associated with the launcher.

Stéphane Ducasse4.7 Transcript show: ‘hello world’

Stéphane Ducasse4.8 Everything is an Object –The workspace is an object. –The window is an object: it is an instance of SystemWindow. –The text editor is an object: it is an instance of ParagraphEditor. –The scrollbars are objects too. –‘hello word’ is an object: it is aString instance of String. –#show: is a Symbol that is also an object. –The mouse is an object. –The parser is an object: instance of Parser. –The compiler is also an object: instance of Compiler. –The process scheduler is also an object. –The garbage collector is an object: instance of ObjectMemory. –Smalltalk is a consistent, uniform world written in itself. You can learn how it is implemented, you can extend it or even modify it. All the code is available and readable

Stéphane Ducasse4.9 Objects communicate via Messages Transcript show: ‘hello world’ The above expression is a message –the object Transcript is the receiver of the message –the selector of the message is #show: –one argument: a string ‘hello world’ –Transcript is a global variable (starts with an uppercase letter) that refers to the Launcher’s report part. Vocabulary Concerns: Message passing or sending a message is equivalent to –invoking a method in Java or C++ –calling a procedure in procedural languages –applying a function in functional languages –of course the last two points must be considered under the light of polymorphism

Stéphane Ducasse4.10 Objects communicate via Messages (II) Message: –receiver : 1 (an instance of SmallInteger) –selector: #+ –arguments: 2 Message: lpr nextNode: macNode –receiver lpr (an instance of LanPrinter) –selector: #nextNode: –arguments: macNode (an instance of Workstation)

Stéphane Ducasse4.11 Objects Communicate by Messages (III) Message: Packet send: 'This packet travelled to' to: #lpr –receiver: Packet (a class) –selector: #send:to: –arguments: 'This packet travelled to' and #lpr Message: Workstation withName: #mac –receiver: Workstation (a class) –selector: #withName: –arguments: #mac

Stéphane Ducasse4.12 Tomagoshi Small entity –Its own night and day cycle –Eating, sleeping, been hungry, been satisfied –Changing color to indicate its mood

Stéphane Ducasse4.13 Tomagoshi woken up sleeping satisfied hungry fallAsleep wakeUp night isSatisfied eat day isHungry

Stéphane Ducasse4.14 Instantiating… To create a tomagoshi: Tomagoshi newStandAlone openInWorld

Stéphane Ducasse4.15 How to Define a Class Fill the template: NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1' classVariableNames: 'ClassVarName1 ClassVarName2' poolDictionaries: '' category: ’TOMA'

Stéphane Ducasse4.16 Tomagoshi For example to create the class Tomagoshi Morph subclass: #Tomagoshi instanceVariableNames: ‘tummy hunger dayCount isNight' classVariableNames: '' poolDictionaries: '' category: ’TOMA'

Stéphane Ducasse4.17 Class Comment! I represent a tomagoshi. A small virtual animal that have its own life. dayCount represents the number of hour (or tick) in my day and night. isNight represents the fact that this is the night. tummy represents the number of times you feed me by clicking on me. hunger represents my appetite power. I will be hungry if you do not feed me enough, but I'm selfish so as soon as I' satisfied I fall asleep because I do not have a lot to say.

Stéphane Ducasse4.18 How to define a method? message selector and argument names "comment stating purpose of message" | temporary variable names | statements

Stéphane Ducasse4.19 Initializing Tomagoshi>>initializeToStandAlone “Initialize the internal state of a newly created tomagoshi” super initializeToStandAlone. tummy := 0. hunger := 2 atRandom + 1. self dayStart. self wakeUp

Stéphane Ducasse4.20 dayStart Tomagoshi>>dayStart night := false. dayCount := 10

Stéphane Ducasse4.21 Step step “This method is called by the system at regurlar time interval. It defines the tomagoshi behavior.” self timePass. self isHungry ifTrue: [self color: Color red]. self isSatisfied ifTrue: [self color: Color blue. self fallAsleep]. self isNight ifTrue: [self color: Color black. self fallAsleep]

Stéphane Ducasse4.22 Time Pass Tomagoshi>>timePass "Manage the night and day alternance" Smalltalk beep. dayCount := dayCount -1. dayCount isZero ifTrue:[ self nightOrDayEnd. dayCount := 10]. self digest Tomagoshi>>nightOrDayEnd "alternate night and day" night := night not

Stéphane Ducasse4.23 Digest Tomagoshi>>digest "Digest slowly: every two cycle, remove one from the tummy” (dayCount isDivisibleBy: 2) ifTrue: [ tummy := tummy -1]

Stéphane Ducasse4.24 Testing Tomagoshi>>isHungry ^ hunger > tummy Tomagoshi>>isSatisfied ^self isHungry not Tomagoshi>>isNight ^ night

Stéphane Ducasse4.25 State Tomagoshi>>wakeUp self color: Color green. state := self wakeUpState Tomagoshi>>wakeUpState "Return how we codify the fact that I sleep" ^ #sleep Tomagoshi>> isSleeping ^ state = self wakeUpState

Stéphane Ducasse4.26 Eating Tomagoshi>>eat tummy := tummy + 1

Stéphane Ducasse4.27 Time and Events Tomagoshi>>stepTime "The step method is executed every steppingTime ms " ^ 500 Tomagoshi>>handlesMouseDown: evt "true means that the morph can react when the mouse down over it" ^ true Tomagoshi>>mouseDown: evt self eat