Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Makeup class: Wednesday May 18 th 4:00PM or 7:00PM?? Makeup class: Wednesday May 18 th 4:00PM or 7:00PM?? Handling XML data in a Java program Handling XML data in a Java program W3C Document Object Model (www.w3.org/DOM/) W3C Document Object Model (www.w3.org/DOM/)www.w3.org/DOM/ A simple way to read an XML file in Java (www.developerfusion.com/show/2064/) A simple way to read an XML file in Java (www.developerfusion.com/show/2064/)www.developerfusion.com/show/2064/ Working with XML (java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html) Working with XML (java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html)java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html  These will be covered in details in the next class

3 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University DTD (Data Type Definitions) DTD (Data Type Definitions) Final Project Assignment Final Project Assignment Chapter 10 – Implementing Subprograms Chapter 10 – Implementing Subprograms Activation Records Activation Records Implementing Subprograms with Stack- Dynamic Local Variables (Dynamic chains) Implementing Subprograms with Stack- Dynamic Local Variables (Dynamic chains) Implementing Recursive Subprograms Implementing Recursive Subprograms Implementing Nested Subprograms (Static chains, Reference to a variable) Implementing Nested Subprograms (Static chains, Reference to a variable) Last Lecture

4 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Implementing Subprograms Implementing Subprograms Implementing Program Blocks Implementing Program Blocks Implementing Dynamic Scoping Implementing Dynamic Scoping Abstraction and Encapsulation Abstraction and Encapsulation Abstract Data Types Abstract Data Types Encapsulation Constructs Encapsulation Constructs Naming Encapsulations Naming Encapsulations

5 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Static Chains to Access Variables program Main_2; var X : integer; var X : integer; procedure Bigsub; procedure Bigsub; var A, B, C : integer; var A, B, C : integer; procedure Sub1; procedure Sub1; var A, D : integer; var A, D : integer; begin begin A := B + C; A := B + C; end; end; procedure Sub2(X : integer); procedure Sub2(X : integer); var B, E : integer; var B, E : integer; procedure Sub3; procedure Sub3; var C, E : integer; var C, E : integer; begin begin Sub1; Sub1; E := B + A: E := B + A: end; end; begin begin Sub3; Sub3; A := D + E; A := D + E; end; end; begin begin Sub2(7); Sub2(7); end; end; begin begin Bigsub; Bigsub; end. end. Ada References to the variable A (0, 3) (2, 3) (1, 3)

6 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Evaluation of the Static Chain Method A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes Display: An alternative to static chains. An array of static links (addresses of accessible activation record instances). Display: An alternative to static chains. An array of static links (addresses of accessible activation record instances).

