Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

TOPIC 1

-Roslan Sadjirin
At the end of the lesson, student should be able to:
 Understand and explain the concept of Abstract
Data Type.
 Understand and explain the concept of Data
Structures.
 List and explain the application of data structure

 Understand and describe the basic algorithm of


Bubble Sort, Insertion Sort, and Binary Search.
 Define and explain generic classes and its
application.
 OOP involves
◦ programming using objects
◦ focuses on structures of data with functionality or
processing capability to those structures.
 OOP is a programming methodology that
views a program as similarly consisting of
objects that interact with each other by
means of action i.e. message passing.
 Represent entity that has a state (properties
or attributes), exhibits some well-defined
behaviour (method/function) and has a
unique identity (name/reference).
 A self-contained entity which has its own
private collection of properties (i.e. data) and
methods (i.e. operation) that encapsulate
functionality into a reusable and dynamically
loaded structure.
 Represent a set or collection of objects that
share a common structure and a common
behaviour.
 Types:
◦ User-defined, e.g. Student, Account, Vehicle, etc.
◦ Predefined, e.g. Date, Scanner, String, Integer,
Double, etc.
 Attribute of the class object.
 Two types:
◦ Primitive, i.e. int, long, double etc.
◦ Reference data types (ADT):
 User-defined, e.g. Student, Account, Vehicle, etc.
 Predefined, e.g. Date, Scanner, String, Integer, Double,
ArrayList, LinkedList, Queue, Stack, etc.
 Represent the action of object
 Two types of methods:
◦ User defined methods: constructor (default, normal,
copy), storer/mutator, retriever/accessor,
processor, printer.
◦ Built-in methods: toString(), parseDouble(),
parseInt(), hasMoreTokens(), etc.
There are FOUR (4) main characteristics of
OOP:
 (1) Encapsulation & (2) Abstraction
 (3) Inheritance & (4) Polymorphism
 Grouping or binding the properties/attributes
(state), its data and operations (behaviour)
together into a single unit. The details of the
implementation are hidden from the other
classes, i.e. so called abstraction.
 Other class that uses the class (another class)
does not need to know all the details of its
implementation but need only know a
description of how to use the class.
 Encapsulation is achieved when each object
keeps its state private, inside a class. Other
objects don’t have direct access to this state.
Instead, they can only call a list of public
functions — called methods.
 The object manages its own state via methods —
 and no other class can touch it unless explicitly
allowed. If object wants to communicate with the
other object, the methods provided should be
used - but (by default), the state can’t be
changed. The thing that allowed to changed is
the state’s data.
 Abstraction is the natural extension of
encapsulation and separate the description of
how to use a class from the implementation
details
 Abstraction means hiding the details of the
internal implementation from the other
classes/objects.
 It only exposes a high level mechanism to the
other classes/objects for using it. – only reveal
relevant operation for the other objects.
 Advantage: avoid information overloading.
 Inheritance is a process by which a new class
(derived class) is created from another class
called the base class (super class).
 A derived class automatically has all the
instance variables and all the methods that
the base class has, and can have additional
methods and/or additional instance variables.
 Polymorphism – ability of an object (one name) to take many
shapes/forms.
 Two types of polymorphism:
◦ Overloading methods – static binding
◦ Overriding methods – dynamic binding
 Overloading methods:
◦ Allow the same word/symbol to mean different things in different
context.
◦ Allow the same method name to process different types and
arguments.
 Overriding methods:
◦ Allow changes being made in the method definition for derived
classes and apply those changes to the class/method written in the
base class (super class).
◦ Allow the same message to be sent to objects from different classes.
◦ Allow various objects to use the same reference object.
“We couldn’t possibly drive a car if we had to
worry about all the details that make the car
work: the spark plugs, the pistons, the
transmission, and so on. Instead, we can focus
on the interface to the car: the steering wheel,
the pedals and a few other controls.”
 These controls are an abstraction, hiding the
