280 likes | 291 Views
Procedural abstraction Information hiding. Abstract Data Types (ADT). Figure 3.1 Isolated tasks: the implementation of task T does not affect task Q. Figure 3.2 A slit in the wall. Add data to a data collection Remove data from a data collection
E N D
Procedural abstraction Information hiding Abstract Data Types (ADT)
Figure 3.1 Isolated tasks: the implementation of task T does not affect task Q
Figure 3.2 A slit in the wall
Add data to a data collection Remove data from a data collection Ask questions about the data in a data collection Operations on data
An abstract data type -- a collection of data and a set of operations on that data. A data structure -- a construct within a programming language that stores a collection of data. ADTs vs. Data Structures
Figure 3.3 A dispenser of chilled water, crushed ice, and ice cubes
Figure 3.4 A wall of ADT operations isolates a data structure from the program that uses it
Figure 3.5 A grocery list
Create an empty list. Determine whether a list is empty. Determine the number of items on a list. Add an item at a given position in the list. Remove the item at a given position in the list. Remove all the items from the list. Retrieve (get) the item at a given position in the list. ADT List Operations
createList() isEmpty() size() add(index, item) remove(index) removeAll() get(index) ADT List Operations-Pseudocode
To get a list of the following items: milk, eggs, butter, apples, bread, chicken First, declare aList as an object of List Then, aList.createList() aList.add(0, milk) aList.add(1, eggs) aList.add(2, butter) aList.add(3, apple) aList.add(4, bread) aList.add(5, chicken) ADT List Operations-Pseudocode
After the following statement aList.add(3, nuts) The list will be milk, eggs, butter, nuts, apples, bread, chicken If the following operation is performed aList.remove(4) The list will be milk, eggs, butter, nuts, bread, chicken ADT List Operations-Pseudocode
Once the behavior (basic operations) of an ADT is designed, access and manipulation of the ADT’s data can be defined based on its operations. displayList - displays all the items on the list aList. replace - replaces the ith item on the list aList with newItem. Access and Manipulation of ADT’s Data
displayList(aList) for (index = 0 through aList.size() - 1) { dataItem = aList.get(index) Display dataItem } Access and Manipulation of ADT’s Data - displayList
replace(aList, i, newItem) if (i >= 0 and i < aList.size()) { aList.remove(i) aList.add(i, newItem) } Access and Manipulation of ADT’s Data - replace
Figure 3.6 The wall between displayList and the implementation of the ADT list
Make an appointment for a certain date, time, and purpose. Cancel the appointment for a certain date and time. Check whether you have an appointment at a given time and date. Determine the purpose of the appointment at a given time and date. ADT for appointment book
createAppointmentBook() isAppointment(date, time) makeAppointment(date, time, purpose) cancelAppointment(date, time) checkAppointment(date, time) More operations based on the operations above, e.g. change the date or time of a particular appointment within the existing appointment book. What needs to be done to achieve that? ADT appointment -Pseudocode
Get the purpose of the old appointment Check if the new date and time is available to appointment If yes, make the appointment at new date and time. If no, remind the user of the existing appointment and the failure to make the new appointment. ADT appointment -Pseudocode
Java Classes (class Sphere) Java Inheritance (class ColoredSphere) Java Interfaces Java Exceptions Implementing ADTs in Java
Figure 3.7 ADT operations provide access to a data structure
Figure 3.8 Violating the wall of ADT operations
Figure 3.9 An object’s data and methods are encapsulated
createList() isEmpty() size() add(newPosition, newItem) remove(index) removeAll() get(index, dataItem) An Array-Based Implementation of the ADT List
Figure 3.10 An array-based implementation of the ADT list
Figure 3.11 Shifting items for insertion at index 2
Figure 3.12 a) Deletion causes a gap; b) fill gap by shifting