Anda di halaman 1dari 20

ALGORITMA dan STRUKTUR DATA

Modularity and Data


Abstraction
Modularity
describes a program organized into loosely
coupled, highly cohesive modules”
– Carrano and Prichard, p. 7

“a technique that keeps the complexity of a large


program manageable by systematically controlling
the interaction of its components”
– Carrano and Prichard, p. 106

2 4/27/2022
In Short :
Modularitas adalah sejauh mana komponen
sistem dapat dipisahkan dan digabungkan kembali
Here we will know about
Abstract Data Type (ADT)
– Untuk lebih memahami ADT, mari kita lihat ilustrasinya

3 4/27/2022
Sistem Informasi Mahasiswa
Misalkan kita akan membuat Sistem Informasi
untuk menyimpan catatan siswa
Ada 4 menu
– Add new data
– Delete data
– Edit data
– View stored data

4 4/27/2022
Apa yang biasanya kita lakukan?
Basic Algorithm Writing Style
Type student < … Tipe deskripsi mahasiswa, nama, id, kelas,
> dll.
Dictionary
…… Mendeklarasikan Variabel dan nama fungsi
function add_student(…) yang akan digunakan dalam program
…… utama

Algorithm Program utama, berisi menu, interface,


…… dll.
…… Program utama akan menggunakan fungsi
yang tersedia atau telah ditentukan
Function add_student( … )
……
Function delete_student( … ) Spesifikasi fungsi, operasi dasar untuk
…… Mahasiswa
Procedure …… ……
…… All in one file (one script)

5 4/27/2022
Here, we actually have
3 bagian program atau 3 modul
Type student < …
> Type description and Primitive declaration
Dictionary [ Specification ]
……
function add_student(…)
……

Algorithm Body of main program


…… [ Program implementation ]
……

Function add_student( … )
……
Function delete_student( … ) Primitive Implementation (basic operation)
……
Procedure …… ……
……

6 4/27/2022
Abstract Data Type
Tujuan ADT adalah memisahkan/separates
antara specification dan implementation
ADT sendiri adalah bagian Spesifikasi yang berisi
deklarasi TYPE dan spesifikasi PRIMITIVE

7 4/27/2022
Megubah basic style
ADT style Algorithm
Basic Writing Stye : 1 file ADT Style : At least 3 files
Type student < … Header / type declaration
>
Type student < …
Dictionary
>
……
function add_student(…)
function add_student(…)
function delete_student(…)
……
Primitive specification
Algorithm
Function add_student( … )
……
……
……
Function delete_student( … )
……
Function add_student( … )
…… Main / body program
Function delete_student( … ) Algorithm
…… ……
Procedure …… …… ……
……
8 4/27/2022
Konektivitas Antara Modul
implement

Interface Header/Type
(Implementation) declaration

compile
specify

Primitive
specification

9 4/27/2022
Mengapa menggunakan ADT?
Security
– User hanya perlu mengetahui spesifikasi fitur tanpa perlu
mengetahui detail implementasi fitur tersebut.

Ada 4
menu ADT students
Interface
(main program)

Saya dapat
add, edit, or
delete data
Don’t need
to know how
they do it

10 4/27/2022
Why we use ADT?
Reusability
– Misalkan kita akan membangun sistem informasi lain yang
kebetulan juga menggunakan record mahasiswa (add,
edit, delete)
– Kita tidak perlu kode ADT lagi, kita bisa menggunakan apa
yang sudah kita miliki

ADT students
Interface 1 Interface 2
(main program 1) (main program 2)

IS 1 IS 2

11 4/27/2022
Question?
Prinsipnya DRY
Don't repeat yourself (tidak mengulangi pekerjaan
yang diulang-ulang)
“Every piece of knowledge must have a single,
unambiguous, authoritative representation within
a system”
– Bila prinsip DRY berhasil diterapkan, modifikasi elemen
tunggal suatu sistem tidak memerlukan perubahan
elemen logis lain yang tidak terkait.

13 4/27/2022
Task/Exercise : Clock ADT
Create a Clock ADT (Clock.h) to store time (hour,
minute, and second)

TYPE Hour : integer {0..23}


TYPE Minute : integer {0..59}
TYPE Second : integer {0..59}
TYPE Clock :
<
HH : Hour,
MM : Minute,
SS : Second;
>

14 4/27/2022
Task/Exercise : Clock ADT
Primitive for Clock.h
Validator
– Function IsValid(HH,MM,SS: integer) → boolean
– { return true if 0≤HH≤23, and 0≤MM≤59, and 0≤MM≤59 }
Constructor
– Function MakeClock(HH, MN, SS: integer) → clock
– { return clock created from input }

15 4/27/2022
Task/Exercise : Clock ADT
Selector
– Function GetHour(c : clock) → hour
– Function GetMinute(c : clock) → minute
– Function GetSecond(c : clock) → second

Value changer
– Procedure SetHour(In/Out c : clock, newHH: integer)
– Procedure SetMinute(In/Out c : clock, newMM: integer)
– Procedure SetSecond(In/Out c : clock, newSS: integer)

16 4/27/2022
Task/Exercise : Clock ADT
Relational Operation
– Function IsEqual (c1 : clock, c2 :clock) → boolean
Arithmetic Operation
– Function AddClock (c1 : clock, c2 :clock) → clock
Output Process
– Procedure PrintClock ( c : clock );

17 4/27/2022
Task/Exercise : Clock ADT
Create the Implementation of Clock ADT
(Clock.cpp)
Create the Driver application to try the
implementation (Main.cpp)
– Example :
– Clock c1  MakeClock(2,30,4)
– Clock c2  MakeClock(6,0,0)
– Clock c3  AddClock (c1, c2 )

18 4/27/2022
Clock ADT
Implementation Diagram of Clock ADT
//Clock.h
//TYPE Declaration
//PRIMITIF Declaration
//Main.Cpp
//Driver ADT Jam
//Clock.Cpp
//PRIMITIF
Implementation

19 1/9/06
Thanks You

4/27/2022
20

Anda mungkin juga menyukai