Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1
» Problem Solving is different from coding ˃Algorithm ˃Documentation ˃Code » Algorithm and Documentation is Abstract Layer » Code is Implementation Layer 2 © Sekolah Tinggi Teknik Surabaya
» Abstraction is important to ˃Improve Readablity ˃Organize Data through Encapsulation » Abstract Data Type (ADT) ˃an object with a generic description independent of implementation details ˃we think about what can be done with the data, not how it is done 3 © Sekolah Tinggi Teknik Surabaya
» ADT abstracts away from a specific representation to focus on the semantic meaning of the data » In what sense are the following two definitions different? » Although the representations differ, the client should instead consider a Point as a set of operations to create and manipulate 2D points on the plane » By restricting the client to only call operations to access data, the potential for modifying the representation (and supporting algorithms) remains 4 © Sekolah Tinggi Teknik Surabaya
» The only operations on objects of the type are those provided by the abstraction » The implementation is hidden 5 © Sekolah Tinggi Teknik Surabaya
6
» An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation 7 © Sekolah Tinggi Teknik Surabaya
» Consider example of an airplane flight with 10 seats to be assigned » Tasks ˃List available seats ˃Reserve a seat » How to store, access data?
» Consider example of an airplane flight with 10 seats to be assigned » Tasks ˃List available seats ˃Reserve a seat » How to store, access data? ˃10 individual variables
» Algorithm to List available seats 1. If seat1 == ‘ ’: display 1 2. If seat2 == ‘ ’: display If seat10 == ‘ ’: display 10 » Algorithm to Reserve a seat 1. Set DONE to false 2. If seat1 ==‘ ’: print “do you want seat #1??” Get answer if answer==‘Y’: set seat1 to ‘X’ set Done to True 3. If seat2 ==‘ ’: print “do you want seat #2??” Get answer if answer==‘Y’: set seat2 to ‘X’ set Done to True.
» Consider example of an airplane flight with 10 seats to be assigned » Tasks ˃List available seats ˃Reserve a seat » How to store, access data? ˃10 individual variables ˃An array of variables
» Algorithm to List available seats For number ranging from 0 to max_seats-1, do: If seat[number] == ‘ ’: Display number » Algorithm to Reserve a seat Read in number of seat to be reserved If seat[number] is equal to ‘ ’: set seat[number] to ‘X’ Else Display a message that the seat having this number is occupied
» Develop a USD ADT ˃Properties +dollar: int +cent: int ˃Methods +Add +Subtract +Multiply +Divide +ToString 13 © Sekolah Tinggi Teknik Surabaya
» Abstract Data Type, e, accessed on March 24 th e » Abstract Data Types, ml, accessed on March 24 th ml » Dr. Bernard Chen Ph.D., Data Structures and Abstract Data Type, © Sekolah Tinggi Teknik Surabaya