7 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Program Blocks 2. Allocate locals on top of the activation record instance Must use a different method to access locals Must use a different method to access locals void main () { int x, y, z; while ( … ) { int a, b, c; int a, b, c; … while ( … ) { while ( … ) { int d, e; int d, e; … }} while ( … ) { int f, g; int f, g; …}…} 1. Treat blocks as parameterless subprograms

8 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementing Dynamic Scoping (1) void sub3() { int x, z; x = u + v; …} void sub2() { int w, x; …} void sub1() { int v, w; …} void main() { int v, u; …} Deep Access: Nonlocal references are found by searching the activation record instances on the dynamic chain Length of chain cannot be statically determined at compile time Length of chain cannot be statically determined at compile time Slower execution Slower execution Every activation record instance must have variable names Every activation record instance must have variable names * AW Lecture Notes

9 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementing Dynamic Scoping (2) void sub3() { int x, z; x = u + v; …} void sub2() { int w, x; …} void sub1() { int v, w; …} Void main() { int v, u; …} Shallow Access One stack for each variable name One stack for each variable name Central table that has a location and an “active bit” for each different variable name in a program Central table that has a location and an “active bit” for each different variable name in a program e.g., COBOL * AW Lecture Notes sub3

10 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 11 Abstraction and Encapsulation

11 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstraction Extraction of essential characteristics (data fields and functions) of an object Extraction of essential characteristics (data fields and functions) of an object Not including unimportant details of an object Not including unimportant details of an object Friend - name : String - String - phone: String > Friend(name: String) > Friend(name: String) + getName() : String + setName(name: String) … An abstraction of the real-world object

12 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstraction – cont. Abstraction: A view or representation of an entity that includes only the attributes of significance in a particular context Abstraction: A view or representation of an entity that includes only the attributes of significance in a particular context Implementation details are hidden from users Implementation details are hidden from users Simplify programming process Simplify programming process Types: Data Abstraction, Process Abstraction Types: Data Abstraction, Process Abstraction Stack PushPop Top  Data structure – a list that grows and shrinks at one end only  Operations – push(stk1, ele), pop(stk1), top(stk1), create(stk1), destroy(stk1)

13 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Data Types Abstract Data Type: A user-defined data type that satisfies the following two conditions: Abstract Data Type: A user-defined data type that satisfies the following two conditions: The representation of, and operations on, objects of the type are defined in a single syntactic unit The representation of, and operations on, objects of the type are defined in a single syntactic unit e.g., Stack stk1, stk2; create(stk1); create(stk2); … push(stk1, color1); color2 = pop(stk2); push(stk1, color1); color2 = pop(stk2); The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition Clients of a Data Type: Program units that use a specific abstract data type Clients of a Data Type: Program units that use a specific abstract data type

14 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Example of Abstract Data Types class Stack extends Vector { public Stack() { // create super(); } public Object push(Object item) { addElement(item);} public Object pop() { int top = size() – 1; if (top < 0) return null; else return elementAt(top); return elementAt(top);}… } // Implementation of the stack // data type in Java using a Vector // data type in Java using a Vector Change to use a Linked List The client doesn’t need to be changed A Client Stack stk1 = new Stack(); stk1.push(“1st ele”); stk1.push( “2nd ele”); … String ele = (String)stk1.pop(); (String)stk1.pop();

15 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues for Abstract Data Types Encapsulation: A syntactic unit in which to encapsulate the type definition Encapsulation: A syntactic unit in which to encapsulate the type definition Information Hiding: A method of making type names and subprogram headers visible to clients, while hiding actual definitions Information Hiding: A method of making type names and subprogram headers visible to clients, while hiding actual definitions Some primitive operations must be built into the language processor (usually just assignment and comparisons for equality and inequality) Some primitive operations must be built into the language processor (usually just assignment and comparisons for equality and inequality) Some operations are commonly needed, but must be defined by the type designer Some operations are commonly needed, but must be defined by the type designer e.g., iterators, constructors, destructors * AW Lecture Notes

16 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Data Types in Ada Encapsulation construct: ‘package’ Encapsulation construct: ‘package’ Information hiding via the ‘private’ part Information hiding via the ‘private’ part Specification Package: an interface of an encapsulation (visible to clients) Body Package: implementation (hidden from clients) A client of the package Linked_List_Type package Linked_List_Type is type Node_Type is private; …private type Node_Type; type Node_Type; type Ptr is access Node_Type; type Ptr is access Node_Type; type Node_Type is type Node_Type isrecord Info : Integer; Info : Integer; Link : Ptr; Link : Ptr; end record; end Linked_List_Type; … use Linked_List_Type; procedure Use_Linked_Lists is Node : Node_Type;

17 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Data Types in C++ Data Members Data Members Constructor Constructor Initializes the data members Initializes the data members Implicitly called when an instance is created (Can be explicitly called) Implicitly called when an instance is created (Can be explicitly called) Name is the same as the class name Name is the same as the class name Destructor Destructor Cleanup after an instance is destroyed (e.g., free heap storage) Cleanup after an instance is destroyed (e.g., free heap storage) Implicitly called when the object’s lifetime ends (Can be explicitly called) Implicitly called when the object’s lifetime ends (Can be explicitly called) Name is the class name, preceded by a tilde (~) Name is the class name, preceded by a tilde (~) Member Functions Member Functions class stack { private: int *ptr, max, top; public: stack() { ptr = new int [100]; ptr = new int [100]; max = 99; max = 99; top = -1; top = -1;} ~stack() { delete [] ptr; }; void push(int num) { if (top < max) if (top < max) ptr[++top] = num; }…}

18 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Data Types in C++ Encapsulation by classes Encapsulation by classes Each instance of a class has its own copy of data members Each instance of a class has its own copy of data members Class instances share a single copy of the member functions Class instances share a single copy of the member functions Instances can be static, stack dynamic, or heap dynamic Instances can be static, stack dynamic, or heap dynamic Information Hiding Information Hiding Private clause for hidden entities Private clause for hidden entities Public clause for interface entities Public clause for interface entities Client part Creates an instance Access a member function class stack { private: int *ptr, max, top; public: stack() { ptr = new int [100]; ptr = new int [100]; max = 99; max = 99; top = -1; top = -1;} ~stack() { delete [] ptr; }; void push(int num) { if (top < max) if (top < max) ptr[++top] = num; }…} void main() { stack stk; stk.push(42); …

19 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Data Types in C# Accessor methods (getters and setters): allow indirect access to hidden data members Accessor methods (getters and setters): allow indirect access to hidden data members C# provides properties as a way of implementing getters and setters without requiring explicit method calls C# provides properties as a way of implementing getters and setters without requiring explicit method calls public class Weather { public int DegreeDays { get { get { return degreeDays; } set { set { degreeDays = value; }} private int degreeDays;...}... Weather w = new Weather(); int todaysDegree, oldDegree; … w.DegreeDays = todaysDegree; oldDegree = w.DegreeDays;

20 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameterized Abstract Data Types Making the stack class usable for any data type and arbitrary maximum size Making the stack class usable for any data type and arbitrary maximum size Making the class a templated class Making the class a templated class Using parameterized constructor functions Using parameterized constructor functions template template class stack { private: int *ptr, max, top; public: stack(int size) { ptr = new Type [size]; ptr = new Type [size]; max = size - 1; max = size - 1; top = -1; top = -1;} ~stack() { delete [] ptr; }; void push(int num) { if (top < max) if (top < max) ptr[++top] = num; }…} C++

21 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Encapsulation & Information Hiding In Java or C++, both data members and member functions are defined in a class In Java or C++, both data members and member functions are defined in a class An interface hides the implementation details of data structures and functions of an object An interface hides the implementation details of data structures and functions of an object Information hiding ensures the data integrity by preventing programmers from accessing data incorrectly Information hiding ensures the data integrity by preventing programmers from accessing data incorrectly Friend > Friend(name: String) > Friend(name: String) + getName() : String + setName(name: String) + get () : String + set ( String) … An interface to access the object public class Friend { private String name, , phone; public Friend ( String name ) { this.name = name; } public String getName() { return name; } public viod setName(String name) { this.name = name; }…}

22 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Encapsulation Constructs Motivation – Large programs with thousands of lines have two special needs: Motivation – Large programs with thousands of lines have two special needs: 1. Some means of organization to keep them intellectually manageable 2. Some means of partial compilation (compilation units that are smaller than the whole program) Solution – Encapsulation: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) Solution – Encapsulation: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) e.g., Java packages, C library & header files e.g., Java packages, C library & header files * AW Lecture Notes

23 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Java Packages

24 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Naming Encapsulations (1) Large programs define many global names; need a way to divide into logical groupings Large programs define many global names; need a way to divide into logical groupings A naming encapsulation is used to create a new scope for names A naming encapsulation is used to create a new scope for names C++, C# Namespaces – One can place each library in its own namespace and qualify names used outside with the namespace C++, C# Namespaces – One can place each library in its own namespace and qualify names used outside with the namespace namespace Stack1 {namespace Stack2 { int top; … int top; … int top; … int top; … }}}}}}}} Stack1::top = Stack2::top; * AW Lecture Notes

25 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Naming Encapsulations (2) Java Packages Java Packages Packages can contain more than one class definition; classes in a package are partial friends Packages can contain more than one class definition; classes in a package are partial friends Clients use fully qualified names (e.g., java.io.File) or use the ‘import’ declaration (e.g., import java.io.*;) Clients use fully qualified names (e.g., java.io.File) or use the ‘import’ declaration (e.g., import java.io.*;) Ada Packages Ada Packages Packages are defined in hierarchies which correspond to file hierarchies Packages are defined in hierarchies which correspond to file hierarchies Visibility from a program unit is gained with the ‘with’ clause (e.g., with Ada.Text_IO;) Visibility from a program unit is gained with the ‘with’ clause (e.g., with Ada.Text_IO;) To access names without qualification, the ‘use’ clause can be used (e.g., use Ada.Text_IO) To access names without qualification, the ‘use’ clause can be used (e.g., use Ada.Text_IO) * AW Lecture Notes


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google