Data Abstraction and Encapsulation

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

Linear Lists – Array Representation
Arrays 2008, Fall Pusan National University Ki-Joune Li.
5.4 Basis And Dimension.
Chapter Matrices Matrix Arithmetic
Array pair C++ array requires the index set to be a set of consecutive integers starting at 0 C++ does not check an array index to ensure that it belongs.
Chapter 4.
Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Intro to Matrices Don’t be scared….
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CS Data Structures Chapter 2 Arrays and Structures.
MATRICES. Matrices A matrix is a rectangular array of objects (usually numbers) arranged in m horizontal rows and n vertical columns. A matrix with m.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Java Unit 9: Arrays Declaring and Processing Arrays.
ECON 1150 Matrix Operations Special Matrices
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Systems of Linear Equation and Matrices
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
Chapter 2 1. Arrays Array: a set of index and value Data structure For each index, there is a value associated with that index. Eg. int list[5]: list[0],
Advance Data Structure Review of Chapter 2 張啟中. Review of Chapter 2 Arrays 1.3 Data Abstraction and Encapsulation 2.2 The Array As An abstract Data Type.
Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Data Structures Chapter 2: Arrays 2-1. Four components in a C++ Class –class name –data members: the data that makes up the class –member functions: the.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Data Abstraction and Encapsulation  Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
◦ A one-dimensional array in C is declared implicitly by appending brackets to the name of a variable int list[5], *plist[5]; ◦ arrays start at index.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
LINKED LISTS.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Data Structure Sang Yong Han
More Linking Up with Linked Lists
Chapter 3 Linear List (Sequential List)
CSCE 3110 Data Structures & Algorithm Analysis
Chap 2 Array (陣列).
Data Structures 4th Week
Dr. Ameria Eldosoky Discrete mathematics
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
 Zero - set zero of a polynomial
CH2. ARRAYS.
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
A type is a collection of values
Presentation transcript:

Data Abstraction and Encapsulation Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world.

Data Abstraction and Encapsulation Definition: Data Abstraction is the separation between the specification of a data object and its implementation. Definition: A data type is a collection of objects and a set of operations that act on those objects.

Advantages of Data Abstraction and Data Encapsulation Simplification of software development Testing and Debugging Reusability Modifications to the representation of a data type

Data Abstraction and Encapsulation Definition: An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.

ADT Example ADT NaturalNumber is objects: An ordered sub-range of the integers starting at zero and ending at the maximum integer (MAXINT) on the computer. functions: for all x, y belong to NaturalNumber; TRUE, FALSE belong to Boolean and where +, -, <, ==, and = are the usual integer operations Zero(): NaturalNumber ::= 0 IsZero(x): Boolean ::= if (x == 0) IsZero = TRUE else IsZero = FALSE Add(x, y): NaturalNumber ::= if (x+y <= MAXINT) Add = x + y else Add = MAXINT Equal(x, y): Boolean ::= if (x == y) Equal = TRUE else Equal = FALSE Successor(x): NaturalNumber ::= if (x == MAXINT) Successor = x else Successor = x +1 Substract(x, y): NaturalNumber ::= if (x < y) Substract = 0 else Substract = x – y end NaturalNumber

Array Is it necessary to define an array as an ADT? C++ array requires the index set to be a set of consecutive integers starting at 0 C++ does not check an array index to ensure that it belongs to the range for which the array is defined.

