300 likes | 321 Views
ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers. Object-Oriented Design. Coding without a solution design increases debugging time
E N D
ES 314 Advanced Programming Lec 2 • Sept 3 • Goals: • Complete the discussion of problem • Review of C++ • Object-oriented design • Arrays and pointers
Object-Oriented Design • Coding without a solution design increases debugging time • A team of programmers for a large software development project requires • An overall plan • Organization • Communication • Software engineering • Provides techniques to facilitate the development of computer programs
An Examination of Problem Solving • Problem solving • The process of taking the statement of a problem and developing a computer program that solves that problem • Object-oriented analysis and design (OOA / D) • A process for problem solving • A problem solution is a program consisting of a system of interacting classes of objects • Each object has characteristics and behaviors related to the solution • A class is a set of objects having the same type
Abstraction and Information Hiding • Abstraction • Separates the purpose of a module from its implementation • Specifications for each module are written before implementation • Functional abstraction • Separates the purpose of a module from its implementation
Abstraction and Information Hiding • Data abstraction • Focuses on the operations of data, not on the implementation of the operations • Abstract data type (ADT) • A collection of data and a set of operations on the data • You can use an ADT’s operations without knowing their implementations or how data is stored, if you know the operations’ specifications
Abstraction and Information Hiding • Data structure • A construct that you can define within a programming language to store a collection of data • Develop algorithms and ADTs in tandem
Abstraction and Information Hiding • Information hiding • Hide details within a module • Ensure that no other module can tamper with these hidden details • Public view of a module • Described by its specifications • Private view of a module • Implementation details that the specifications should not describe
Principles of Object-Oriented Programming • Object-oriented languages enable us to build classes of objects • A class combines • Attributes (characteristics) of objects of a single type • Typically data • Called data members • Behaviors (operations) • Typically operate on the data • Called methods or member functions
What is a Good Solution? • A solution is good if: • The total cost it incurs over all phases of its life cycle is minimal • The cost of a solution includes: • Computer resources that the program consumes • Difficulties encountered by users • Consequences of a program that does not behave correctly • Programs must be well structured and documented • Efficiency is one aspect of a solution’s cost
Key Issues in Programming • Modularity • Style • Modifiability • Ease of Use • Fail-safe programming • Debugging • Testing
Modularity • Modularity has a favorable impact on • Constructing programs • Debugging programs • Reading programs • Modifying programs • Eliminating redundant code
Style • Use of private data members • Proper use of reference arguments • Proper use of methods • Avoidance of global variables in modules • Error handling • Readability • Documentation
Data Structures – key to software design • Data structures play a key role in every type of software. • Data structure deals with how to store the data internally while solving a problem in order to • Optimize the overall running time of a program • Optimize the response time (for queries) • Optimize the memory requirements • Optimize other resources (e.g. band-width of a network) • Simplify software design • make solution extendible, more robust
Abstract vs. concrete data structures • Abstract data structure (sometimes called ADT -> Abstract Data Type) is a collection of data with a set of operations supported to manipulate the structure • Examples: • stack, queue insert, delete • priority queue insert, deleteMin • Dictionary insert, search, delete • Concrete data structures are the implementations of abstract data structures: • Arrays, linked lists, trees, heaps, hash table • A recurring theme: Find the best mapping between abstract and concrete data structures.
Abstract Data Structure (ADT) • supporting operations • Dictionary • search • insert primary operations • Delete • deleteMin • Range search • Successor secondary operations • Merge • Priority queue • Insert • deleteMin • Merge, split etc. Secondary operations primary operations
Linear data structures • key properties of the (1-d) array: • a sequence of items are stored in consecutive memory locations. • array provides a constant time access to k-th element for any k. • (access the element by: Element[k].) • inserting at the end is easy. • if the current size is S, then we can add x at the end using the single instruction: • Element[S++] = x; • deleting at the end is also easy. • inserting or deleting at any other position is expensive. • Even searching is expensive (unless sorted).
Linked lists Linked lists: Storing a sequence of items in non-consecutive locations of the memory. Not easy to search for a key (even if sorted). Inserting next to a given item is easy. In doubly linked list, inserting before or after a given item is easy. Don’t need to know the number of items in advance. (dynamic memory) order is important
stacks and queues • stacks: • insert and delete at the same end. • equivalently, last element inserted will be the first one to be deleted. • very useful to solve many problems • Processing arithmetic expressions • queues: • insert at one end, deletion at the other end. • equivalently, first element inserted is the first one to be deleted.
Priority queues • insert, deleteMin – main operations • merge, split, etc. – secondary operations • expected performance: • number of operations performed for insert and deletemin – should both be much smaller than n, the number of keys in the queue.
Hashing • dictionary operations • expected performance • constant time on average for each of the operations search, insert and delete.
Arrays and pointers • variable name • variable • value • address – a binary number used by the operating system to identify a memory cell of RAM • It is important to know the precise meanings of these terms
Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 15 x
Memory Terminology (cont.) Addresses Variable name 00110 01010 01110 10010 10110 15 x
Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 15 x A variable (which is a location)
Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 15 x Value of the variable
Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 15 x Address of the variable
Array Example Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size)
Array Example Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size) { if (size == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }
Notion of time complexity int max (int[] A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; } What is the total number of operations performed by the above procedure (as a function of n)? Assignment, comparison, return – each costs one unit.