1 es 314 advanced programming lec 2 sept 3 goals: complete the discussion of problem review of c++...

30
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: •Complete the discussion of problem •Review of C++ •Object-oriented design •Arrays and pointers

Post on 22-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

1

ES 314 Advanced Programming Lec 2

Sept 3

Goals:•Complete the discussion of problem

•Review of C++ •Object-oriented design•Arrays and pointers

2

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

3

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

4

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

5

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

6

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

7

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

8

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

9

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

10

Key Issues in Programming

1. Modularity2. Style3. Modifiability4. Ease of Use5. Fail-safe programming6. Debugging7. Testing

11

Modularity

• Modularity has a favorable impact on– Constructing programs– Debugging programs– Reading programs– Modifying programs– Eliminating redundant code

12

Style

1. Use of private data members2. Proper use of reference arguments3. Proper use of methods4. Avoidance of global variables in

modules5. Error handling6. Readability7. Documentation

13

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

14

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.

15

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

16

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

17

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

18

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.

19

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.

20

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

x15

Memory Terminology (cont.)

Addresses

00110

01010

01110

10010

10110

x15

Variable name

Memory Terminology (cont.)

Addresses

00110

01010

01110

10010

10110

x15

A variable (which is a location)

Memory Terminology (cont.)

Addresses

00110

01010

01110

10010

10110

x15

Value of the variable

Memory Terminology (cont.)

Addresses

00110

01010

01110

10010

10110

x15

Address of the variable

27

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)

28

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

29

Implementation using a vector

30

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.