Docsity

Abstract Data Types: Understanding ADT, Data Structures, and Operations, Study notes of Design

Toggle sidebar
bg1
ADT, A. Bellaachia Page 1
Abstraction and Abstract Data Types
• Abstraction:
o Whatever is visible to the user?
• Examples: Variable names & real numbers.
o How real numbers are implemented?
o How arrays are implemented?
The abstraction is rectangular arrays
The implementation is one-dimensional array.
• Given the following algorithm:
S = set of persons.
X in S;
smallest-age = X;
S = S-{X};
While S is not empty do
Y in S;
S = S-{Y};
If age(Y) < age(X)
Then smallest = Y;
Endwhile;
o What do you need to implement such a problem?
pf3
pf4
pf5

Partial preview of the text

Download Abstract Data Types: Understanding ADT, Data Structures, and Operations and more Study notes Design in PDF only on Docsity!

Abstraction and Abstract Data Types

  • Abstraction : o Whatever is visible to the user?
  • Examples: Variable names & real numbers. o How real numbers are implemented? o How arrays are implemented?

 The abstraction is rectangular arrays

 The implementation is one-dimensional array.

  • Given the following algorithm:

S = set of persons. X in S; smallest-age = X; S = S-{X}; While S is not empty do Y in S; S = S-{Y}; If age(Y) < age(X) Then smallest = Y; Endwhile;

o What do you need to implement such a problem?

o For a given implementation,

 we need to make the following assumptions:

  1. The data we are dealing with are of some data types.
  2. A construct to store the data types: Data Structures
  3. The set of operations on the data types defined in(1).

o (1) & (3) =====> Abstract Data Types ( ADT ). o (2) =====> A way to implement the ADT.

  • Data Types: (1) o Definition: The data type of a variable determines the set of values the variable can take.

o Simple data types:

 Integer, real, character, enumeration types, etc.

o Structured data types:

 arrays, records, files, and sets.

  • Data Structures: (2) o Definition:  A data structure (D. S.) is a construct that you can define within a programming language to store a collection of data types.
  • Examples: o All structured data types are D.S. o Trees, Linked lists, etc.
  • OOP and ADT: o A class is an actual representation of an ADT.

o It provides implementation details for the data structure used and operations.

o OOP supports the implementation of an ADT using information hiding.

o Information hiding  is used to hide the implementation details  Prevent users from directly accessing data members ( private access modifier).

  • Example:

o ADT: Fraction  Data type:

  • numerator: integer;
  • denominator: integer;  Data structure: data types  Operations:
  • Add, multiply, equal, reduce, divide, etc.  Axioms and preconditions:
  • The denominator should be different from zero.

o Java Implementation:

//Fraction ADT public class Fraction {

private int numerator; private int denominator; public Fraction(){ numerator = 0; denominator = 1; } public Fraction( int num, int deno){ numerator = num; denominator = deno; if (denominator == 0){ throw new IllegalArgumentException("Denominator cannot be zero"); } } public void reduceF(){ int n = numerator; int d = denominator; while (d != 0) { int t = d; d = n % d; n = t; } numerator /= n; denominator /= n; }