3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
S.Ducasse Stéphane Ducasse 1 Numbers.
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 
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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 
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
ASP.NET Programming with C# and SQL Server First Edition
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.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
String Escape Sequences
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright Curt Hill Variables What are they? Why do we need them?
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
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.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Stéphane Ducasse 1 Abstract Classes.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Java Primer 1: Types, Classes and Operators
The Selection Structure
Haskell.
Java Programming: From Problem Analysis to Program Design, 4e
C++ for Engineers and Scientists Second Edition
Smalltalk – a Pure (and first) OOPL
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4: Control Structures I (Selection)
Introduction to Smalltalk
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes Running Little Smalltalk A Small Example Squeak Essentials

3C-2Identifiers Variable names  Local variable: begin with a lower case letter (convention).  newName := 17 Assignment  Shared variable: begin with an upper case letter (convention).  TranscriptGlobal variable Class names  Begin with a capital letter (convention).  Integer Pseudo-variables: reserved names  self Current object  super Superclass  true Instance of True  false Instance of False  nil Instance of UndefinedObject  thisContext the top frame of the run-time stack  Method arguments  Block arguments

3C-3 Kinds of Variables Global Variables: live forever.  X := 2 Local Variables: defined in blocks/methods Live as long as needed. Often, obey call stack semantics.  Temporary Variables  Actual Parameters  Example: findSkill: aSkill | empsWithSkill | empsWithSkill := Set new. … ^empsWithSkill. Instance Variables: defined in a class definition. One copy per class instance. Live as long as the instance (also a variable) lives. Object subclass: #Color instanceVariableNames: 'rgb cachedDepth …' classVariableNames: '…'

3C-4 Kinds of Variables – Cont. Class Variables: Shared among all the instances of a class. Live as long as the class lives. Object subclass: #Color instanceVariableNames: '…' classVariableNames: '…ColorNames…' Pool Variables: Shared between several classes that may not be related by inheritance. Not a good practice – try to avoid them ArrayedCollection subclass: #Text instanceVariableNames: 'string runs' classVariableNames: '' poolDictionaries: 'TextConstants' category: 'Collections--Text'

3C-5 Self and Super self indicates the current object itself, for  re-sending  return super indicates the sub-object, i.e., the object corresponding to the super class. Used for  Refining an overridden method: Example: suppose that class Manager inherits from class Employee. Then, every manager has an employee part in it: Super Self

3C-6 Literal Objects Integer  7 Uniquely identified by its name, need not be declared Float  Char  $A$9$$ String Subclass of ArrayedCollection  'a string' Array  #(this is an array)  #(12 'abc' (another one))  { } Symbol  #aSymbol

3C-7Numbers May be Integer ( 1, -3 ), Float ( 0.25, 3.5e-2 ) or Fraction ( 1/4, -3/5 ) Recognise arithmetic messages: + - * / Recognise comparison messages: = = ~= Quotient and remainder: quo: rem: Bitwise logical operations: bitAnd: bitInvert: bitOr: bitXor: bitShift: Other messages: positive, negative, strictlyPositive, squared, sqrt, lcm:, gcd:, abs, truncated, rounded, ceiling, floor...

3C-8Characters Characters are written with a preceding dollar sign. Recognise comparison messages: = = ~= asInteger returns the numeric character code corresponding to the receiver. asString returns a string of length 1 corresponding to the receiver. isAlphaNumeric, isDigit, isLowercase, isUppercase return true if the receiver satisfies the condition, and false otherwise.

3C-9Strings Are sequences of characters enclosed by single apostrophes. Recognise comparison messages: = = ~= Concatenation:, Change: at:put: Substring: copyFrom:to:

3C-10 Summary: Basic Objects In Smalltalk, several objects may be uniquely identified by literals. Some examples are: Number : e-2 Character: $a $& $$ String : 'Hello, world!' '9 o''clock' Array : #('cat' 'dog' 'cow') #( (1 0) (0 1) ) Boolean : true false Block : [ :name | name print ] [ i := i + 1 ] Other basic objects may be obtained by sending messages to these literals: Fraction : Returned by the message / to an Integer. Examples 1/5 -2/3 Interval : Returned by the message to:by: to a Number. Example 1 to:5 by:2 returns Interval( )

