Abstract Data Type - Sekolah Tinggi Teknik Surabaya

Download Report

Transcript Abstract Data Type - Sekolah Tinggi Teknik Surabaya

1
Lecture 3
Abstract Data Type
Sandy Ardianto & Erick Pranata
© Sekolah Tinggi Teknik Surabaya
» 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
© Sekolah Tinggi Teknik Surabaya
4
» The only operations on objects of the
type are those provided by the
abstraction
» The implementation is hidden
5
© Sekolah Tinggi Teknik Surabaya
6
© Sekolah Tinggi Teknik Surabaya
» 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 2
.
.
.
10. If seat10 == ‘ ’:
display 10
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
.
.
.
Algorithm to Reserve a seat
» 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,
http://c2.com/cgi/wiki?AbstractDataTyp
e, accessed on March 24th 2014.
» Abstract Data Types,
http://cse.iitkgp.ac.in/pds/notes/ADT.ht
ml, accessed on March 24th 2014.
» Dr. Bernard Chen Ph.D., Data Structures
and Abstract Data Type, 2008
14
© Sekolah Tinggi Teknik Surabaya