CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington ©Chengkai.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
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.
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 
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
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.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Tutorial 10 Programming with JavaScript
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
JavaScript, Third Edition
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
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.
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.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Object-Oriented Programming Lecture 13 – OO Programming, Spring CSE3302 Programming Languages,
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
JavaScript Syntax, how to use it in a HTML document
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Squeak and Botkit-Port Jeff Forbes Joel Miller. Introduction ● Squeak is a pure OO language ● Based off of SmallTalk-80 – Designed at Xerox PARC by Alan.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
1 JavaScript in Context. Server-Side Programming.
Python Let’s get started!.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New 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.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Feedback from Assignment 1
Smalltalk – a Pure (and first) OOPL
Building Java Programs
Chapter 2 Squeak: Object-oriented design with multimedia applications
Building Java Programs
Lecture 2 Python Programming & Data Types
CS 331 Principles of Programming Languages
Introduction to Smalltalk
February , 2009 CSE 113 B.
Tutorial 10: Programming with javascript
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Readings: Smalltalk by Example, Chapter 1-6 (Chapter 7-9 are also recommended, especially sections related to Array) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

The language is together with its interactive runtime system Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Smalltalk Runtime Runtime written in the language itself Can change the system on-the-fly Debug system state (image), object, class hierarchy Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Squeak Demo The files (.exe.changes,.img,.sources) Menu and Mouse (red-button (usually left button), yellow-button (usually right button), blue-button(Alt+red, “morphic halo”) ) Save changes into.img and.sources Transcript: system console, log Workspace: Run code snippet, text documents, … do it, print it, inspect it, explore it System Browser (Class, Object, Instance variable, Method) Hierarchy Browser Method Finder Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Race Car Based on Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Quinto Game Squeak by Example, Chapter 2 Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Quinto Game 1.Create new class category: SBE-Quinto 2.Define new class: SBECell Object subclass: #NameOfSubclass instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: ‘SBE-Quinto‘ SimpleSwitchMorph subclass: #SBECell instanceVariableNames: 'mouseAction' classVariableNames: '' poolDictionaries: '' category: ‘SBE-Quinto' Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Message: class compiled

Demo: Quinto Game 3. Add methods: initialize initialize super initialize. self label: ''. self borderWidth: 2. bounds := corner: offColor := Color paleYellow. onColor := Color paleBlue darker. self useSquareCorners. self turnOff Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, newCell := SBECell new. Message initialize will be automatically sent to the newly created object when the class has an initialize method. newCell initialize.

Demo: Quinto Game 4. Inspect an object SBECell new (inspect it) self openInWorld (do it) 5. Add new class: SBEGame BorderedMorph subclass: #SBEGame instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'SBE--Quinto' Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Quinto Game 6. Add methods: SBEGame>>initialize initialize | sampleCell width height n | super initialize. n := self cellsPerSide. sampleCell := SBECell new. width := sampleCell width. height := sampleCell height. self bounds: extent: + (2 * self borderWidth)). cells := Matrix new: n tabulate: [ :i :j | self newCellAt: i at: j ]. 7. Put methods into category (protocol) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Quinto Game 8. Two more methods for SBEGame SBEGame»cellsPerSide "The number of cells along each side of the game" ^10 SBEGame»toggleNeighboursOfCellAt: i at: j (i > 1) ifTrue: [ (cells at: i -- 1 at: j ) toggleState]. (i < self cellsPerSide) ifTrue: [ (cells at: i + 1 at: j) toggleState]. (j > 1) ifTrue: [ (cells at: i at: j -- 1) toggleState]. (j < self cellsPerSide) ifTrue: [ (cells at: i at: j + 1) toggleState]. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, For annotation only, not the language syntax.

Demo: Quinto Game 9. Two more methods for SBECell SBECell»mouseAction: aBlock mouseAction := aBlock SBECell»mouseUp: anEvent mouseAction value 10. Try and debug SBEGame>>initialize … ^cells Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Demo: Quinto Game File Out and File In Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Everything is object. Objects communicate by messages. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Object Hierarchy Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Object UndefinedObjectBoolean Fraction False Magnitude Char Number … True IntegerFloat Collection Set …