ADT 2.1 Abstract data type GeneralArray class GeneralArray { // objects: A set of pairs < index, value> where for each value of index in IndexSet there // is a value of type float. IndexSet is a finite ordered set of one or more dimensions, // for example, {0, …, n-1} for one dimension, {(0, 0), (0, 1), (0, 2),(1, 0), (1, 1), (1, 2), (2, 0), // (2, 1), (2, 2)} for two dimensions, etc. public: GeneralArray(int j; RangeList list, float initValue = defatultValue); // The constructor GeneralArray creates a j dimensional array of floats; the range of // the kth dimension is given by the kth element of list. For each index i in the index // set, insert <i, initValue> into the array. float Retrieve(index i); // if (i is in the index set of the array) return the float associated with i // in the array; else signal an error void Store(index i, float x); // if (i is in the index set of the array) delete any pair of the form <i, y> present // in the array and insert the new pair <i, x>; else signal an error. }; // end of GeneralArray

ADT 2.2 Abstract Data Type Polynomial class polynomial { // objects: a set of ordered pairs of <ei, ai> //where ai Coefficient and ei Exponent // We assume that Exponent consists of integers ≥ 0 public: Polynomial(); // return the polynomial p(x) = 0 int operator!(); // if *this is the zero polynomial, return 1; else return 0; Coefficient Coef(Exponent e); // return the coefficient of e in *this Exponent LeadExp(); // return the largest exponent in *this Polynomial Add(Polynomial poly); // return the sum of the polynomials *this and poly Polynomial Mult(Polynomial poly); // return the product of the polynomials *this and poly float Eval(float f); // Evaluate the polynomial *this at f and return the result }; end of Polynomial

Polynomial Representations private: int degree; // degree ≤ MaxDegree float coef [MaxDegree + 1]; Representation 2 int degree; float *coef; Polynomial::Polynomial(int d) { degree = d; coef = new float [degree+1]; }

Polynomial Representations class Polynomial; // forward delcaration class term { friend Polynomial; private: float coef; // coefficient int exp; // exponent }; static term termArray[MaxTerms]; static int free; int Start, Finish; term Polynomial:: termArray[MaxTerms]; Int Polynomial::free = 0; // location of next free location in temArray

Figure 2.1 Array Representation of two Polynomials Represent the following two polynomials: A(x) = 2x1000 + 1 B(x) = x4 + 10x3 + 3x2 + 1 A.Start A.Finish B.Start B.Finish free coef 2 1 10 3 exp 1000 4 5 6

Polynomial Addition O(m+n) Polynomial Polynomial:: Add(Polynomial B) // return the sum of A(x) ( in *this) and B(x) { Polynomial C; int a = Start; int b = B.Start; C.Start = free; float c; while ((a <= Finish) && (b <= B.Finish)) switch (compare(termArray[a].exp, termArray[b].exp)) { case ‘=‘: c = termArray[a].coef +termArray[b].coef; if ( c ) NewTerm(c, termArray[a].exp); a++; b++; break; case ‘<‘: NewTerm(termArray[b].coef, termArray[b].exp); b++; case ‘>’: NewTerm(termArray[a].coef, termArray[a].exp); a++; } // end of switch and while // add in remaining terms of A(x) for (; a<= Finish; a++) // add in remaining terms of B(x) for (; b<= B.Finish; b++) C.Finish = free – 1; return C; } // end of Add O(m+n)

Program 2.9 Adding a new Term void Polynomial::NewTerm(float c, int e) // Add a new term to C(x) { if (free >= MaxTerms) { cerr << “Too many terms in polynomials”<< endl; exit(); } termArray[free].coef = c; termArray[free].exp = e; free++; } // end of NewTerm

Disadvantages of Representing Polynomials by Arrays What should we do when free is going to exceed MaxTerms? Either quit or reused the space of unused polynomials. But costly. If use a single array of terms for each polynomial, it may alleviate the above issue but it penalizes the performance of the program due to the need of knowing the size of a polynomial beforehand. How about Insertion and deletion?

Sparse Matrices

ADT 2.3 Abstract data type SparseMatrix class SparseMatrix // objects: A set of triples, <row, column, value>, where row and column are integers and form a unique combinations; value is also an integer. public: SparseMatrix(int MaxRow, int MaxCol); // the constructor function creates a SparseMatrix that can hold up to // MaxInterms = MaxRow x MaxCol and whose maximum row size is MaxRow // and whose maximum column size is MaxCol SparseMatrix Transpose(); // returns the SparseMatrix obtained by interchanging the row and column value // of every triple in *this SparseMatrix Add(SparseMatrix b); // if the dimensions of a (*this) and b are the same, then the matrix produced by // adding corresponding items, namely those with identical row and column // values is returned // else error. SparseMatrix Multiply(SparseMatrix b); // if number of columns in a (*this) equals number of rows in b then the matrix d // produced by multiplying a by b according to the formula d[i][j] = // Σ(a[i][k] . b[k][j]), where d[i][j] is the (i, j)th element, is returned. k ranges from 0 // to the number of columns in a – 1 // else error };

Sparse Matrix Representation Use triple <row, column, value> Store triples row by row For all triples within a row, their column indices are in ascending order. Must know the number of rows and columns and the number of nonzero elements

Sparse Matrix Representation (Cont.) class SparseMatrix; // forward declaration class MatrixTerm { friend class SparseMatrix private: int row, col, value; }; In class SparseMatrix: int Rows, Cols, Terms; MatrixTerm smArray[MaxTerms];

Transposing A Matrix Intuitive way: More efficient way: for (each row i) take element (i, j, value) and store it in (j, i, value) of the transpose More efficient way: for (all elements in column j) place element (i, j, value) in position (j, i, value)

Program 2.10 Transposing a Matrix SparseMatrix SparseMatrix::Transpose() // return the transpose of a (*this) { SparseMatrix b; b.Rows = Cols; // rows in b = columns in a b.Cols = Rows; // columns in b = rows in a b.Terms = Terms; // terms in b = terms in a if (Terms > 0) // nonzero matrix int CurrentB = 0; for (int c = 0; c < Cols; c++) // transpose by columns for (int i = 0; i < Terms; i++) // find elements in column c if (smArray[i].col == c) { b.smArray[CurrentB].row = c; b.smArray[CurrentB].col = smArray[i].row; b.smArray[CurrentB].value = smArray[i].value; CurrentB++; } } // end of if (Terms > 0) } // end of transpose O(terms*columns)

Fast Matrix Transpose The O(terms*columns) time => O(rows*columns2) when terms is the order of rows*columns A better transpose function in Program 2.11. It first computes how many terms in each columns of matrix a before transposing to matrix b. Then it determines where is the starting point of each row for matrix b. Finally it moves each term from a to b.

Program 2.11 Fast Matrix Transposing SparseMatrix SparseMatrix::Transpose() // The transpose of a(*this) is placed in b and is found in Q(terms + columns) time. { int *RowSize = new int[Cols]; int *RowStart = new int[Rows]; SparseMatrix b; b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; if (Terms > 0) // nonzero matrix // compute RowSize[i] = number of terms in row i of b for (int i = 0; i < Cols; i++) RowSize[i] = 0; // Initialize for ( i= 0; i < Terms; i++) RowSize[smArray[i].col]++; // RowStart[i] = starting position of row i in b RowStart[0] = 0; for (i = 1; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1]; O(columns) O(terms) O(columns-1)

Program 2.11 Fast Matrix Transposing (Cont.) for (i = 1; i < Terms; i++) // move from a to b { int j = RowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; RowStart[smArray[i].col]++; } // end of for } // end of if delete [] RowSize; delete [] RowStart; return b; } // end of FastTranspose O(terms) O(row * column)

Representation of Arrays Multidimensional arrays are usually implemented by one dimensional array via row major order. Example: One dimensional array α α+1 α+2 α+3 α+4 α+5 α+6 α+7 α+8 α+9 α+10 α+12 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]

Two Dimensional Array Row Major Order Col 0 Col 1 Col 2 Col u2 - 1 Row 0 X X X X Row 1 X X X X Row u1 - 1 X X X X u2 elements u2 elements Row 0 Row 1 Row i Row u1 - 1 i * u2 element

Generalizing Array Representation The address indexing of Array A[i1][i2],…,[in] is α+ i1 u2 u3 … un + i2 u3 u4 … un + i3 u4 u5 … un : + in-1 un + in =α+