cs 152: programming language paradigms march 10 class meeting department of computer science san...

32
CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu /~mak

Upload: rebecca-johnston

Post on 31-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

CS 152: Programming Language Paradigms

March 10 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

2

Programming Paradigms

Different programming paradigms (cultures).

Functional Lisp, Scheme, ML, Haskell, F#

Logic Prolog

Object-oriented C++, C#, Objective C, Java, etc.

_

Page 3: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

3

Object-Oriented Programming

The language SIMULA 67 formally introduced the concept of object-oriented programming in 1967. It extended Algol 60 to make it suitable for performing

computer simulations of real-word objects. Norwegian Computing Center, Oslo, Norway

Ole-Johan Dahl and Kristen Nygaard A central goal was to incorporate the notion of an object.

Classes, instances, subclasses, virtual methods, coroutines, discrete event simulation.

Alan Kay at Xerox PARC introduced the term object-oriented programming in the 1970s. Invented the Smalltalk language which borrowed from SIMULA.

_

Page 4: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

4

Object-Oriented Programming, cont’d

An object has properties that control its ability to react to events in predefined ways. Just like objects in the real world.

Object-oriented programming A program consists of a set of objects. The objects can vary dynamically. They act upon and react to other objects. Just like objects in the real world.

Mid 1980’s: Interest in object-oriented programming exploded._

Page 5: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

5

Object-Oriented Programming, cont’d

Bjarne Stroustrup at Bell Labs starts to develop C with Classes 1983: Renamed C++

Early to mid 1990s OOP used to implement graphical user interfaces (GUIs).

Objective C

Recent history OOP features added to existing languages

such as Ada, FORTRAN, COBOL, Pascal. James Gosling at Sun Microsystems releases Java in 1995. More new languages: C#, Python, Ruby, Visual BASIC

_

Page 6: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

6

Software Reuse and Independence

Object-oriented programming languages satisfy three important needs in software design: Need to reuse software components as much as possible. Need to modify program behavior with minimal changes

to existing code. Encapsulate parts of the program whose design will change.

Need to maintain the independence of different components. Loose coupling between cooperating components. Each component only depends on the other component’s

public interface.

Abstract data type mechanisms can increase the independence of software components. Separate interfaces from implementations.

_

Page 7: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

7

Software Reuse and Independence, cont’d

Four basic ways a software component can be modified for reuse:

Extension of the data or operations Redefinition of one or more of the operations Abstraction Polymorphism

_

Page 8: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

8

Extension of the Data or Operations

Extend an existing object to give it more capabilities.

Example: Add new methods to a queue. A “standard” queue can add elements only to its tail and

remove elements only from its head. Extend a queue to allow elements to be removed from its tail

and new elements to be added to its head. double-ended queue (AKA deque)

Example: Add capabilities to a “standard” window to allow it to display text. In addition to the standard move and resize operations, add:

line wrapping functionality scrollbars to scroll text

Page 9: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

9

Redefinition of Operations

Redefine existing operations of an object to accommodate new behavior.

Example: A text window redefines the basic window display operations in order to display text._

Page 10: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

10

Abstraction

Collect similar operations from different components into a new component.

Example: A circle and a rectangle have common properties: Position that can be moved. Ability to display on the screen.

Combine these properties into a abstract figure object. Specific figures such as circle, rectangle, triangle, etc.

must then share the common properties._

Page 11: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

11

Polymorphism

Extend a set of operations over different object types. The objects share a common interface

that defines the operations. A operation may be performed on different objects but it will

behave differently according to the type of the object.

Example: Display a figure object. The display operation behaves differently depending on

whether the object is a circle, rectangle, triangle, etc._

Page 12: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

12

Encapsulation and Loose Coupling

An object can restrict access by other objects to its internal implementation. Information hiding. This allows the object’s implementation to change

without affecting other objects. As long as the object maintains the same public interface.

Loosely coupled objects that have minimal dependencies on each other. This plus encapsulation allows for flexible code design. Changes to one set of objects do not ripple out to other