No Data Type. There is only Class. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Smalltalk Syntax is Simple. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Syntax Smalltalk is really “small” – Only 6 keywords (pseudo variables) – Class, object, variable, method names are self explanatory – Only syntax for calling method (messages) and defining method. No syntax for control structure No syntax for creating class Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Expressions Literals Pseudo Variables Variables Assignments Blocks Messages Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Literals Number: Character: $a String: ‘ ’ ( ‘Hel’,’lo!’ and ‘Hello!’ are two objects) Symbol: # (#foo and #foo are the same object) Compile-time (literal) array: #(1 $a ‘abc’) Run-time (dynamic) array: {1. $a. SBEGame new } Comment: “This is a comment.” Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Pseudo Variables true : singleton instance of True false : singleton instance of Falsee nil : singleton instance of UndefinedObject self : the object itself super : the object itself (but using the selector defined for the superclass) thisContext : activation of method. (inspect the state of system) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Variables Instance variables. Local variables (method, blocks) | sampleCell width height n | Arguments (method argument, block argument) SBEGame»toggleNeighboursOfCellAt: i at: j [ :i :j | self newCellAt: i at: j ] Shared Variables: – Global variables, e.g., Transcript – Class variables, e.g., Epsilon in Float Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Conventions Class name, class variable, global variable: (Capital letter for the first character of every word) Table HashTable Local variables, arguments, instance variable: (Capital letter for the first character of every word, except the first word) sampleCell Object (instance of a class, especially arguments) aTable aHashTable Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Assignments bounds := corner: or bounds _ corner: Assignment returns value, which is the object to the left of :=. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Defining a Method selector (method name) | local variable | statement (expression). (. Is used to end a statement) statement(expression). ^ return-value (^ returns value from a method) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

objectselector (raiseTo:) message Methods and Messages Method Name: Selector Method Invocation: Message – Unary selector 3 factorial – Keyword selector 3 raisedTo: 2 ‘Programming Language’ indexOf: $a startingAt: 3 Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, objectselector message object selector ( indexOf:startingAt: ) message

Keyword Selector: more readable table insert: anItem at: anIndex table insert: 3 at: 5 vs. table.insert(anItem, anIndex) table.insert(3,5) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

objectselector parameter Binary selector ? aTable / 3 (what it means depends on the class) 1+2*3( * does not have higher precedence than -, because they are messages that can be sent to any object. No mathematical meaning is assumed.) Examples: – Integer>>#+ – Complex>>#+ – Fraction>>#+ 3/5 (1/3) + (1/2) Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Binary selector + - * / = (equality) ~= >= < == (identity, the two objects are the same unique object), ~~ & | Boolean, (string concatenation) ‘Hel’,’lo’ = ‘Hello’ ‘Hel’,’lo’ == ‘Hello’ #Hello == #Hello Assignment := is not a method Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Expression Associativity for Binary selector : left to right Precedence rules: Unary selector, then Binary selector, then Keyword selector 1+2/4 2 raisedTo: factorial ( ) for changing the order of evaluation “-object” was not there originally. So “ " generated syntax errors in previous versions. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Message Cascading i.e., Sequence Operator Transcript cr. Transcript show: 'hello world'. Transcript cr Transcript cr; show: 'hello world‘; cr Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

A block is an anonymous function. Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Block Evaluate a block: value The evaluation result is the object from the last statement. [ 1+2 ] value [ 1+2.‘abc’, ‘def’] value [1+2. SBEGame new] value Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Block Parameters [:x :y | x+y ] value:2 value:3 [ :x :y | | z | z := x+ y. z:=z*z. z ] value: 2 value: 3 Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Block Closure Block can access variables declared in enclosing scope. | x | x := 1. [ :y | x + y ] value: 2 [ :y | self x: + y ] value: 2 Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Block is Object! z := [:x :y | x+y ]. z value:2 value:3 Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

“Control Structures” by Messages Conditions: Messages to Boolean objects, with blocks as arguments class True (subclass of Boolean, False is similar) Selectors: – ifTrue: alternativeBlock ^ alternativeBlock value – ifFalse: alternativeBlock ^nil – ifTrue:ifFalse: – ifFalse:ifTrue: Example – (a < b) ifTrue: [max:=b] ifFalse: [max:=a] Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

“Control Structures” by Messages While Loops : Messages to blocks Example – n := 1. [ n < 10 ] whileTrue: [ n := n*2 ] Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

“Control Structures” by Messages Counting Loops : Blocks as both message receivers and parameters Example – n := timesRepeat: [ n := n*2 ] – n := 1. 1 to: 10 do: [ n := n*2 ] – n := 0. 1 to: 10 do: [ :i | n := n + i ] – n := 0. 1 to: 10 by: 2 do: [ :i | n := n + i ] – n := to: 1 by: -2 do: [ :i | n := n + i ] Let’s see how Number>>to:do: is implemented Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,