ADTs by pengxiuhui

VIEWS: 16 PAGES: 19

									      Thought for the Day
“In this world it is not what we take up,
but what we give up, that makes us rich.”
– Henry Ward Beecher
Abstraction in Modern Computer
           Languages
• Procedural Abstraction
  – Methods allow us to use and to create abstract
    procedures or processes

     objStream.writeObject(obj);

      calculateTax(employee[k]);
        Abstract Data Types
         Data Abstraction
• View data in an abstract way
         Abstract Data Types
          Data Abstraction
• View data in an abstract way

• Formal definition:
 An Abstract Data Type is a pair <V, O> where:
      V is a set of values
      O is a set of operations defined on V

• Abbreviate: ADT
        Primitive Data Types
• Types such as int, double, etc. are “built
  in” ADTs

• For int in Java:
 V = { -2 147 483 648 ... 2 147 483 647 }
 O = { +, -, *, /, %, ~, |, &, ^, <<, >>,
        >>>, <, <=, ==, >=, >, !=, = }
     New Abstract Data Types
• Many programming languages allow us to
  create our own new ADTs
• Java: class
• Example:
  – Frequency table class
         V = { “bins” with frequency counts }
         O = { addEntry, numHits }
  Client and Implementor Views
• ADTs allow us to hide details of the
  implementation from “clients”
  – Vital for abstraction

• In Java: private

• Try to keep separate:
  – implementor/programmer view
  – client view
             Client Views
• In the notes, distinguished by grey
  background

• Also use simplified UML class diagrams:

                FrequencyTable
         bin, lb, n, binSize
         FrequencyTable
         addEntry, numHits
  Interfaces and Data Abstraction
 • Sometimes a client will use an object
   defined by an interface

 • It does not need to know anything else
   about the type of the actual object
   – Hidden implementation detail
void delegate (ManagerialResponsibilities m)
  { ... m.manage(); ...
  } // delegate
          Closing Comment
• Geoff Rehmet (Technical Business
  Manager, Internet Solutions):

…the single most powerful construct I learned
[at Rhodes] is the concept of abstraction.
… [it] gives me an infinite lead over my peers
         Section 2
  Advanced Data Structures
Vectors and Lists
Stacks and Queues
Trees and Graphs
Dictionaries and Hash Tables
   Chapter 4: Vectors and Lists
• Objectives
  – To consider simple lists of information

  – To study the implementation of simple lists:
     • using arrays
     • using linked lists

  – To see how polymorphism and generics can be
    used with ADTs in Java
                           Lists
• Many problems involve the use of lists
  –   Lists of courses
  –   Lists of students
  –   Lists of guests
  –   Lists of groceries
  –   Lists of marks           A list of lists!
                       Lists
• In this chapter we consider:

  – A list of integers, using an array

  – A list of integers, using a linked list
                                              Values
  – A generic list, using polymorphism

  – A generic list, using Java 5 generics
           Operations on Lists
• Basic set of operations:
  –   Creating an empty list
  –   Adding an element
  –   Accessing an element
  –   Removing an element
  –   Finding an element
  –   Finding the number of elements
  –   Displaying all the elements
  The IntegerVector Class
• Vector
  – Technical/mathematical term for a list


• Class diagram (incomplete):
                   IntegerVector
       data, numElements
       add, get, set, position,
       remove, length
  The IntegerVector Class
       public class IntegerVector
         { private int[] data;
           private int numElements;
           ...
         } // class IntegerVector


• Data members are all private
  – A very common pattern for ADTs
  – Data is accessed (and protected) by public
    methods
 IntegerVector: Constructors
public IntegerVector (int initSize)
  { if (initSize <= 0)
      throw new IllegalArgumentException(…);
    numElements = 0;
    data = new int[initSize];
  } // constructor

public IntegerVector ()
  { this(100);
  } // constructor

								
To top