Data Abstraction l l A technique for increasing
Data Abstraction l l A technique for increasing the modularity of a program The need to support several operations on the data arise need to define abstract data types (ADTs) Only after you have clearly specified the operations of an abstract data type should you consider data structure for implementing it Typical operations on data: l l l Add data to a data collection Remove data from a data collection Ask questions about the data in a data collection 1
Data Abstraction l l l A modular program is easier to write, read, and modify Write specifications for each module before implementing it Isolate the implementation details of a module from other modules (Walls!) l l Isolation cannot be total, however, e. g. Although task Q does not know how task T is performed, it must know what task T is and how to initiate it What goes in and comes out is governed by the terms of the method’s specifications, or contract: If you use the method in this way, this is exactly what it will do for you 2
Data Abstraction l l Data abstraction asks that you think in terms of what you can do to a collection of data independently of how you do it Data abstraction is a technique that allows you to develop each data structure in relative isolation from the rest of the solution The other modules of the solution will “know” what operations they can perform on the data, but they should not depend on how the data is stored or how the operations are performed The terms of contract are what and not how 3
Abstract data Type l l l A collection of data together with a set of operations on that data are called an abstract data type, or ADT. Specifications indicate what ADT operations do, but not how to implement them Data structures are part of an ADT’s implementation, i. e. , you choose a particular data structure when you implement an ADT l l e. g. Arrays – data structure built into Java To store both the names and salaries of a group of employees: final int MAX_NUM = 500; String[] names = new String[MAX_NUMBER]; double[] salaries = new double[MAX_NUMBER]; So that the employee names[i] has a salary of salaries[i]. The two arrays names and salaries together form a data structure, yet Java has no single data type to describe it. 4
Abstract data Type l l When a program must perform data operations that are not directly supported by the language, you should first design an abstract data type and carefully specify what the ADT operations are to do (the contract) Then-and only then-should you implement the operations with a data structure The rest of the program will be able to assume that the operations perform as specified-that is, that the terms of the contract are honored. However, the program must not depend on a particular approach for supporting the operations 5
ADT versus Data Structure l l l An abstract data type is not another name for data structure. l An abstract data type is a collection of data and set of operations on that data l A data structure is a construct within a programming language that stores a collection of data Data abstraction results in a wall of ADT operations between data structures and the program that accesses the data within these data structures If you are on the program’s side of the wall, you will see an interface that enables you to communicate with the data structure 6
A wall of ADT operations isolates a data structure from the program that uses it Interface add Operation request Program Operation result remove Data structure find Display 7 Wall of ADT operations
The ADT List l List contain items of the same type. What can you do on the items on a list? l l l You might count the items to determine the length of the list, add an item to the list, remove an item from the list, or look at (retrieve) an item The items on the a list, together with operations that you can perform on the items, form an abstract data type Possibly want to access items any where on the list, i. e. , look at the item at position i, delete the item at position i, or insert an item at position i on the list. Such operations are part of the ADT list 8
ADT List Operations 1. 2. 3. 4. 5. 6. 7. l Create an empty list Determine whether the 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 An ADT list is simply an ordered collection of items that you reference by position number 9
Pseudocode for the ADT List Operations create. List() //creates an empty list is. Empty() //determines whether a list is empty size() //returns the number of items that are in the list add(index, item) //inserts item at position index of a list, if 1 <= index <= size() +1 //If index <= size(), items are renumbered as follows: The item at index //becomes the item at index+1, the item at index+1 becomes the //item at index+2, and so on. //Throws an exception when index is out of range or if the item cannot //be placed on the list (list full). 10
Pseudocode for the ADT List Operations remove(index) //Removes the item at position index of a list, if 1 <= index <= size(). If //index < size(), items are renumbered as follows: The item at //index+1 becomes the item at index, the item at index+2 becomes //the item at index+1, and so on. Throws an exception when index is //out of range or if the list is empty remove. All() //Removes all the items in the list get(index) //Returns the item at position index of a list if 1 <= index <= size(). The //list is left unchanged by this operation. Throws an exception if //index is out of range 11
Applying the ADT operations l l l Consider part of IS 137 student list: Lightness, Lazaro, Jane, Jaqlini, Clement, Joyce Where Lightness becomes the first list item and Joyce becomes the last item in the list Can construct this list by using the operations of the ADT list l One way is to create an empty list st. List and then use a series of insertion operations to append the items to the list as follows: st. List. create. List() st. List. add(1, Lightness) st. List. add(2, Lazaro) st. List. add(3, Jane) st. List. add(4, Jaqlini) st. List. add(5, Clement) st. List. add(6, Joyce) 12
Applying the ADT operations l l The notation st. List. O indicates that an operation O applies to the list st. List. Suppose now we perform the operation st. List. add(4, Nancy) the st. List now becomes Lightness, Lazaro, Jane, Nancy, Jaqlini, Clement, Joyce l l Why? Moreover, if now we perform the operation st. List. remove(5) the st. List now becomes Lightness, Lazaro, Jane, Nancy, Clement, Joyce l Why? 13
Applying the ADT operations l l l An ADT specification should not include implementation issues A program (client) should depend only on the behavior of the ADT The insertion, deletion, and retrieval operations throws an exception when the argument index is out of range l l This technique provides the ADT with a simple mechanism to communicate operation failure to its client Exceptions enable the client to handle error situations in an implementation-independent way 14
Applying the ADT operations l What does the specification of the ADT list tell you about its behaviour? l l The operation adds data to a data collection The operations remove and remove. All remove data from a data collection The operations is. Empty, size, and get ask questions about the data in a data collection Can design applications that access and manipulate the ADT’s data solely in terms of its operations and without regard for its implementation 15
Applying the ADT operations l Suppose we want to display the items in the list: can write a method display. List in terms of the operations that define the ADT list. display. List(st. List) //displays the items on the list a. List for (index = 1 through st. List. size()) { data. Item = st. List. get(index) Display data. Item } //end for l l The method disply. List dose not depend on how you implement the list The method will work regardless of whether you use an array or some other data structure to store the list’s data 16
Applying the ADT operations l Suppose that you want a method replace that replaces the item in position i with a new item. If the ith item exists, replace deletes the item and inserts the new item at position i, as follows: replace(st. List, i, new. Item) //Replaces the ith item on the list st. List with new. Item if(i > = 1 and i <= st. List. size()) { st. List. remove(i) st. List. add(i, new. Item) } //end if l Sum-up: Given an ADT specification, without any knowledge of how the ADT will be implemented, you can design applications that use the ADT’s operations to access its data 17
The ADT Sorted List l l The ADT sorted list maintains items in sorted order. The following specifications define the operations for the ADT sorted list Pseudocode for the ADT Sorted List Operations create. Sorted. List() //creates an empty sorted list sorted. Is. Empty() //determines whether a sorted list is empty sorted. Size() //returns the number of items that are in a sorted list 18
The ADT Sorted List Operations cnt. . sorted. Add(item) /* Inserts item into its proper sorted position in a sorted list. Throws an exception if the item cannot be placed on the list (list full) */ sorted. Remove(item) /* Deletes item from a sorted list. Throws an exception if the item is not found. */ sorted. Get(index) /* Returns the item at position index of a sorted list, if 1<= index <= sorted. Size(). The list is left unchanged by this operation. Throws an exception if the index is out of range */ locate. Index(item) /* Returns the position where item belongs or exists in a sorted list; item and the list are unchanged. */ 19
The ADT Sorted List l l l The ADT sorted list differs from the ADT list in that a sorted list inserts and deletes items by their values and not by their positions. Also, locate. Index which determines the position of any item, given its value is a sorted list operation but not a list operation Note: l l l You can design an ADT by identifying data and choosing operations that are suitable to your problem. You can use an ADT without knowledge of its implementation You can use an ADT to implement another ADT 20
Implementing ADTs l l l Data structures are part of an ADT’s implementation The design of an ADT results into a set of clearly specified ADT operations How do you implement an ADT once its operations are clearly specified? i. e. , how do you store the ADT’s data and carry out its operations? l Refine an ADT through successive levels of abstraction. i. e. , use top down approach to designing an algorithms for each of the ADT operations. Refinement process stops when you reach data structures that are available in your programming language 21
An array-Based Implementation of the ADT List l ADT List operations create. List() is. Empty() size() add(new. Position, new. Item) remove(index) remove. All() get(index, data. Item) 22
An array-Based Implementation of the ADT List l l Store the list ‘s items in any array items, thus a list’s kth item in items[k-1]. The maximum length of the array – fixed value such as MAX_LIST The list’s length tracked by a variable num. Items We use the following statements to implement a list of integers private final int MAX_LIST = 100; //max length of list private int items[MAX_LIST]; //array of list items private int num. Items; //length of list 23
An array-Based Implementation of the ADT List Array indexes 0 1 2 3 k 12 3 19 100 num. Items 1 2 3 4 k-1 … 5 items 10 18 k MAX_LIST -1 ? ? … ? MAX_LIST ADT list positions 24
Insert item at given position l Shift to the right the items from this position on, and insert the new item in the newly created opening 25
Shifting items for insertion at position 3 New item Array indexes 0 1 2 3 4 K+1 12 3 44 19 100 num. Items 1 2 3 4 k … 5 items 5 10 18 k+1 MAX_LIST -1 ? ? … ? MAX_LIST ADT list positions 26
Delete item from the list l l Could blank it out, but this strategy can lead to gaps in the array An array that is full of gaps has three significant problems l l num. Items – 1 is no longer the index of the last item in the array Because the items are spread out, the method get might have to look at every cell of the array, even when are few items are present When items[MAX_LIST -1] is occupied, the list coud appear full, even when fewer than MAX_LIST items are present Shift array elements to delete an item 27
Deletion causes a gap, fill gap by shifting Array indexes 0 1 2 k 12 3 44 num. Items 1 3 Delete 19 3 4 100 4 5 … 5 items k-1 k 10 18 k k+1 MAX_LIST -1 ? ? … ? MAX_LIST ADT list positions Array indexes 0 1 2 3 k 12 3 44 100 num. Items 1 3 4 ADT list positions k-1 … 5 items 10 18 k MAX_LIST -1 ? ? … ? MAX_LIST 28
An array-Based Implementation of the ADT List l l Implement the each ADT operation as a method of a class Each operation will require access to both the array items and the list’s length num. Items, so make items and num. Items data fields of the class To hide items and num. Items from the clients of the class, make these data fields private You may add auxiliary methods to make the implementation task easier, but such a method should be private. l l E. g. for convenience define the method translate(position), which returns the index value position -1 If one of the operations is provided an index value that is out of range, an exception should be thrown 29
An out-of-bounds list index exception definition l List. Index. Out. Of. Bound. Exception is based upon the more general Index. Out. Of. Bounds. Exception from the Java API: public class List. Index. Out. Of. Bound. Exception extends Index. Out. Of. Bounds. Exception{ public List. Index. Out. Of. Bounds. Exception(String s) { super(s); } //end constructor } //end List. Index. Out. Of. Bounds. Exception 30
List full exception definition l List. Exception is based upon the more general Runtime. Exception from the Java API public class List. Exception extends Runtime. Exception { public List. Exception(String s) { super(s) } // end constructor } //end List. Exception 31
Interface Integer. List. Interface l l l An interface specifies methods and constants but supplies no implementation Integer. List. Interface is an interface for a list of integers The ADT operation create. List does not appear in the interface because it will be implemented as a constructor //******************************** //Interface Integer. List. Interface for the ADT list //********************************* public interface Integer. List. Interface{ public boolean is. Empty(); //Determine whether a list is empty //precondition: None 32
//Postcondition: Returns true if the list is empty otherwise returns false //Throws: None public int size(); //Determines the length of a list //Precondition: None //Postcondition: Returns the number of items that are currently in the list //Throws: None public void remove. All(); //Deletes all the items from the lis //Precondition: None //Postcondition: the list is empty //Throws: None public void add(int index, int item) throws List. Index. Out. Of. Bounds. Exception, List. Exception; //Adds an item to the list at position index 33
//Preondition: index indicates the position at which the item should be inserted in the list // //Postcondition : If insertion is successful, item is at position index in the // list, and other items are renumbered accordingly //Throws: List. Index. Out. Of. Bounds. Exception if index < 1 or index > size() + 1 //Throws: List. Exception if item cannot be placed on the list public int get(int index) throws List. Index. Out. Of. Bounds. Exception; //Retrieves a list item by position Precondition: index is the number of the item to be retrieved Postcondition: If 1 <= index <= size(), the item at position index in the list is returned Throws: List. Index. Out. Of. Bounds. Exception if index < 1 or index > size() public void remove (int index) throws List. Index. Out. Of. Bounds. Exception; //Deletes an item from the list at a given position //Precondition: index indicates where the deletion should occur 34
//Postcondition : If 1 <= index <= size(), the item at position index in the list is deleted, and other items are renumbered accordingly //Throws: List. Index. Out. Of. Bounds. Exception if index < 1 or index > size() } // end Integer. List. Interface l l l The above interface is very limiting in that it will support only a list of integers If you use the class Object as the type for the list’s elements, the specification will be far more flexible Every class in Java is ultimately derived from the class Object through inheritance If the list item type is Object, you can no longer use the primitive type int as the item type, since int is not derived from the class Object For a list to contain items of primitive type, need to use a corresponding wrapper class from the Java API. 35
Revised List Interface //******************************** //Interface List. Interface for the ADT list //********************************* public interface List. Interface{ public boolean is. Empty(); public int size(); public void add(int index, Object item) throws List. Index. Out. Of. Bounds. Exception, List. Exception public Object get(int index) throws List. Index. Out. Of. Bounds. Exception; public void remove. All(); 36
Array Implementation of List. Interface //***************************************** //Array-based implementation of the ADT list //***************************************** public class List. Array. Based implements List. Interface { private static final int MAX_LIST = 50; private Object items[]; //an array of list items private int num. Items; //number of items in list public List. Array. Based() { items = new Object[MAX_LIST]; num. Items = 0; } // end default constructor public boolean is. Empty() { return (num. Items == 0); } // end is. Empty 37
public int size() { return num. Items; } //end size public void remove. All() { //create a new array; marks old array for garbage collection items = new Object[MAX_LIST]; num. Items = 0; } // end remove. All public void add(int index, Object item) throws List. Index. Out. Of. Bounds. Exception { if ( num. Items > MAX_LIST) { throw new List. Exception(“List. Exception on add”); } //end if if (index >=1 && index <= num. Items + 1) { //make room for new element by shifting all items at positions >= index //toward the end of the list (no shift if index == num. Items +1) 38
for (int pos = num. Items; pos >= index; pos--) { items[translate(pos +1)] = items[translate(pos)]; } // end for // insert new items[translate(index)] = item; num. Items++; } else { //index out of range throw new List. Index. Out. Of. Bounds. Exception( “List. Index. Out. Of. Bounds. Exception on add”); } // end if } //end add public Object get(int index) throws List. Index. Out. Of. Bounds. Exception { if (index >= 1 && index <= num. Items) { return items[translate(index)]; } else {//index out of range throw new List. Index. Out. Of. Bounds. Exception( “List. Index. Out. Of. Bounds. Exception on get”); } // end if } //end get 39
public void remove(int index) throws List. Index. Out. Of. Bounds. Exception { if (index >= 1 && index <= num. Items) { //delete item by shifting all items at positions > index toward the //beginning of the list (no shift if index == size) for (int pos = index+1; pos <= size(); pos++) { items[translate(pos-1)] = items[translate(pos)]; } //end for num. Items--: } else { //index out of range throw new List. Index. Out. Of. Bounds. Exception( “List. Index. Out. Of. Bounds. Exception on remove”); } // end if } //end remove private int translate(int position { return postion – 1; } //end translate } // end List. Array. Based 40
How to use List. Array. Based l The program fragment below demonstrates the use of List. Array. Based: public static void main(String args[]) { … List. Array. Based st. List = new List. Array. Based(); String data. Item; st. List. add(1, “Lightness”); … data. Item = st. List. get(1); … l Note: references such as st. List. num. Items, st. List. items[4], and st. List. translate(6) would be illegal, why? (b’z num. Items, Items and translate are private member of the class) 41
- Slides: 41