Modularity & Data Abstraction

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Lecture 9 Concepts of Programming Languages
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to Object-oriented programming and software development Lecture 1.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Abstraction ADTs, Information Hiding and Encapsulation.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction: The Walls
Sixth Lecture ArrayList Abstract Class and Interface
Sections Inheritance and Abstract Classes
Abstract Data Types in C
Abstract Data Types and Encapsulation Concepts
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Abstraction A tool (concept) to manage complexity
Chapter 3: Using Methods, Classes, and Objects
Programming Abstractions
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Linked node problem 3 What set of statements turns this picture:
Chapter 12 Sorted Lists and their Implementations
Interface.
Abstract Data Types and Encapsulation Concepts
Chapter 13 Collections.
Circular List removedNode General Case Single Node Case lastNode aNode
Abstract Data Types and Encapsulation Concepts
Programming Abstractions
More Object-Oriented Programming
COSC 1030 Section 8 List.
Queues CSC212.
CHAPTER 11: Priority Queues and Heaps
Introduction to Data Structure
Programming Languages and Paradigms
Lecture 8 Object Oriented Programming (OOP)
Chapter 20 Lists, Stacks, Queues, and Priority Queues
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Modularity & Data Abstraction 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 Compilation Unit 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 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)

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

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 Class <ClassIdentifier> Extends 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; } 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.

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