underlying details and enabling us to control
an otherwise very complicated machine
 Definition: ADT is a data type (a set of values
and a collection of operations on those
values) that is accessed only through
interface.
 A program that uses an ADT is refer as a
client and a program that specifies the data
type as an implementation
Figure: Concept of ADT
 ADT is a way of looking at data structures,
where it focuses on what it does rather than
how it does its job. The end user should
know what method to be called and how to
call it, rather than how the method works.
 A data structure is the implementation for an
ADT.
 In an OOP language such as Java, an ADT and
its implementation together make up a class.
 Each operation associated with the ADT is
implemented by a member function or
method.
 An arrangement of data in a computer’s
memory – sometimes on a disk
 Every structure of data needs a variety of
algorithms processing the data – insertion,
deletion and retrieval.
◦ Can only be accessed with defined operations
described in ADT.
 ArrayList (Sequential List) - Topic 2
 LinkedList – Topic 3
 Queue – Topic 4
 Stack – Topic 5
 Trees – Topic 6
 Discuss the application of data structures and
its operation on the data.
 Explain what is static data structures
 Explain what is dynamic data structures
 List and Explain advantages and
disadvantages of static and dynamic data
structures
Sorting:
 Sorting is the process of arranging a group of
items into a defined order, either ascending
or descending, based on the criterion.
◦ The task of putting a collection/list in increasing
order (or sometimes in decreasing order)
 Sorting is considered a classic area of study
in computer science.
 E.g. Bubble Sort, Insertion Sort, Selection Sort,
Quicksort, Mergesort, Heapsort, Radix Sort,
Special-Purpose Sort.
Characteristics:
 Slow, but it’s conceptually the simplest
sorting algorithms.
 Uses two nested loops
 Sort a list by repeatedly comparing
neighboring elements and swapping them if
necessary - so that the list will be in order.
 Makes several passes through the list.
 On each pass, successive neighboring pairs
are compared.
 If a pair is in decreasing order, its values are
swapped; otherwise, the values remain
unchanged.
 It’s called bubble sort or sinking sort,
because the smaller values gradually “bubble”
their way to the top and the larger values sink
to the bottom of the list.
Strategy:
 Scan through the list comparing adjacent
elements, and exchange (swap) them if they are
not in relative order.
◦ After the 1st pass, the last element becomes the largest
in the list.
 Then, scan through the list again, bubbling up
the second-to-last value.
◦ After the 2nd pass, the second-to-last element becomes
the second largest in the list.
 This process continues until all elements have
been bubbled into their correct positions.
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1
2
3
4
5
After
step-n
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1 2 9 5 4 8 1
2
3
4
5
After 2 9 5 4 8 1
step-n
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1
2 2 9 5 4 8 1
3
4
5
After 2 5 9 4 8 1
step-n
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1
2
3 2 5 9 4 8 1
4
5
After 2 5 4 9 8 1
step-n
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1
2
3
4 2 5 4 9 8 1
5
After 2 5 4 8 9 1
step-n
 1st pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 9 5 4 8 1
1st pass
Step 1
2
3
4
5 2 5 4 8 9 1
After 2 5 4 8 1 9
step-n
 2nd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 5 4 8 1 9
2nd pass
Step 1
2
3
4
5
After
step-n
 2nd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 5 4 8 1 9
2nd pass
Step 1 2 5 4 8 1 9
2
3
4
5
After 2 5 4 8 1 9
step-n
 2nd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 5 4 8 1 9
2nd pass
Step 1
2 2 5 4 8 1 9
3
4
5
After 2 4 5 8 1 9
step-n
 2nd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 5 4 8 1 9
2nd pass
Step 1
2
3 2 4 5 8 1 9
4
5
After 2 4 5 8 1 9
step-n
 2nd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 5 4 8 1 9
2nd pass
Step 1
2
3
4 2 4 5 8 1 9
5
After 2 4 5 1 8 9
step-n
 3rd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 5 1 8 9
3rd pass
Step 1
2
3
4
5
After
step-n
 3rd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 5 1 8 9
3rd pass
Step 1 2 4 5 1 8 9
2
3
4
5
After 2 4 5 1 8 9
step-n
 3rd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 5 1 8 9
3rd pass
Step 1
2 2 4 5 1 8 9
3
4
5
After 2 4 5 1 8 9
step-n
 3rd pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 5 1 8 9
2nd pass
Step 1
2
3 2 4 5 1 8 9
4
5
After 2 4 1 5 8 9
step-n
 4th pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 1 5 8 9
4th pass
Step 1
2
3
4
5
After
step-n
 4th pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 1 5 8 9
4th pass
Step 1 2 4 1 5 8 9
2
3
4
5
After 2 4 1 5 8 9
step-n
 4th pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 4 1 5 8 9
4th pass
Step 1
2 2 4 1 5 8 9
3
4
5
After 2 1 4 5 8 9
step-n
 5th pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 1 4 5 8 9
5th pass
Step 1
2
3
4
5
After
step-n
 5th pass (length = 6):
Index (i) [0] [1] [2] [3] [4] [5]
Before 2 1 4 5 8 9
6th pass
Step 1 2 1 4 5 8 9
2
3
4
5
After 1 2 4 5 8 9
step-n
 After the n-1 passes, the list is sorted!

1 2 4 5 8 9
list[index]
list.length = 6 0 1 2 3 4 5
Action
index < list.length- list[index] > temp = list[index] = list[index+1] = 2 9 5 4 8 1
i i < list.length-1 index (i+1) list[index+1] list[index] list[index+1] temp index++ i++
0 TRUE 0 0 < 5; TRUE FALSE 1 2 9 5 4 8 1 remain
1 1 < 5; TRUE TRUE 9 5 9 2 2 5 9 4 8 1 swap
2 2 < 5; TRUE TRUE 9 4 9 3 2 5 4 9 8 1 swap
3 3 < 5; TRUE TRUE 9 8 9 4 2 5 4 8 9 1 swap
4 4 < 5; TRUE TRUE 9 1 9 5 2 5 4 8 1 9 swap
5 5 < 5; FALSE 1 2 5 4 8 1 9 unsorted
1 TRUE 0 0 < 4; TRUE FALSE 1 2 5 4 8 1 9 remain
1 1 < 4; TRUE TRUE 5 4 5 2 2 4 5 8 1 9 swap
2 2 < 4; TRUE FALSE 3 2 4 5 8 1 9 remain
3 3 < 4; TRUE TRUE 8 1 8 4 2 4 5 1 8 9 swap
4 4 < 4; FALSE 2 2 5 4 1 8 9 unsorted
2 TRUE 0 0 < 3; TRUE FALSE 1 2 4 5 1 8 9 remain
1 1 < 3; TRUE FALSE 2 2 4 5 1 8 9 remain
2 2 < 3; TRUE TRUE 5 1 5 3 2 4 1 5 8 9 swap
3 3 < 3; FALSE 3 2 4 1 5 8 9 unsorted
3 TRUE 0 0 < 2; TRUE FALSE 1 2 4 1 5 8 9 remain
1 1 < 2; TRUE TRUE 4 1 4 2 2 1 4 5 8 9 swap
2 2 < 2; FALSE 4 2 1 4 5 8 9 unsorted
4 TRUE 0 0 < 1; TRUE TRUE 2 1 2 1 1 2 4 5 8 9 swap
1 1 < 1; FALSE 5 1 2 4 5 8 9 sorted!
5 FALSE 1 2 4 5 8 9 sorted!
Characteristics:
 About twice as fast as the bubble sort.
 Not too complex, although it’s slightly more
involved than the bubble sort.
 Sort a list of values by repetitively inserting a
particular value into a subset of the list that
has already been sorted.
 One at a time, each unsorted element is
inserted at the appropriate position in that
sorted subset until the entire list is in order.
Strategy:
 Sort the first two values in the list relative to each
other by exchanging them if necessary.
 Insert the third list value into the position relative to
the first two (sorted) values.
 Then insert the fourth value into its proper position
relative to the first three values in the list.
◦ Each time an insertion is made, the number of values in the
sorted subset is increases by one.
 Continue this process until all values in the list are
completely sorted.
 The insertion process requires that the other values
in the list shift to make room for the inserted
element.
 Step 1, list = {2}:
◦ Initially, the sorted sublist contains the first element
in the list. Insert 9 into the sublist.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 2 9 5 4 8 1
1 2 9 5 4 8 1
2
3
4
5
6
 Step 2, list = {2, 9}:
◦ The sorted sublist is {2, 9}. Insert 5 into the sublist.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 2 9 5 4 8 1
1
2 2 9 5 4 8 1
3
4
5
6
 Step 3, list = {2, 5, 9}:
◦ The sorted sublist is {2, 5, 9}. Insert 4 into the
sublist.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 2 5 9 4 8 1
1
2
3 2 5 9 4 8 1
4
5
6
 Step 4, list = {2, 4, 5, 9}:
◦ The sorted sublist is {2, 4, 5, 9}. Insert 8 into the
sublist.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 2 4 5 9 8 1
1
2
3
4 2 4 5 9 8 1
5
6
 Step 5, list = {2, 4, 5, 8, 9}:
◦ The sorted sublist is {2, 4, 5, 8, 9}. Insert 1 into the
sublist.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 2 4 5 8 9 1
1
2
3
4
5 2 4 5 8 9 1
6
 Step 6, list = {1, 2, 4, 5, 8, 9}:
◦ The entire list is now sorted. List = {1, 2, 4, 5, 8, 9}.

Index (i) [0] [1] [2] [3] [4] [5]


Step (n) 1 2 4 5 8 9
1
2
3
4
5
6 1 2 4 5 8 9
 After 6th step, the list is sorted!

1 2 4 5 8 9
Searching:
 Searching is the process of finding a designated
target element within a group of items, or that
the target does not exist within the group.
◦ Searching is the task of determining whether a
collection/list contains some particular element.
 The group of items to be searched is sometimes
called the search pool.
 E.g. Binary Search, Linear Search, Binary Search
Tree, Hashing, Radix Search, External Search, etc.
 Binary search algorithm is one of the common
search approaches for a list of values.
 A binary search algorithm eliminates large
parts of the search pool with each
comparison by capitalizing on the fact that
the search pool is in sorted.
◦ For a binary search to work, the elements in the list
must already be ordered (sorted).
 Sort the list (assume list is sorted in ascending order)
 Searching for the key begins in the middle of the
element/list.
◦ If the key is less than the middle element, search continue
in the first half of the list.
 Eliminate the second half of the list.
 The search continues in the same manner as in the beginning of
the search (examining the middle).
◦ If the key is equal to the middle element, the search ends
with a match.
◦ If the key is greater than the middle element, search
continue in the second half of the list.
 Eliminate the first half of the list.
 The search continues in the same manner as in the beginning of
the search (examining the middle).
 Each comparison reduces the viable
candidates by half until eventually the target
element is found or there are no more viable
candidates, which means the target element
is not in the search pool.

1st Round

 1st round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 0
◦ high = 5
◦ mid = (low + high) / 2 → (0 + 5) / 2 = 2

2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 1st round
◦ Key to be searched: 2

◦ list.length = 6
◦ low = 0
◦ high = 5
◦ mid = 2

2nd Round
 2nd round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 0
◦ high = 1
◦ mid = (low + high) / 2 → (0 + 1) / 2 = 0
2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 2nd round
◦ Key to be searched: 2
◦ list.length = 6

◦ low = 0
◦ high = 1
◦ mid = 0 Found!

 3rd round
◦ Key to be searched: 2
◦ list.length = 6
◦ low = 1
◦ high = 1
◦ mid = (low + high) / 2 → (1 + 1) / 2 = 1
2
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

mid
Found!
low high

 1st round 1st Round


◦ Key to be searched: 5
◦ list.length = 6
◦ low = 0
◦ high = 5
◦ mid = (low + high) / 2 → (0 + 5) / 2 = 2

