Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modularity & Data Abstraction

Similar presentations


Presentation on theme: "Modularity & Data Abstraction"— Presentation transcript:

1 Modularity & Data Abstraction
COSC 1030 Section 5 Modularity & Data Abstraction

2 Objective Program Language Evolution Modularity Abstract Data Type
Priority Queue

3 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

4 Programming Languages(2)
Simula 67, Eiffel, C++, Object Orientation Abstract Data Types Component Reuse Java Network & Security Awareness Common Platform Package level component

5 Modularity Programming Component Compilation Unit
Interface Between Modules Information Hiding Separate roles – Implementer & User

6 Components Component Compilation Unit File  Compilation Unit Class
Package Compilation Unit File  Class Dependent Hierarchy Small in Size

7 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

8 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

9 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

10 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))

11 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 X  Y 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)

12 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(); }

13 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); }

14 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

15 ADT Implementation Different Implementations Parallel Development
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

16 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

17 Java Class Header Modifier Class <ClassIdentifier> Extends
Public Abstract Class <ClassIdentifier> Extends One Base Class Single Inheritance Implements One or More Interfaces

18 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;

19 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) {…}

20 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; } Different cases: 1. Found insert position, break. 1.1 previous node is null, insert at first. 1.2 previous node is not null, insert after 2. While loop ends when current node is null. 2.1 empty list, previous and current are null. 2.2 previous point to the last node of the list.

21 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;

22 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;

23 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

24 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

25 Test Priority Queues aPQ.insert(“red”); 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


Download ppt "Modularity & Data Abstraction"

Similar presentations


Ads by Google