250 likes | 357 Views
COSC 1030 Section 5. Modularity & Data Abstraction. Objective. Program Language Evolution Modularity Abstract Data Type Priority Queue. Programming Languages. FORTRAN, Algol 60 Function abstraction Subroutine, Function and Procedure Primitive data types Pascal, C Data abstraction
E N D
COSC 1030 Section 5 Modularity & Data Abstraction
Objective • Program Language Evolution • Modularity • Abstract Data Type • Priority Queue
Programming Languages • FORTRAN, Algol 60 • Function abstraction • Subroutine, Function and Procedure • Primitive data types • Pascal, C • Data abstraction • Record type, Structure, Union • Modula, Ada • Modularity • Compilation Unit, Incremental Compilation • Interface, implementation
Programming Languages(2) • Simula 67, Eiffel, C++, • Object Orientation • Abstract Data Types • Component Reuse • Java • Network & Security Awareness • Common Platform • Package level component
Modularity • Programming Component • Compilation Unit • Interface Between Modules • Information Hiding • Separate roles – Implementer & User
Components • Component • File Compilation Unit • Class • Package • Compilation Unit • File Class • Dependent • Hierarchy • Small in Size
Interface between Modules • Clear and Simple • Association and Aggregation – “has” relationship • Class Inheritance – “is a” relationship • Meaningful Naming Convention • Hierarchical • Hollywood Rule – “I call you” • Big Component Knows Small One • Specific Class Knows Generic One
Information Hiding • Prevent Misuse • Minimum Explosion • Keep all instance variables private • Provide Public Getters • Provide Public or Protected Setters Only if Necessary • Eliminate Friends Relationship • Your friend is not my friend even if you are my friend • Break Possible Looping • Use Named Constants (final) • Hide Representation • Meaningful Name • Eliminate Duplicated Objects
Abstract Data Type • User Defined Data Type • Extend primitive data types • Modeling problems and solutions • Data Abstraction + Operations on Data • Data has no meaning without operation • Using Operations to represent a data type • Hide Implementation Detail • Hide data representation • Hide method implementation • ADT as interface • ADT implementations
Natural Number ADT • Zero() : N • Succ(n: N) N • Plus(n: N, m: N) N • Multiple (n: N, m: N) N 0 : N Succ(n) = n+1: N Plus(n, 0) = n Plus(n, succ(m)) = succ(plus(n, m)) Multiple(n, 0) = 0 Multiple(n, succ(m)) = plus(n, mulipe(n, m))
Priority Queue ADT • A finite collection of comparable items for which the following operations are defined • Construct an initially empty priority queue(PQ) • Obtain the size of the PQ • Insert a new Item X, into the PQ • Remove from PQ an item X, of the highest priority from PQ, if PQ is not empty • highest priority item means an item X in PQ such that XY for all Y in the PQ • Comparable Item • Compare to another item X, produces one of the following three status: great than (1), equals to (0) or less than (-1)
Priority Queue Interface public interface PriorityQueue { // PriorityQueue() constructs empty PQ int size(); // number of items in PQ void insert(Comparable x); // puts x into PQ // removes highest priority item from PQ // and returns the highest priority item Comparable remove(); }
Comparable Interface Public interface Comparable { /** * compare to another comparable item * it returns 1 if this item is “great than” another * returns 0 if this is “equals to” another * or returns –1 is this is “less than” another */ int compareTo(Comparable anOtherItem); }
Using Priority Queue ADT • Sort • Put all items into a PQ • Remove from the PQ one by one • Emergency Waiting Room • Patients registered according to time arrival • Patients are seen according to seriousness • Stock Exchange • Put orders based on time • Execute orders based on best matching
ADT Implementation • Different Implementations • Different Vendors • Different Versions • Different and hidden data representations • Parallel Development • Stub Implementation • Develop and test component using stubs • Framework • Common interface • Common patterns • Different plug-ins
JAVA ADT Specification • Interface • Pure ADT, no implementation • Implements interface(s) • Abstract Class • Partially implemented ADT, need plug-ins • Extends Abstract class • Class • Implemented ADT • Refine implementation • Add Additional Operations • Extends class
Java Class Header • Modifier • Public • Abstract • Class <ClassIdentifier> • Extends • One Base Class • Single Inheritance • Implements • One or More Interfaces
Priority Queue Implementations • How to represent data • Sorted Linked List • Maintain order when insert • Return first node when remove • Array • Insert: increase array size if necessary; write to the last position and increase last position • Remove: find the highest priority item; move the last item to the position where highest priority item occupied;
Sorted Linked List Impl. public class PriorityQueueImpl implements PriorityQueue { private LinkedList sortedList = null; public PriorityQueue() { sortedList = new LinkedList(); // empty list } public void insert(Item newItem) { ListNode previousNode = findInsertPositionFor(newItem); sortedList.insertAfter(previousNode, new ListNode(newItem)); } public Item remove() { return sortedList.remove(FIRST); } public size() { return sortedList.size(); } private ListNode findInsertPositionFor(Item anItem) {…} }
Helper Method private ListNode findInsertPositionFor(Item anItem) { ListNode currentNode = sortedList.getFirst(); ListNode previousNode = null; while(currentNode != null) { if(anItem.compareTo(currentNode.getItem() > 0) { break; // found the insert position } else { previousNode = currentNode; currentNode = currentNode.getNext(); } // end if } // end while return previousNode; }
Array Implementation Class PriorityQueueImpl2 implements PriorityQueue { private int count; private final int capacityIncrement; private Item[] itemArray; public PriorityQueueImple2() { count = 0; capacityIncrement = 5; itemArray = new ItemArray[10]; } public int size() { return count; } public void insert(Item newItem) { if(count == itemArray.length) { increaseCapacity(); } itemArray[count++] = newItem; }
Copy Array private void increaseCapacity() { capacity += capacityIncrement; Item[] tempArray = new Item[capacity]; for (int I = 0; I < itemArray.length; I++) { tempArray[I] = itemArray[I]; } itemArray = tempArray; }
remove method public Item remove() { Item maxItem = null; if(count != 0) { int maxPosition = findMaxPosition(); maxItem = itemArray[maxPosition]; itemArray[maxPosition] = itemArray[--count]; itemArray[count] = null; // clean up } // end of if; return maxItem; } // end of remove } // end of PriorityQueueImpl2
maxPosition Method private int maxPosition() { int maxPosition = 0; maxItem = itemArray[0]; for(int I = 0; I < count; I ++) { if(itemArray[I].compareTo(maxItem) > 0) { maxPosition = I; maxItem = itemArray[I]; } // end of if // assert(maxItem == maximum(itemArray[0:I])); } // end of for return maxPosition; } // end of maxPosition
Test Priority Queues Class PriorityQueueTester { public static void main(String[] args) { PriorityQueue aPQ = new PriorityQueueImpl1(); aPQ.insert(“red”); aPQ.insert(“green”); aPQ.insert(“yellow”); aPQ.insert(“blue”); aPQ.insert(“white”); while(aPQ.size() > 0) { York.println(aPQ.remove()); } // end of while } // end of main } // end of PriorityQueueTester