Download presentation
Presentation is loading. Please wait.
Published byRebecca Johnston Modified over 9 years ago
1
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
2
SJSU Dept. of Computer Science Spring 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. _
3
SJSU Dept. of Computer Science Spring 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. _
4
SJSU Dept. of Computer Science Spring 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. _
5
SJSU Dept. of Computer Science Spring 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 _
6
SJSU Dept. of Computer Science Spring 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. _
7
SJSU Dept. of Computer Science Spring 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 _
8
SJSU Dept. of Computer Science Spring 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
9
SJSU Dept. of Computer Science Spring 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. _
10
SJSU Dept. of Computer Science Spring 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. _
11
SJSU Dept. of Computer Science Spring 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. _
12
SJSU Dept. of Computer Science Spring 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. _
13
SJSU Dept. of Computer Science Spring 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.
14
SJSU Dept. of Computer Science Spring 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.
15
SJSU Dept. of Computer Science Spring 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/ http://www.sjsu.edu/at/atn/webcasting/archives/fall_2011/hist/Computing/ Scroll down to “Invention of the Dynabook”, Alan Kay Alan Kay and a Dynabook prototype.
16
SJSU Dept. of Computer Science Spring 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.
17
SJSU Dept. of Computer Science Spring 2014: March 10 CS 152: Programming Language Paradigms © R. Mak 17 Smalltalk IDE Download Pharo open-source Smalltalk: http://www.pharo-project.org/homehttp://www.pharo-project.org/home
18
SJSU Dept. of Computer Science Spring 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). _
19
SJSU Dept. of Computer Science Spring 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]
20
SJSU Dept. of Computer Science Spring 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 ]
21
SJSU Dept. of Computer Science Spring 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 ]
22
SJSU Dept. of Computer Science Spring 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-DC8chttp://www.youtube.com/watch?v=aGlt2G-DC8c
23
SJSU Dept. of Computer Science Spring 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
24
SJSU Dept. of Computer Science Spring 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 -> 3 1 -> 2 3 -> 2 1 -> 3 2 -> 1 2 -> 3 1 -> 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
25
SJSU Dept. of Computer Science Spring 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 Java when 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
26
SJSU Dept. of Computer Science Spring 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); }
27
SJSU Dept. of Computer Science Spring 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
28
SJSU Dept. of Computer Science Spring 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. _
29
SJSU Dept. of Computer Science Spring 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. _
30
SJSU Dept. of Computer Science Spring 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"
31
SJSU Dept. of Computer Science Spring 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"
32
SJSU Dept. of Computer Science Spring 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 argument and 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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.