Smalltalk – a Pure (and first) OOPL

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming Languages and Paradigms The C Programming Language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
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.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
4. Python - Basic Operators
Forth A stack language.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
2440: 211 Interactive Web Programming Expressions & Operators.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Visual Basic Programming
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.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Sorts, CompareTo Method and Strings
Information and Computer Sciences University of Hawaii, Manoa
Definition of the Programming Language CPRL
User-Written Functions
Java Language Basics.
Chapter 2 Basic Computation
Introduction to Python
GC211Data Structure Lecture2 Sara Alhajjam.
Variables, Expressions, and IO
JavaScript Syntax and Semantics
Introduction to C++ October 2, 2017.
Fundamental of Java Programming Basics of Java Programming
Programming Language Concepts (CIS 635)
Arrays, For loop While loop Do while loop
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Arithmetic operations, decisions and looping
Feedback from Assignment 1
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Java - Data Types, Variables, and Arrays
Introduction to Java, and DrJava part 1
T. Jumana Abu Shmais – AOU - Riyadh
Programming We have seen various examples of programming languages
Differences between Java and C
Python Primer 1: Types and Operators
Introduction to Java, and DrJava
Fundamentals of Functional Programming
Introduction to Smalltalk
CISC101 Reminders All assignments are now posted.
Introduction to Primitives
COMPUTER PROGRAMMING SKILLS
In this class, we will cover:
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Introduction to Smalltalk
CSE 3302 Programming Languages
Class code for pythonroom.com cchsp2cs
Smalltalk on a Dime.
Presentation transcript:

Smalltalk – a Pure (and first) OOPL Smalltalk was developed by Alan Kay as part of his phd dissertation It was further developed by AI researchers at Xerox PARC to construct a GUI-based operating system as well as a graphics language called Turtle Graphics although today Smalltalk has a number of variants, we will limit ourselves to a more recent version called Squeak (an open source product) Pure OOPL – everything is an object all data are pointed to by references all communication through message passing messages come in a few forms that make the language look more like non-pure OOPLs that is, message passing doesn’t look like message passing in languages like Java

Classes and Methods Examine the definitions for and define classes and methods through the Browser window, there are four top panes that represent (shown on the next slide) categories (name space) classes within the selected category class methods (methods that operate on the class as a whole) instance methods (methods that operate on instances of the class) Selecting any class shows the class definition in the bottom window selecting a category gives you a blank class definition that you can fill in to define a new class: define at least its name and instanceVariableNames (instance data) Selecting any method shows the method’s definition in the bottom window selecting a class and clicking in the blank space under the list of methods gives you a blank method to define a new one

Viewing a Class Here, we are viewing the Integer class and looking specifically at how <= is implemented

Defining a Class and Method Here, I have created a new category called MyNameSpace and added a class called MyName (I was going to call it PersonName but had already created a class by that name earlier) To this class, I added four methods as shown on the right, the first method’s implementation is shown in the bottom pane

Messages: Binary Called binary because these methods operate on two objects x < 5 pass to x the message < 5 to test to see if the value stored in x is less than 5, returns true or false x == z pass the message “equal to” z to x, returns true or false a + 1 pass the message “add” 1 to a, returns the value of a + 1 (a does not change) b * c + d / e several messages passed to b in a left to right manner so this is actually the same as (((b * c) + d) / e), / is used for float division x // y integer division (use \\ for mod)

Messages: Continued Other binary messages include <, >, <=, >=, ==, ~= (not equal), ~~ (not identical), eqv: (equivalent) not, and: (or &), or: (or |), xor: & and | are short-circuited, and: and or: are not when using not, the not comes after the object as in x not (instead of not x because the not message is being passed to x) Some binary messages are of the form object message: object as in x quo: y. (integer divide) x rem: y. (integer remainder) x gcd: y. (greatest common denominator) x roundTo: 2. (round x to 2 places, also truncateTo:) x log: base. (compute log base x) x max: y. (return the maximum between the two values) -5 is a unary message (literally passes “negate” to 5)

Messages: Multiple Parameters For other messages, use the notation object messagename[: parameters]. as in x increment: 5. pass to variable x the message increment: 5 (presumably to add 5 to the value stored in object x) object message: firstparam keyword2: secondparam … . here, the parameters are denoted by special keywords Messages can be chained together in which case they operate left to right x abs sqrt factorial. is the same as sqrt(|x|)!

Example Message Passing 3 factorial + 4 factorial between: 10 and: 100. this computes 10 <= 3! + 4! <= 100 Specifically, it works as follows: 3 gets the factorial message, returns 6 4 gets the factorial message and returns 24 6 (from 3 factorial) gets the message + 24 and returns 30 30 gets the message between: 10 and: 100 and returns true Consider: (3 factorial + 4) factorial between: 10 and: 100. 3 factorial + 4 = 10, 10 gets factorial and computes 10!, which is not between 10 and 100 so this returns false All messages return a value of some kind (although the return value doesn’t have to be used) All messages end with a period

