Docsity

CS3723 Homework 8: Data Abstraction and Object-Orientation, Assignments of Programming Languages

Toggle sidebar
bg1
cs3723 Homework #8,Due: 2pm, 4/17/08
Note: If you collaborated with your classmates or used their notes, please note which classmates you collab-
orated with. If you use an external source, besides the text book, lectures, notes provided by the instructor,
and your own intellect, please cite that source. Use quote marks if you are quoting material word-for-word
from any source (including the text book).
Data Abstraction (Chapter 9)
1. (a) What is the oldest abstraction mechanism provided by programming
langauges? And what does it encapsulate?
Procedures, which encapsulate code.
(b) List three features of data abstraction mechanisms provided by lan-
gauges.
i. ā€œidentifying the interface of the data structureā€
ii. ā€œproviding information hidingā€
iii. ā€œallowing the data structure to be used in different ways by differ-
ent programsā€
2. (a) What is the difference between an interface and a specification?
An interface is the externally visible parts (e.g., operation names, types,
parameters) through which a client interacts with a component. There
is often explicit language support for defining the interfaces and en-
suring that the client and component implementation agree on it (e.g.,
public methods/fields in Java and declarations in header files in C). The
specification is a description of the behavior of a component as visible
through its interface. If the implementation does not satisfy the spec-
ification, then the client may not work correctly. But there is usually
no automatic mechanism to determine if the implementation satisfies
the specification. (This is one of the primary goals of unit testing.)
(b) In ML’s abstype construct only the abstract type name and the names
and types of the values/functions declared in the abstype are externally
visible. What is the externally visible interface of:
exception Empty;
ab s t y p e qu e u e = Q o f i n t li s t
wi t h
fu n mk _ Qu e u e () = Q ( ni l )
an d is _ e mp t y ( Q ( l )) = l= ni l
an d a d d (x , Q ( l ) ) = Q ( l@ [ x ] )
an d f i r s t ( Q ( n il )) = r a i se E mpt y
| f ir s t ( Q ( x :: l ) ) = x
an d r e st ( Q( n i l )) = ra i se E m pt y
| r es t ( Q ( x :: l ) ) = Q ( l )
an d l e ng t h ( Q ( ni l ) ) = 0
| l en g t h ( Q ( x :: l ) ) = 1 + l e n gt h ( Q ( l ))
en d ;
1
pf3
pf4

Partial preview of the text

Download CS3723 Homework 8: Data Abstraction and Object-Orientation and more Assignments Programming Languages in PDF only on Docsity!

cs3723 Homework # 8 , Due: 2 pm, 4 / 17 / 08

Note: If you collaborated with your classmates or used their notes, please note which classmates you collab- orated with. If you use an external source, besides the text book, lectures, notes provided by the instructor, and your own intellect, please cite that source. Use quote marks if you are quoting material word-for-word from any source (including the text book).

Data Abstraction (Chapter 9 )

  1. (a) What is the oldest abstraction mechanism provided by programming langauges? And what does it encapsulate? Procedures, which encapsulate code. (b) List three features of data abstraction mechanisms provided by lan- gauges. i. ā€œidentifying the interface of the data structureā€ ii. ā€œproviding information hidingā€ iii. ā€œallowing the data structure to be used in different ways by differ- ent programsā€

  2. (a) What is the difference between an interface and a specification? An interface is the externally visible parts (e.g., operation names, types, parameters) through which a client interacts with a component. There is often explicit language support for defining the interfaces and en- suring that the client and component implementation agree on it (e.g., public methods/fields in Java and declarations in header files in C). The specification is a description of the behavior of a component as visible through its interface. If the implementation does not satisfy the spec- ification, then the client may not work correctly. But there is usually no automatic mechanism to determine if the implementation satisfies the specification. (This is one of the primary goals of unit testing.) (b) In ML’s abstype construct only the abstract type name and the names and types of the values/functions declared in the abstype are externally visible. What is the externally visible interface of: exception Empty ; abstype queue = Q of int list with fun mk_Queue () = Q ( nil ) and is_empty ( Q ( l )) = l = nil and add (x , Q ( l )) = Q ( l@ [ x ]) and first ( Q ( nil )) = raise Empty | first ( Q ( x :: l )) = x and rest ( Q ( nil )) = raise Empty | rest ( Q ( x :: l )) = Q ( l ) and length ( Q ( nil )) = 0 | length ( Q ( x :: l )) = 1 + length ( Q ( l )) end ;

e x c e p t i o n Empty type queue val mk Queue = f n : u n i t āˆ’> queue val is empty = f n : queue āˆ’> bool val add = f n : i n t (^) * queue āˆ’> queue val f i r s t = f n : queue āˆ’> i n t val r e s t = f n : queue āˆ’> queue val l e n g t h = f n : queue āˆ’> i n t

Overview of Object-Orientation (Chapter 10 )

  1. (a) List the four steps of Grady Booch’s approach to object-oriented design. i. ā€œIdentify the objects at a given level of abstraction.ā€ ii. ā€œIdentify the semantics (intended behavior) of these objects.ā€ iii. ā€œIdentify the relationship among the objects.ā€ iv. ā€œImplement the objects.ā€ (b) List the four main language concepts for object-oriented programming. i. dynamic lookup ii. abstraction iii. subtyping iv. inheritance

  2. (a) What is dynamic lookup (a.k.a., dynamic dispatch)? It is a mechanism that allows objects to respond in different ways to the same ā€˜message’ based on the dynamic identity of the receiving object. (b) How does dynamic lookup (e.g., a method invocation in Java) differ from an ordinary function call (e.g., in C)? In an ordinary function call, the code that is being jumped to is de- termined by the name of the function (and perhaps overloading based on the type of the function’s parameter) or by an explicit function val- ue/pointer. With dynamic lookup, there is an implicit mechanism that determines the method implementation based on the dynamic identity of the receiving object. (c) What is the key word for declaring a member function with dynamic lookup in C++? virtual

  3. (a) What do object-based abstraction and data abstraction both encapsulate (or as Mitchell says ā€˜combine’)? functions (code) and data

  1. It is possible to mimic many aspects of objects in ML using records and closures. What is it about object-oriented programs cannot be mimicked using records and closures? subtyping and inheritance

  2. (a) What is a design pattern?

ā€œA design pattern is a guideline or approach to solving a kind of prob- lem that occurs in a number of specific forms.ā€ (b) List two design patterns. The two described in the textbook are Singleton and Facade. In addi- tion, the Visitor design pattern was described in lecture and in lab. (c) How does the organization of a typical object-oriented program dif- fer from an equivalent program with a conventional function-oriented organization? A program with a conventional function-oriented organization, will be divided up into functions to handle different logical operations. Each function may be further subdivided into separate cases for han- dling different types of data. The code related to each data type will be spread across several functions. In contrast, in a typical object- oriented program, the data type is the primary organization criteria and the operation is secondary to that. So the different code related to a single logical operation will be spread across the different classes in the class hierarchy. (d) What design pattern allows a program to use subtyping and dynamic lookup to achieve an organization/effect similar to the type case used on p. 288 in your text book? (The answer to this question is not in the text book.) The Visitor Design Pattern describes a way of arranging objects so that one achieves a operation-oriented organization for code using object- oriented mechanisms (primarily subtyping and dynamic lookup).