ADT's: Preface

(see also ADT's in brief)

These notes are designed based on object-oriented techniques. Specifically, in object- oriented programming, it is understood that an object instance is sent a message. Thus, when such an object class defines its methods, the instance of the object that is getting the message is assumed to be known (since how else did one get into this class) and thus, it does not need to be passed in the method arguments.

For example, in object-oriented programming, if one had two instances of complex numbers and a programmer wanted to add one to the other, the code might look something like (in Java): compNumInstance1.AddComplex(compNumInstance2) which basically means that compNumInstance1 is being sent a message to add compNumInstance2 to itself.

Another example might be (for an instance of a Stack "stackIns1" and an item x to be put on the stack) stackIns1.Push(x)

Note that in object-oriented programming, instances are always being sent messages. In Java, this looks like instanceName.methodName()

If a method is called within some other method, it will always have this syntax - instanceName.methodName() - except when it is known that the instance being sent the message is the instance that is currently active (this).

Thus, within code, one can often find messages being sent without any instance specified, which indicates, by default, a message with the implicit use of the current instance, i.e. this.

New programmers in OO often forget to put the instanceName in front of method calls. Because of this, I would suggest to any new programmer to OO to not use the default and to explicitly put the this in this.methodName() so that you are always aware of who is getting the message.

This may sound wasteful, but I have found errors in over 50% of the OO labs I grade due to programmers forgetting to explicitly state to what object the message is being sent. I attribute these errors to the closeness of syntax in using the default (when the instance isthis to simply drop the instance name and provide only the method name) to syntax in procedural languages. Specifically, in procedural languages one does not think of an object getting a message; simply of sending procedure calls.. and thus one sees only a "procedure" name.

Succinctly, a good programming practice for new object-oriented programmers is to always use the entire form of instanceName.methodName() for all method calls...even if the instanceName is this.

Reminder:

Precondition: an assertion that must be true in order for the operation to execute correctly precondition

Postcondition: an assertion that prevails upon completion of the operation. A description of the results of the actions performed by the operation. It must be accurately and precisely stated

postcondition

For the following specifications, we will assume that x is the instance to whom messages are being sent.

One Conceptual Module for Complex Numbers

** var means that the stated variable will/can change

An Implementation of ComplexNumberKeeper Another Conceptual Module for Complex Numbers A Conceptual Module for Stacking Queue A Conceptual Module for Lists

Thus a List can conceptually be thought of as two parts: the items that have been traversed and the ones that have not (they "remain"). By specifying these two parts we provide a "marker" so we know where the list is at any time. (Like an index)

Note: This method does not Find an item x, it only Gets an item in the sense that it returns reference to the last item of the items Traversed. It does not really get anything in the sense that it does not take anything out of the List, the List stayed the same. How do I know this?

Conceptual Modules for various ADT's

Problem with "disjointness" We want to be able to use without knowing ADT implementation, but we need the type in order to declare properly (i.e., program that uses module and module must define type.)

(e.x. Say P1 uses modules M1 and M2. M1 declares type complex. Then one must have the type complex defined in P. These do not have to have the same name but must have the same definition. Unfortunately, if we change one definition, we must be sure to change the other)

Want loosely coupled (as little dependencies as possible)

Conceptual module for complex numbers

    • Imagine that there are variables of type complex
    • variables used in calling programs can be declared as type complex
      (computationally the same)
    • Don't imagine these variables hold objects there but x is re, im
    • instead variables hold names of objects x is and points to x.re and x.im
(now, don't have to know how complex numbers are implemented, just how names are)

Advantage:
Won't have undefined objects because one really just has a skeleton if the type complex is present

Disadvantage: