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

Lab # 9 SSUET/QR/114

Lab#9
Abstract Data Types

OBJECTIVE: Understanding and implementing abstract data types.

1. Introduction to ADT:
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented. It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It is called “abstract” because it
gives an implementation-independent view. The process of providing only the essentials and
hiding the details is known as abstraction.
The user of data type does not need to know how that data type is implemented, for example, we
have been using Primitive values like int, float, char data types only with the knowledge that these
data type can operate and be performed on without any idea of how they are implemented. So a
user only needs to know what a data type can do, but not how it will be implemented. An ADT
does not specify how the data type is implemented. These implementation details are hidden from
the user of the ADT and protected from outside access, a concept referred to as Encapsulation.

2. Operations of ADT
The operations of an abstract data type are classified as follows:

• Creators create new objects of the type. A creator may take an object as an argument, but
not an object of the type being constructed.

• Producers create new objects from old objects of the type. The concat method of String,
for example, is a producer. It takes two strings and produces a new one representing their
concatenation.

• Observers take objects of the abstract type and return objects of a different type. The size
method of List, for example, returns an int.

• Mutators change objects. The add method of List, for example, mutates a list by adding
an element to the end.

3. ADTs in Java
Java library has Abstract Data Types such as List, Stack, Queue, Set, Map as inbuilt interfaces
which are being implemented using various data structures.

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

In Java, Abstract Data Types extend the Collections Interface which represents the data type. It is
part of the Java Collections framework and is the root interface in the collection hierarchy. A
collection represents a group of objects, known as its elements.

The JDK does not provide any direct implementations of this interface. It provides
implementations of more specific sub interfaces like List, Set. This interface is typically used to
pass collections around and manipulate them where maximum generality is desired.

3.1 List ADT


List Abstract Data Type is a collection of elements that have a linear relationship with each other.
A linear relationship means that, except for the first one, each element on the list has a unique
successor. Also lists have a property intuitively called size, which is simply the number of elements
on the list.

List is mutable. List is also an interface, which means that other classes provide the actual
implementation of the data type. These classes include ArrayList which is implemented internally
using Arrays and LinkedList which is implemented internally using LinkedList data structure.

Java library’s List interface specifies 25 different operations/methods and some of the methods are
as follows

• get(int index) – Returns an element at a particular index from the list.

• add(E e) – Appends the specified element to the end of this list.

• remove(Object o) – Remove the first occurrence of the specified element from the list.

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

• remove(int index) – Removes the element at the specified index from the list.

• size() – Returns number of elements of the list.

• isEmpty() – Return true if the list is empty, else return false.

How to create List?

The ArrayList and LinkedList classes provide the implementation of List interface. Let's see the
examples to create the List:

//Creating a List of type String using ArrayList

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

//Creating a List of type Integer using ArrayList

List<Integer> list=new ArrayList<Integer>();

//Creating a List of type String using LinkedList

List<String> list=new LinkedList<String>();

Java List Example

Let's see a simple example of List where we are using the ArrayList class as the implementation.

Create an ArrayList object called cars that will store strings:

List <String> cars = new ArrayList<String>();

Add Items:
The ArrayList class has many useful methods. For example, to add elements to the ArrayList, use
the add() method:

import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List <String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

cars.add("Mazda");
System.out.println(cars);
}
}

Output:

Access an Item:
To access an element in the ArrayList, use the get() method and refer to the index number:
System.out.println(cars.get(0));
System.out.println(cars.get(3));

