COSC 1030 Section 5

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

description

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 - PowerPoint PPT Presentation

Transcript of COSC 1030 Section 5

Page 1: COSC 1030 Section 5

COSC 1030 Section 5COSC 1030 Section 5

Modularity & Data Abstraction

Page 2: COSC 1030 Section 5

ObjectiveObjective

Program Language EvolutionModularityAbstract Data TypePriority Queue

Page 3: COSC 1030 Section 5

Programming LanguagesProgramming 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

Page 4: COSC 1030 Section 5

Programming Languages(2)Programming Languages(2)

Simula 67, Eiffel, C++, – Object Orientation– Abstract Data Types– Component Reuse

Java– Network & Security Awareness– Common Platform– Package level component

Page 5: COSC 1030 Section 5

ModularityModularity

Programming ComponentCompilation UnitInterface Between ModulesInformation HidingSeparate roles – Implementer & User

Page 6: COSC 1030 Section 5

ComponentsComponents

Component– File Compilation Unit– Class– Package

Compilation Unit– File Class– Dependent– Hierarchy – Small in Size

Page 7: COSC 1030 Section 5

Interface between ModulesInterface 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

Page 8: COSC 1030 Section 5

Information HidingInformation 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

Page 9: COSC 1030 Section 5

Abstract Data TypeAbstract 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

Page 10: COSC 1030 Section 5

Natural Number ADTNatural Number ADT

Zero() : NSucc(n: N) NPlus(n: N, m: N) NMultiple (n: N, m: N) N

0 : NSucc(n) = n+1: NPlus(n, 0) = nPlus(n, succ(m)) = succ(plus(n, m))Multiple(n, 0) = 0Multiple(n, succ(m)) = plus(n, mulipe(n, m))

Page 11: COSC 1030 Section 5

Priority Queue ADTPriority 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)

Page 12: COSC 1030 Section 5

Priority Queue InterfacePriority 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();

}

Page 13: COSC 1030 Section 5

Comparable InterfaceComparable 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);

}

Page 14: COSC 1030 Section 5

Using Priority Queue ADTUsing 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

Page 15: COSC 1030 Section 5

ADT ImplementationADT 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

Page 16: COSC 1030 Section 5

JAVA ADT SpecificationJAVA 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

Page 17: COSC 1030 Section 5

Java Class HeaderJava Class Header

Modifier– Public– Abstract

Class <ClassIdentifier> Extends

– One Base Class– Single Inheritance

Implements– One or More Interfaces

Page 18: COSC 1030 Section 5

Priority Queue Priority Queue ImplementationsImplementations

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;

Page 19: COSC 1030 Section 5

Sorted Linked List Impl.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) {…}}

Page 20: COSC 1030 Section 5

Helper Method 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;}

Page 21: COSC 1030 Section 5

Array ImplementationArray 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; }

Page 22: COSC 1030 Section 5

Copy ArrayCopy 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;

}

Page 23: COSC 1030 Section 5

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

remove methodremove method

Page 24: COSC 1030 Section 5

maxPosition MethodmaxPosition 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

Page 25: COSC 1030 Section 5

Test Priority QueuesTest Priority QueuesClass 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