Local Variables and Assignments To declare variables inside a method, use | … | example: | x y z | variables are untyped (they are all references to objects) Assign variables values using one of :=, <- or _ x := 0. y <- x * 5 + 3. z := y raisedTo: 3. strings are available using ‘’ individual characters are denoted as $char as in $a or $* (which would be ‘*’) LISP-like symbols are available using $ as in $apple $banana

Strings and Characters Strings are placed between single quotes (not double quotes) string messages include isEmpty size at: index (return the character at the given index, indices start at 1, not 0) copyFrom: index to: index indexOf: $char indexOf: $char ifAbsent: [block]. String new: size. (as in String new: 10.) at: index put: character Characters follow $ symbols as in $a for ‘a’ character messages include isLowercase, isUppercase, isLetter, isVowel, isDigit, isAlphaNumeric, isSeparator, asLowercase, asUppercase, asString, asciiValue

Other Useful Message between: … and: isKindOf: className. as in x between: 5 and: 10. returns true if x stores a numeric value between 5 and 10 isKindOf: className. as in x isKindOf: Number. or name isKindOf: String. isMemberOf: ClassName. respondsTo: messageName. here, we ask if the given message is available for the object example: x respondsTo: sqrt. Others: isNil, positive, strictlyPositive (> 0), even, odd, isLiteral, isInteger, isFloat, isNumber, isUpperCase, isLowerCase

Blocks Blocks of code are placed inside [ ] If there are parameters passed to the block, use [ :params | block code ]. the block returns the last value computed in the block, like LISP Example: [:x | x + 1]. receives a parameter (x) and returns the value + 1 Blocks are typically used in control statements expr ifTrue: [then clause] ifFalse: [else clause] expr ifTrue: [ expr2 ifTrue: [ inner then clause] ifFalse: [inner else clause]] ifFalse: [ expr3 ifTrue: [ inner then clause2 ] ifFalse: [ inner else clause2]]. expr whileTrue: [ while loop body ]. there is also a whileFalse: expr timesRepeat: [ for loop body ].

Input and Output Output is performed through the Transcript object (a window) Pass messages to Transcript Transcript clear. clear the window, also cr (\n) and tab (\t) Transcript show: item output item where item is a variable or a literal string as in ‘hello world’ ‘string’ printOn: Transcript alternate way to print to Transcript window To get input, use an assignment statement and the FillInTheBlank message as in x := FillInTheBlank request: ‘Enter your age’

Arrays Create an array through var := Array new: size. Assign array elements using array at: put: as in x at: i put: y. (same as x[i] = y) Access array elements using at: without put: as in y := y + x at: i. Initializing an array is done with successive with: parameters var := Array with: value with: value with: value … You can iterate through the array elements using var do: [ :index | block of code ] For instance, to output the elements of an array do x do: [ :i | Transcript cr; show i: ].

Other Array Messages includes: value as in copyFrom: to: as in x includes: 3. (returns true or false) copyFrom: to: as in y := x copyFrom: 2 to: 8. (copies x[2] through x[8] into y) occurrencesOf: value (counts number of occurrences of value) select: [ a block that contains a test ] y := x select: [:a | a > 0]. copies all elements of x into y which are > 0 reject: [ block ] – opposite of select collect: [ block ] – returns elements of the array after being processed by the block y := x collect [:a | a + 1]. y will be all elements of x after being incremented by 1

Continued inject: object into: [ block ] this performs a computation on the array based on instructions in the block examples: sum := x inject: 0 into: [:a :b | a + b]. Sums up the values in x max := x inject: 0 into: [:a :b | (a > b) ifTrue: [a]. ifFalse: [c]]. max gets the max value in array x shuffled – randomizes elements in the array asSortedCollection – sorts array asOrderedCollection – same but creates an ordered collection (a flex array) asSet – converts array to set

More Types There are bitwise methods available bitAnd:/bitOr:/bitXor: (followed by value to be ANDED/ORED/XORED to) bitShift: (followed by number of bits to shift, negative for right shift) Dictionary using key/value pairs denoted using #key->value example: myDictionary := Dictionary new. myDictionary add: #a->1; add: #b->12; add: #c->5; add: #d- >10; yourself. has many methods such as keyAtValue: ifAbsent:, removeKey: ifAbsent:, includes:, includesKey:, occurrencesOf: value, select, reject, inject, as Array, asOrderedCollection, assortedCollection Date and Time classes Point class (Cartesian point) Drawing classes