3C-11Blocks A block has the general form: [arguments | statements]  Enclosed in surrounding square braces.  Encapsulates a sequence of Smalltalk statements.  [ Transcript show:'hello’]  Several statements are separated by dots.  [ i := i + 1. Transcript show: i]  Executes only when received a message value.  [ i := i + 1. Transcript show: i] value  May have arguments.  [ :x :y | Transcript show:(x + y)] value: 2 value: 3  Arguments are local. Methods are actually blocks.

3C-12 Blocks as First Class Citizens May be stored in variables May be returned from procedures Example  Twice := [:x | 2 * x]  Twice value: 10  20  Twice value: 5  10 Executes in the context in which it was defined. b := [ i <- i + 1. Transcript show: i]... b value The identifier i above refers to the binding known at the time the block was defined. Capitalized Twice is acceptable, since it is a global variable.

3C-13 Control Structures Blocks are useful for several control structures:  anInteger timesRepeat: aBlock  anArray do: aBlock  anInterval do: aBlock  aBoolean ifTrue: aTrueBlock ifFalse: aFalseBlock  aBlock whileTrue: anotherBlock Examples:  5 timesRepeat: [Transcript show: 'Hello, world!‘; cr]  #( ) do: [ :grade | sum := sum + grade ]  ( 0 to: 10 by: 2 ) do: [ :i | Transcript show: i squared; cr]  0 positive ifTrue: [Transcript show:'0 is positive‘; cr]  [ count <= max ] whileTrue: [Transcript show: count;cr. count := count+1]

3C-14 Passing Messages All actions are produced by passing messages.  A message is a request for an object to perform some operation.  A message can contain certain argument values. The behaviour of an object in response to a specific message is dictated by the object ’ s class.  An object is an instance of a larger class (category) of objects. The list of statements that define how an instance of some class will respond to a message is called the method for that message.

3C-15 Message Types Messages have the general form: receiver selector arguments Unary messages  Formed by a single word, requires no arguments. 7 sign Yields 1 7 factorial sqrt Evaluated from left to right: sqrt(7!) Binary messages  Formed from one or two adjacent nonalphabetic characters, a single argument is required Yields * 3 Yields sqrt Unary message has a higher precedence Keyword messages  Consists of one or more keywords (an identifier followed by a colon), each keyword requires an argument. 7 max: 14 7 between: 2 sqrt and: 4 + 2

3C-16 Messages ’ Precedence Unary messages have precedence over binary messages, that have precedence over keyword messages. > 4 squared + 1 negated gcd: 3 factorial 3 Parentheses may be used to change precedence. > ( 4 squared + 1 negated gcd: 3 ) factorial 6 Keyword messages can not be combined. > 100 quo: 5 rem: 3 ERROR: SmallInteger does not understand message #quo:rem: Keyword messages can be separated by parentheses. > ( 100 quo: 5 ) rem: 3 2

3C-17Cascades The character ; may be used to send several messages to the same object. The receiver of all cascaded messages is the receiver of the first message involved in a cascade The value returned by a cascade of messages is the value of the last cascaded message Examples: > 2+2; * 1000; squared; factorial; sqrt > ( 'Smalltalk' copyFrom:1 to:5 ) size 5 > 'Smalltalk' copyFrom:1 to:5; size 9 > 'Smalltalk' at:4 put:$r; at:5 put:$t;asString Smarttalk  Why wasn ’ t the answer Smarltalk ?!?

3C-18 Defining the Class Point2D * File point2D.st Object subclass: #Point2D instanceVariableNames: 'xCoord yCoord’ classVariableNames: ‘’ poolDictionaries: ‘’ category: 'Point'! initialize xCoord := 0. yCoord := 0 ! x: newX xCoord := newX ! y: newY yCoord := newY ! x: newX y: newY self x: newX. self y: newY !

3C-19 The Class Point2D (cont.) x ^xCoord ! y ^yCoord ! ! distanceTo: aPoint ^ ( ( xCoord - aPoint x ) squared + ( yCoord - aPoint y ) squared ) sqrt ! ! printString ^'( ', xCoord printString, ', ', yCoord printString, ' )'

3C-20 Using the Class Point2D > (Point2D methodDict keys) do: [:x | Transcript show: x asString; show: ‘ ’] distanceTo: initialize y x: printString x:y: y: x > origin := Point2D new ( 0, 0 ) > aPoint := Point2D new. > aPoint x: 3; y: 4 ( 3, 4 ) > aPoint x + aPoint y 7 > aPoint distanceTo: origin 5.0

3C-21 Manipulating a Point p := Point new  Assigns to p an object of class Point p x: 3 y: 4  Sends the point object p a keyword message x:y: p x  Sends the point object p an unary message x  The object returns as a result the object 3 p rho  Sends the point object p an unary message rho  The object returns as a result the object 5  Oops, but rho is not defined yet...

3C-22 Adding Methods to Class Point2D rho: dist theta: angle xCoord := dist * angle cos. yCoord := dist * angle sin ! rho ^ (xCoord squared + yCoord squared) sqrt ! theta ^ (yCoord / xCoord) arcTan

3C-23 Summary of Main Points Objects  Everything is an object.  Each object is an instance of a class.  Instance variables are private. Messages  All actions are produced by passing messages.  A message activates a method. Methods  A method has a signature (selector & arguments) that defines how it is to be used.  ^exp returns the value of exp (an object) as the result.