objects._

Page 13: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

13

Software Frameworks Object-oriented programming supports

component-based programming. A program uses components (objects) from a

software framework. Components all have well-defined public interfaces.

The framework defines how the components work together to achieve an overall common goal. The components can be extended, redefined, etc. The components can be polymorphic.

Example: The Java Foundation Classes (JFC) More commonly known as “Swing”. A software framework for GUI programming. Components include windows, menus, buttons, etc.

Page 14: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

14

Smalltalk

Pioneering object-oriented programming language. Just about everything is an object.

Objects communicate by “sending messages” to each other. Messages are requests for service.

Ideas from Alan Kay in the early 1970s. Learning Research Group at Xerox PARC. Early implementation by Dan Ingalls.

Used to program the Dynabook. A “prehistoric” laptop or tablet.

Page 15: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

15

Alan Kay and Dynabook

Watch video: http://www.sjsu.edu/at/atn/webcasting/archives/fall_2011/hist/Computing/ Scroll down to “Invention of the Dynabook”, Alan Kay

Alan Kay and aDynabook prototype.

Page 16: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

16

Smalltalk, cont’d

An early integrated development environment (IDE) Highly integrated set of development tools Graphical user interface (GUI) with overlapping windows.

Smalltalk can be said to be purely object-oriented. Includes garbage collection and dynamic typing. Includes a windowing system with menus and a mouse,

long before this became common for PCs.

An interactive and dynamically oriented language. Classes and objects are created by interaction with the system,

using a set of browser windows. Contains a large hierarchy of preexisting classes. Program by adding new classes to the hierarchy.

Today’s Eclipse uses tiled windows.

Page 17: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

17

Smalltalk IDE

Download Pharo open-source Smalltalk: http://www.pharo-project.org/home

Page 18: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

18

Smalltalk: Everything is an Object

Java: a = b + c; Smalltalk: a b + c.

Invoke the + method of object b passing argument c Assign the result to a

Java: x = max(y, z); Smalltalk: x y max: z.

Java: b = between(x, y, z); Smalltalk: b x between: y and: z.

Java: a[i+1] = a[i]; Smalltalk: a at: i+1 put: (a at: i).

_

Page 19: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

19

Smalltalk: Everything is an Object, cont’d

Java:

Smalltalk:

if (atEnd(stream)) { reset(stream);}else { c = next(stream);}

stream atEnd ifTrue: [stream reset] ifFalse: [c stream next]

Page 20: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

20

Smalltalk: Everything is an Object, cont’d

Java:

Smalltalk:

while (i < 10) { sum = sum + a[i]; i = i + 1;}

[i < 10] whileTrue: [ sum sum + (a at: i). i i + 1 ]

Page 21: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

21

Smalltalk: Everything is an Object, cont’d

Java:

Smalltalk:

for (i = 1; i <= 10; i++) { a[i] = 0;}

1 to: 10 by: 1 do: [ :i | a at: i put: 0 ]

Page 22: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

22

Towers of Hanoi

Start: All the disks are on one pin from smallest to largest. Goal: Move all the disks from one pin to another,

using the third pin for temporary storage. The disks must end up in order from smallest to largest.

Rule: A larger disk can never be on top of a smaller disk.

Youtube video: http://www.youtube.com/watch?v=aGlt2G-DC8c

Page 23: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

23

Towers of Hanoi: Java

public class Hanoi1 { /* Recursive procedure to move the disk at a height from one pin to another pin using a third pin. */ private void moveTower(int height, int fromPin, int toPin, int usingPin) { if (height > 0) { moveTower(height-1, fromPin, usingPin, toPin); moveDisk(fromPin, toPin); moveTower(height-1, usingPin, toPin, fromPin); } } /* Move a disk from a pin to another pin. Print the results in the console window. */ private void moveDisk(int fromPin, int toPin) { System.out.println(fromPin + " -> " + toPin); }

...}

Hanoi1.java

Page 24: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

24

Towers of Hanoi: Java, cont’d

