Lab-9 Abstract Data Types
Lab-9 Abstract Data Types
Lab#9
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.
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.
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
• remove(Object o) – Remove the first occurrence of the specified element from the list.
• remove(int index) – Removes the element at the specified index from the list.
The ArrayList and LinkedList classes provide the implementation of List interface. Let's see the
examples to create the List:
Let's see a simple example of List where we are using the ArrayList class as the implementation.
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");
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));
//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>();
//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());
}
Output: 4
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
• pop() – Removes an element from the top of the stack if it is not empty.
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.
import java.util.Stack;
class Use
{
// Pushing element on the top of the stack
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);
stack_search(S, 2);
stack_search(S, 6);
} }
Java library provides the below following operations which can be performed on java.util.Queue
• offer(E e) – Inserts the specified element into this queue if it is possible to do without
violating capacity restrictions.
import java.util.LinkedList;
import java.util.Queue;
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.