Date post: | 16-Apr-2017 |
Category: |
Education |
Upload: | andres-mendez-vazquez |
View: | 286 times |
Download: | 0 times |
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
2 / 30
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
3 / 30
Images/cinvestav-1.jpg
What is a data structure?
IntuitionA data object is a set or collection of instances!!!
Examplesinteger = {0, +1, -1, +2, -2, +3, -3, ...}daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
Images/cinvestav-1.jpg
What is a data structure?
IntuitionA data object is a set or collection of instances!!!
Examplesinteger = {0, +1, -1, +2, -2, +3, -3, ...}daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
Images/cinvestav-1.jpg
What is a data structure?
IntuitionA data object is a set or collection of instances!!!
Examplesinteger = {0, +1, -1, +2, -2, +3, -3, ...}daysOfWeek = {S,M,T,W,Th,F,Sa}
4 / 30
Images/cinvestav-1.jpg
Those instance may be related!!!
Something quite basicInstances may or may not be related.
ExamplemyDataObject = {apple, chair, 2, 5.2, red, green, Jack}
ThusData Structure ≈ Data object + relationships that exist among instancesand elements that comprise an instance.
5 / 30
Images/cinvestav-1.jpg
Those instance may be related!!!
Something quite basicInstances may or may not be related.
ExamplemyDataObject = {apple, chair, 2, 5.2, red, green, Jack}
ThusData Structure ≈ Data object + relationships that exist among instancesand elements that comprise an instance.
5 / 30
Images/cinvestav-1.jpg
Those instance may be related!!!
Something quite basicInstances may or may not be related.
ExamplemyDataObject = {apple, chair, 2, 5.2, red, green, Jack}
ThusData Structure ≈ Data object + relationships that exist among instancesand elements that comprise an instance.
5 / 30
Images/cinvestav-1.jpg
Examples
Among instances of integers369 < 370280 + 4 = 284
ThusThe relationships are usually specified by specifying operations on one ormore instances.
Examplesadd, subtract, predecessor, multiply
6 / 30
Images/cinvestav-1.jpg
Examples
Among instances of integers369 < 370280 + 4 = 284
ThusThe relationships are usually specified by specifying operations on one ormore instances.
Examplesadd, subtract, predecessor, multiply
6 / 30
Images/cinvestav-1.jpg
Examples
Among instances of integers369 < 370280 + 4 = 284
ThusThe relationships are usually specified by specifying operations on one ormore instances.
Examplesadd, subtract, predecessor, multiply
6 / 30
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
7 / 30
Images/cinvestav-1.jpg
Abstract Data Type
Data TypeA data type such as int or double is a group of values and operations onthose values that is defined within a specific programming language.
Abstract Data TypeAn Abstract Data Type, or ADT, is a specification for a group of valuesand the operations on those values that is defined conceptually andindependently of any programming language.
ThusA data structure is an implementation of an ADT within a programminglanguage.
8 / 30
Images/cinvestav-1.jpg
Abstract Data Type
Data TypeA data type such as int or double is a group of values and operations onthose values that is defined within a specific programming language.
Abstract Data TypeAn Abstract Data Type, or ADT, is a specification for a group of valuesand the operations on those values that is defined conceptually andindependently of any programming language.
ThusA data structure is an implementation of an ADT within a programminglanguage.
8 / 30
Images/cinvestav-1.jpg
Abstract Data Type
Data TypeA data type such as int or double is a group of values and operations onthose values that is defined within a specific programming language.
Abstract Data TypeAn Abstract Data Type, or ADT, is a specification for a group of valuesand the operations on those values that is defined conceptually andindependently of any programming language.
ThusA data structure is an implementation of an ADT within a programminglanguage.
8 / 30
Images/cinvestav-1.jpg
Using Abstract Data Types
Something NotableAn abstract data type is defined indirectly, only by the operations that maybe performed on it and by mathematical constraints on the effects (andpossibly cost) of those operations.
Using Abstract Data Types1 An abstract object may be operated upon by the operations which
define its abstract type.2 An abstract object may be passed as a parameter to a procedure.3 An abstract object may be assigned to a variable.
9 / 30
Images/cinvestav-1.jpg
Using Abstract Data Types
Something NotableAn abstract data type is defined indirectly, only by the operations that maybe performed on it and by mathematical constraints on the effects (andpossibly cost) of those operations.
Using Abstract Data Types1 An abstract object may be operated upon by the operations which
define its abstract type.2 An abstract object may be passed as a parameter to a procedure.3 An abstract object may be assigned to a variable.
9 / 30
Images/cinvestav-1.jpg
Using Abstract Data Types
Something NotableAn abstract data type is defined indirectly, only by the operations that maybe performed on it and by mathematical constraints on the effects (andpossibly cost) of those operations.
Using Abstract Data Types1 An abstract object may be operated upon by the operations which
define its abstract type.2 An abstract object may be passed as a parameter to a procedure.3 An abstract object may be assigned to a variable.
9 / 30
Images/cinvestav-1.jpg
Using Abstract Data Types
Something NotableAn abstract data type is defined indirectly, only by the operations that maybe performed on it and by mathematical constraints on the effects (andpossibly cost) of those operations.
Using Abstract Data Types1 An abstract object may be operated upon by the operations which
define its abstract type.2 An abstract object may be passed as a parameter to a procedure.3 An abstract object may be assigned to a variable.
9 / 30
Images/cinvestav-1.jpg
Using Abstract Data Types
Something NotableAn abstract data type is defined indirectly, only by the operations that maybe performed on it and by mathematical constraints on the effects (andpossibly cost) of those operations.
Using Abstract Data Types1 An abstract object may be operated upon by the operations which
define its abstract type.2 An abstract object may be passed as a parameter to a procedure.3 An abstract object may be assigned to a variable.
9 / 30
Images/cinvestav-1.jpg
For More
We have the following“PROGRAMMING WITH ABSTRACT DATA TYPES” by Barbara Liskovand Stephen Zilles
10 / 30
Images/cinvestav-1.jpg
Abstract Data Types: An Example
We will start withAn example using Lists.
Actually in JavaAn interface in the Java programming language is an abstract type that isused to specify an interface (in the generic sense of the term) that classesmust implement.
11 / 30
Images/cinvestav-1.jpg
Abstract Data Types: An Example
We will start withAn example using Lists.
Actually in JavaAn interface in the Java programming language is an abstract type that isused to specify an interface (in the generic sense of the term) that classesmust implement.
11 / 30
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
12 / 30
Images/cinvestav-1.jpg
Linear (or Ordered) Lists
DefinitionA linear list is a data structure that holds a sequential list of elements.
InstancesThey have the following structure, (e0, e1, e1, ..., en−1)
PropertiesWhere ei denotes a list elementn ≥ 0 is finiteList size is n
13 / 30
Images/cinvestav-1.jpg
Linear (or Ordered) Lists
DefinitionA linear list is a data structure that holds a sequential list of elements.
InstancesThey have the following structure, (e0, e1, e1, ..., en−1)
PropertiesWhere ei denotes a list elementn ≥ 0 is finiteList size is n
13 / 30
Images/cinvestav-1.jpg
Linear (or Ordered) Lists
DefinitionA linear list is a data structure that holds a sequential list of elements.
InstancesThey have the following structure, (e0, e1, e1, ..., en−1)
PropertiesWhere ei denotes a list elementn ≥ 0 is finiteList size is n
13 / 30
Images/cinvestav-1.jpg
Linear (or Ordered) Lists
DefinitionA linear list is a data structure that holds a sequential list of elements.
InstancesThey have the following structure, (e0, e1, e1, ..., en−1)
PropertiesWhere ei denotes a list elementn ≥ 0 is finiteList size is n
13 / 30
Images/cinvestav-1.jpg
What are the relations inside of a Liner List?
Having thisL = (e0, e1, e2, ..., en−1)
Relationshipse0 is the zero’th (or front) elementen−1 is the last elementei immediately precedes ei+1
14 / 30
Images/cinvestav-1.jpg
What are the relations inside of a Liner List?
Having thisL = (e0, e1, e2, ..., en−1)
Relationshipse0 is the zero’th (or front) elementen−1 is the last elementei immediately precedes ei+1
14 / 30
Images/cinvestav-1.jpg
What are the relations inside of a Liner List?
Having thisL = (e0, e1, e2, ..., en−1)
Relationshipse0 is the zero’th (or front) elementen−1 is the last elementei immediately precedes ei+1
14 / 30
Images/cinvestav-1.jpg
What are the relations inside of a Liner List?
Having thisL = (e0, e1, e2, ..., en−1)
Relationshipse0 is the zero’th (or front) elementen−1 is the last elementei immediately precedes ei+1
14 / 30
Images/cinvestav-1.jpg
Examples
Simple onesStudents in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)Exams in COP3530 = (exam1, exam2, exam3)Days of Week = (S, M, T, W, Th, F, Sa)Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
Images/cinvestav-1.jpg
Examples
Simple onesStudents in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)Exams in COP3530 = (exam1, exam2, exam3)Days of Week = (S, M, T, W, Th, F, Sa)Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
Images/cinvestav-1.jpg
Examples
Simple onesStudents in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)Exams in COP3530 = (exam1, exam2, exam3)Days of Week = (S, M, T, W, Th, F, Sa)Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
Images/cinvestav-1.jpg
Examples
Simple onesStudents in COP3530 = (Jack, Jill, Abe, Henry, Mary, ..., Judy)Exams in COP3530 = (exam1, exam2, exam3)Days of Week = (S, M, T, W, Th, F, Sa)Months = (Jan, Feb, Mar, Apr, ..., Nov, Dec)
15 / 30
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
16 / 30
Images/cinvestav-1.jpg
Which operations must be supported?
We need the size of a linear listHey, we need to know how many elements the linear list has.
Determine list size forL = (a, b, c, d , e)
So, we need to have a operation that returns the sizesize(L)=5
17 / 30
Images/cinvestav-1.jpg
Which operations must be supported?
We need the size of a linear listHey, we need to know how many elements the linear list has.
Determine list size forL = (a, b, c, d , e)
So, we need to have a operation that returns the sizesize(L)=5
17 / 30
Images/cinvestav-1.jpg
Which operations must be supported?
We need the size of a linear listHey, we need to know how many elements the linear list has.
Determine list size forL = (a, b, c, d , e)
So, we need to have a operation that returns the sizesize(L)=5
17 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: get(theIndex)
Get operationsGet element with given index.
For exampleL = (a, b, c, d , e)
Thusget(0) = aget(2) = cget(4) = eget(-1) = errorget(9) = error
18 / 30
Images/cinvestav-1.jpg
Linear List Operations: indexOf(theElement)
IndexOf operationsDetermine the index of an element.
ExampleL = (a, b, d , b, a)
ThusindexOf(d) = 2indexOf(a) = 0indexOf(z) = -1
19 / 30
Images/cinvestav-1.jpg
Linear List Operations: indexOf(theElement)
IndexOf operationsDetermine the index of an element.
ExampleL = (a, b, d , b, a)
ThusindexOf(d) = 2indexOf(a) = 0indexOf(z) = -1
19 / 30
Images/cinvestav-1.jpg
Linear List Operations: indexOf(theElement)
IndexOf operationsDetermine the index of an element.
ExampleL = (a, b, d , b, a)
ThusindexOf(d) = 2indexOf(a) = 0indexOf(z) = -1
19 / 30
Images/cinvestav-1.jpg
Linear List Operations: indexOf(theElement)
IndexOf operationsDetermine the index of an element.
ExampleL = (a, b, d , b, a)
ThusindexOf(d) = 2indexOf(a) = 0indexOf(z) = -1
19 / 30
Images/cinvestav-1.jpg
Linear List Operations: indexOf(theElement)
IndexOf operationsDetermine the index of an element.
ExampleL = (a, b, d , b, a)
ThusindexOf(d) = 2indexOf(a) = 0indexOf(z) = -1
19 / 30
Images/cinvestav-1.jpg
Linear List Operations: add(theIndex, theElement)
DefinitionAdd an element so that the new element has a specified index.
If we haveL = (a, b, c, d , e, f , g)
Thusadd(0,h) =⇒ L = (h, a, b, c, d , e, f , g)
I Thus, index of a, b, c, d , e, f , g increases by 1.
20 / 30
Images/cinvestav-1.jpg
Linear List Operations: add(theIndex, theElement)
DefinitionAdd an element so that the new element has a specified index.
If we haveL = (a, b, c, d , e, f , g)
Thusadd(0,h) =⇒ L = (h, a, b, c, d , e, f , g)
I Thus, index of a, b, c, d , e, f , g increases by 1.
20 / 30
Images/cinvestav-1.jpg
Linear List Operations: add(theIndex, theElement)
DefinitionAdd an element so that the new element has a specified index.
If we haveL = (a, b, c, d , e, f , g)
Thusadd(0,h) =⇒ L = (h, a, b, c, d , e, f , g)
I Thus, index of a, b, c, d , e, f , g increases by 1.
20 / 30
Images/cinvestav-1.jpg
Example
add(2,h) using the original L=⇒ L = (a, b, h, c, d , e, f , g)
I index of c, d , e, f , g increases by 1
What about out of the boundadd(10,h) =⇒ erroradd(-6,h) =⇒ error
21 / 30
Images/cinvestav-1.jpg
Example
add(2,h) using the original L=⇒ L = (a, b, h, c, d , e, f , g)
I index of c, d , e, f , g increases by 1
What about out of the boundadd(10,h) =⇒ erroradd(-6,h) =⇒ error
21 / 30
Images/cinvestav-1.jpg
Example
add(2,h) using the original L=⇒ L = (a, b, h, c, d , e, f , g)
I index of c, d , e, f , g increases by 1
What about out of the boundadd(10,h) =⇒ erroradd(-6,h) =⇒ error
21 / 30
Images/cinvestav-1.jpg
Linear List Operations: remove(theIndex)
DefinitionRemove and return element with given index.
For exampleL = (a, b, c, d , e, f , g)
ExampleRemove(2) returns c and L becomes (a, b, d , e, f , g)
I Index of d , e, f , g decreases by 1
22 / 30
Images/cinvestav-1.jpg
Linear List Operations: remove(theIndex)
DefinitionRemove and return element with given index.
For exampleL = (a, b, c, d , e, f , g)
ExampleRemove(2) returns c and L becomes (a, b, d , e, f , g)
I Index of d , e, f , g decreases by 1
22 / 30
Images/cinvestav-1.jpg
Linear List Operations: remove(theIndex)
DefinitionRemove and return element with given index.
For exampleL = (a, b, c, d , e, f , g)
ExampleRemove(2) returns c and L becomes (a, b, d , e, f , g)
I Index of d , e, f , g decreases by 1
22 / 30
Images/cinvestav-1.jpg
What about the Error
ForL = (a, b, c, d , e, f , g)
Thusremove(-1) =⇒ errorremove(20) =⇒ error
23 / 30
Images/cinvestav-1.jpg
What about the Error
ForL = (a, b, c, d , e, f , g)
Thusremove(-1) =⇒ errorremove(20) =⇒ error
23 / 30
Images/cinvestav-1.jpg
The Final Abstract Data Type for Linear List
Linear ListAbstractDataType LinearList{
instancesordered finite collections of zero or more elements
operationsisEmpty(): return true iff the list is empty, false otherwisesize(): return the list size (i.e., number of elements in the list)get(index): return the element with “index” indexindexOf(x): return the index of the first occurrence of x in the list, return -1
if x is not in the listremove(index): remove and return the indexth element, elements with higher
index have their index reduced by 1add(theIndex, x): insert x as the index of th element, elements with
theIndex ≥ index have their index increased by 1output(): output the list elements from left to right
}
24 / 30
Images/cinvestav-1.jpg
Java Interface
We can implement this idea of Abstract Data TypesUsing Java through the generic interface!!!An interface may include constants and abstract methods (i.e.,methods for which no implementation is provided).
Example for List
25 / 30
Images/cinvestav-1.jpg
Java InterfaceWe can implement this idea of Abstract Data Types
Using Java through the generic interface!!!An interface may include constants and abstract methods (i.e.,methods for which no implementation is provided).
Example for List
p u b l i c i n t e r f a c e L i n e a r L i s t <Item>{p u b l i c boo l ean isEmpty ( ) ;p u b l i c i n t s i z e ( ) ;p u b l i c I tem get ( i n t i nd ex ) ;p u b l i c i n t indexOf ( Item myobject ) ;p u b l i c vo i d add ( i n t index , I tem myobject ) ;p u b l i c I tem remove ( i n t i nd ex ) ;p u b l i c S t r i n g t o S t r i n g ( ) ;
}
25 / 30
Images/cinvestav-1.jpg
Implementing the Java Interface
Using a calss to implement the interface
c l a s s S imp l eA r r a y L i s t <Item> implementsL i n e a r L i s t <Item>{
// code f o r a l l methods must be p r o v i d ed
}
26 / 30
Images/cinvestav-1.jpg
Outline
1 IntroductionBasic IdeasAbstract Data Types
2 Linear List: An Example of ADTBasic DefinitionOperations
3 Generic Java InterfaceAbstract Classes Vs Interfaces
27 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Abstract Classes Vs Interfaces
However, we have another way to implement ADTAbstract Classes
Abstract ClassesAn abstract class is a special kind of class that cannot be instantiated.It is a contract that is used to define hierarchies for all subclasses.
InterfaceAn interface is not a class.An interface has no implementation.It only has the definition of the methods without the body.
28 / 30
Images/cinvestav-1.jpg
Important
We haveA Java class may implement as many interfaces as it wants but can extendat most one class.
This is whyOnce you have chosen your design you can enforce the structure usingAbstract Classes!!!
29 / 30
Images/cinvestav-1.jpg
Important
We haveA Java class may implement as many interfaces as it wants but can extendat most one class.
This is whyOnce you have chosen your design you can enforce the structure usingAbstract Classes!!!
29 / 30
Images/cinvestav-1.jpg
Example of Abstract Class
Code
p u b l i c a b s t r a c t c l a s s L i n e a r L i s t <Item>{p u b l i c boo l ean isEmpty ( ) ;p u b l i c i n t s i z e ( ) ;p u b l i c I tem get ( i n t i nd ex ) ;p u b l i c i n t indexOf ( Item myobject ) ;p u b l i c vo i d add ( i n t index , I tem myobject ) ;p u b l i c I tem remove ( i n t i nd ex ) ;p u b l i c S t r i n g t o S t r i n g ( ) ;
}
30 / 30