public class main {


public static void main(String[] args) {
List <String> cars = new ArrayList<String>();
//Add items
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Toyota");
System.out.println(cars);

//Access items
System.out.println(cars.get(0));
System.out.println(cars.get(3));

Output:

Change an Item:
To modify an element, use the set() method and refer to the index number:
public class main {
public static void main(String[] args) {
List<String> cars = new ArrayList<String>();

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

//Add items
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Toyota");
//Change items
cars.set(0, "Honda");
System.out.println(cars);}

Output:

Remove an Item:
To remove an element, use the remove() method and refer to the index number:
cars.remove(0);
System.out.println(cars);
Output:

ArrayList Size:
To find out how many elements an ArrayList have, use the size method:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List <String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.size());

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

}
Output: 4

3.2 Stack ADT:


Stack ADT is a collection with homogeneous data items (elements), in which all insertions and
deletions occur at one end, called the top of the stack. A stack is a LIFO “Last In, First Out”
structure. Analogy to Stack is a stack of plates.

Stacks are managed mainly using two functions like below

• PUSH – places an element on top of the stack.

• POP – removes an element from the stack.

In Java, Stack ADT class extends the Vector class which is a growable array of Objects and it can
be accessed using integer index.

Java library provides the below following operations which can be performed on java.util.Stack

• push(E e) – Inserts an element at the top of stack.

• pop() – Removes an element from the top of the stack if it is not empty.

• peek() – Returns the top element of stack without removing it.

• size() – Returns the size of the stack.

• isEmpty() – Returns true if the stack is empty, else it returns false.

How to Create a Stack?

In order to create a stack, we must import java.util.stack package and use the Stack() constructor
of this class. The below example creates an empty Stack.

Stack<E> stack = new Stack<E>();

Java Stack Example

import java.util.Stack;
class Use
{
// Pushing element on the top of the stack

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

static void stack_push(Stack<Integer> S)


{
for(int i = 0; i < 5; i++)
{
S.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack<Integer> S)
{
System.out.println("Pop Operation:");

for(int i = 0; i < 5; i++)


{
Integer y = (Integer) S.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack<Integer> S)
{
Integer element = (Integer) S.peek();
System.out.println("Element on stack top: " + element);
}

// Searching element in the stack


static void stack_search(Stack<Integer> S, int element)
{
Integer pos = (Integer) S.search(element);

if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: "
+ pos);
}
public static void main (String[] args)
{
Stack<Integer> S = new Stack<Integer>();
stack_push(S);
stack_pop(S);
stack_push(S);
stack_peek(S);

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

stack_search(S, 2);
stack_search(S, 6);
} }

3.3 Queue ADT


Queue ADT is a collection in which the elements of the same type are arranged sequentially. The
operations can be performed at both ends with insertion being done at rear end deletion being done
at the front end for a single ended queue. Theoretically, Queue is a FIFO “First In, First Out”
structure.

Java data structures like java.util.LinkedList, java.util.concurrent.ArrayBlockingQueue


implement Queue ADT using LinkedList and ArrayLists internally respectively.

Java library provides the below following operations which can be performed on java.util.Queue

• add(E e) – Queues an element at the end of queue.

• remove() – Dequeues an element from the head of the queue.

• peek() – Returns the element of the queue without removing it.

• offer(E e) – Inserts the specified element into this queue if it is possible to do without
violating capacity restrictions.

• size() – Returns the size of the queue.

Java Queue Example

import java.util.LinkedList;
import java.util.Queue;

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

public class main {


public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<Integer>();

// Adds elements {0, 1, 2, 3, 4} to the queue


for (int i = 0; i < 5; i++)
{
q.add(i);
}
// Display contents of the queue.
System.out.println("Elements of queue " + q);

// To remove the head of queue.


int removedelement = q.remove();
System.out.println("removed element:" + removedelement);

System.out.println("updated queue:" +q);

// To view the head of queue


int head = q.peek();
System.out.println("head of queue:" + head);

// To check size of Queue


int size = q.size();
System.out.println("Size of queue:"+ size);
}

4. Which Java ADT to choose?


In this section, we will discuss on the scenarios to choose between either of List, Stack and Queue
ADT.

SWE-312 Software Construction and Development


Lab # 9 SSUET/QR/114

As List ADT is a collections of elements stored sequentially and can be accessed using their
indexes, it needs to be chosen in cases which involves sequential or indexed access or removal of
elements. For example, various implementations of List ADT can be used to store data of a list of
students in a sorted order for an ordered or indexed access or removal.

Since Stack is a Last In First data structure, the implementations of Stack ADT need to be chosen
in scenarios where the most recently inserted elements need to be accessible first. One of the
common example where this kind of LIFO data structure is required is the function call stack of
every programming language where the most recent function in the stack needs to be executed
first.

Queue is a First In First Out structure and data structures implementing Queue ADT need to be
chosen in scenarios where the elements need to be accessed in their order of insertion i.e where
fairness needs to be ensured. Example of one such scenario is request handling by web servers.
Web servers facilitate fairness of request handling according to their order of arrival by
maintaining an internal queue for the requests.

LAB TASK:
Considering a bank interface, it will have different concrete classes for receiving bill payments,
opening of new accounts and contacting those loan takers whose limits of extension have been
expired. Now, develop this scenario on the described ADT and decide which ADT should be used
for each concrete class, also implement the whole scenario in multiple java classes.

SWE-312 Software Construction and Development

You might also like