5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 1st round
◦ Key to be searched: 5

◦ list.length = 6
◦ low = 0 2nd Round
◦ high = 5
◦ mid = 2

 2nd round
◦ Key to be searched: 5
◦ list.length = 6
◦ low = 3
◦ high = 5
◦ mid = (low + high) / 2 → (3 + 5) / 2 = 4
5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 2nd round
◦ Key to be searched: 5
◦ list.length = 6

◦ low = 3
◦ high = 5
◦ mid = 4 Found!

 3rd round
◦ Key to be searched: 5
◦ list.length = 6
◦ low = 3
◦ high = 3
◦ mid = (low + high) / 2 → (3 + 3) / 2 = 3
5
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

mid
Found!
low high

 1st round 1st Round


◦ Key to be searched: 7
◦ list.length = 6
◦ low = 0
◦ high = 5
◦ mid = (low + high) / 2 → (0 + 5) / 2 = 2

7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 1st round
◦ Key to be searched: 7

◦ list.length = 6
◦ low = 0 2nd Round
◦ high = 5
◦ mid = 2

 2nd round
◦ Key to be searched: 7
◦ list.length = 6
◦ low = 3
◦ high = 5
◦ mid = (low + high) / 2 → (3 + 5) / 2 = 4

7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

low mid high


 2nd round
◦ Key to be searched: 7
◦ list.length = 6

◦ low = 3
◦ high = 5
◦ mid = 4
3rd Round
 3rd round
◦ Key to be searched: 7
◦ list.length = 6
◦ low = 3
◦ high = 3
◦ mid = (low + high) / 2 → (3 + 3) / 2 = 3

7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

mid
low high
 3rd round
◦ Key to be searched: 7 α
◦ list.length = 6
◦ low = 3
◦ high = 3
◦ mid = 3
Not
Found!
 4th round
◦ Key to be searched: 7
◦ list.length = 6
◦ low = 4
◦ high = 3
◦ mid = 3

7
[0] [1] [2] [3] [4] [5]
1 2 4 5 8 9

high >= low


mid is
high low FALSE, then
return -1
 Generic Class is a general class which allows
us to define a set of operations that
manipulate objects of a particular class,
without specifying the class of the objects
being manipulated until a later time.
 In the other word, Generic Class allows the
same organisation and operations on
different ADT’s .
 The key benefit of generics is to enable errors
to be detected at compile time rather than at
runtime.
ArrayList<String> list1 = new
ArrayList<String>();

ArrayList<Student> list2 = new


ArrayList<Student>();
ArrayList<String> list = new ArrayList<String>();

list.add("Red");
list.add("White");
list.add("White");
String s = list.get(0);
System.out.println(s);

Output: ?
ArrayList<Integer> list = new
ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
int i = list.get(0);
System.out.println(i);

Output: ?
ArrayList<Double> list = new
ArrayList<Double>();
list.add(1.0);
list.add(2.0);
list.add(3.0);
double i = list.get(0);

System.out.println(i);

Output: ?
Student student1 = new Student("Ali", "CS110");
Student student2 = new Student("Ahmad", "BM111");
ArrayList<Student> listStudent = new
ArrayList<Student>();
listStudent.add(student1);
listStudent.add(student2);
System.out.println(listStudent.get(1));
System.out.println(listStudent.get(0));
Object ob = listStudent.get(1);
System.out.println(ob);

Output: ?
public class Chp1GenericMethod {
public static void main(String[] args)
{
Integer[] integers = {1,2,3,4,5}; Output:
String[] strings = {"London", "Paris", 1
"New York",
"Kuala Lumpur"};
2
3
Chp1GenericMethod.<Integer>print(integers); 4
Chp1GenericMethod.<String>print(strings); 5
--------
}
public static <E> void print(E[] list){ London
for (int i=0; i<list.length; i++) Paris
System.out.println(list[i] + " "); New York
System.out.println("--------"); Kuala
} Lumpur
}
 Topic 1
End of Topic 1

Question?

You might also like