public class Hanoi1 { ... /* Main: Create the object and invoke the moveTower method. */ public static void main(String args[]) { (new Hanoi1()).moveTower(3, 1, 3, 2); }}

1 -> 31 -> 23 -> 21 -> 32 -> 12 -> 31 -> 3

moveTower(height-1, fromPin, usingPin, toPin);moveDisk(fromPin, toPin);moveTower(height-1, usingPin, toPin, fromPin);

We are invoking the methods of which object?

Hanoi1.java

Page 25: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

25

Towers of Hanoi: Java, cont’d

The Hanoi1 object invokes its own methods.

We usually leave off this in Javawhen an object invokes its own methods._

/* Recursive procedure to move the disk at a height from one pin to another pin using a third pin. */private void moveTower(int height, int fromPin, int toPin, int usingPin){ if (height > 0) { this.moveTower(height-1, fromPin, usingPin, toPin); this.moveDisk(fromPin, toPin); this.moveTower(height-1, usingPin, toPin, fromPin); }}

Hanoi2.java

Page 26: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

26

Towers of Hanoi: Smalltalk Solution

moveTower: height from: fromPin to: toPin using: usingPin "Recusive procedure to move the disk at a height from one pin to another pin using a third pin"

(height > 0) ifTrue: [ self moveTower: (height-1) from: fromPin to: usingPin using: toPin. self moveDisk: fromPin to: toPin. self moveTower: (height-1) from: usingPin to: toPin using: fromPin ]

/* Recursive procedure to move the disk at a height from one pin to another pin using a third pin. */private void moveTower(int height, int fromPin, int toPin, int usingPin){ if (height > 0) { this.moveTower(height-1, fromPin, usingPin, toPin); this.moveDisk(fromPin, toPin); this.moveTower(height-1, usingPin, toPin, fromPin); }}

Page 27: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

27

Towers of Hanoi: Smalltalk Solution

moveDisk: fromPin to: toPin "Move a disk from a pin to another pin. Print the results in the transcript window"

Transcript show: (fromPin printString, ' -> ', toPin printString). Transcript cr.

Demo

/* Move a disk from a pin to another pin. Print the results in the console window. */private void moveDisk(int fromPin, int toPin){ System.out.println(fromPin + " -> " + toPin);}

(Object new) moveTower: 3 from: 1 to: 3 using: 2

Page 28: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

28

Smalltalk Objects and Messages

Every object in Smalltalk has properties and behaviors.

In a Smalltalk program, an object gets another object to do things by sending it messages.

Message: A request for service. Selector: The message name.

Message passing: Sending and receiving messages.

Sender: The originator of the message. May supply data in the form of parameters or arguments.

Receiver: An object that receives a message._

Page 29: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

29

Smalltalk Objects and Messages, cont’d

Mutator: A message that results in a change of state in the receiver object.

Interface: The set of messages that an object recognizes.

Method: How an object performs a service._

Page 30: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

30

Smalltalk Syntax

The object that receives the message is written first, followed by the message name and any arguments.

Example: Create a new Set object:

Set new "Returns a new Set object"

Page 31: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

31

Smalltalk Messages

The size message returns the number of elements in a set.

You can send multiple messages. Example:

The Set class receives the new message. It returns an instance of Set. Which receives the size message and returns an integer.

Class message: A message sent to a class. Instance message: A message sent to an

instance of a class. In the above example, new is a class message,

while size is an instance message.

Set new size "Returns 0"

Page 32: CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: March 10

CS 152: Programming Language Paradigms© R. Mak

32

Smalltalk Messages, cont’d

Unary message: A message with no arguments. Keyword message: A message that expect arguments.

The message selector (method name) ends in a colon. Example:

includes is a boolean method that has as an element argumentand returns whether or not the element is in the set.

Does the new set include the element 'Hello' ?

If a message has more than one argument, a keyword and colon must precede each argument. Example:

at:put: is the complete message selector.

Set new includes: 'Hello'

a at: i+1 put: (a at: i).