Object Oriented Programming: Unit IV: I/O, Generics, String Handling

Generic Classes

with Example Java Programs

A generic class contains one or more variables of generic data type. Following is a simple example which shows how to define the generic class.

Generic Classes

A generic class contains one or more variables of generic data type. Following is a simple example which shows how to define the generic class.

public class Test<T>

{

public Test(){val=null;}

public Test(T val)

{

this.val=val;

}

public getVal()

{

return val;

}

public setVal()

{

val=newValue;

}

private T val; //variable defined as of genric type

}

The concept of generic class supports the idea of type-independency. Typical example of data structure is stack. We can create a generic class for stack which allows us to insert integer, character or any other data type elements using the same push and pop methods.

Ex. 4.6.1: Create a generic class for the stack data structure. Your class must handle integer and character type elements. Show clearly how will you handle the stack empty condition

Sol. We will create a generic class for stack data structure using following steps -

Step 1: Create a Java file named Stack.java as follows-

Java Program[Stack.java]

import java.util.*; //Supports the ArrayList

public class Stack<T> //T denotes any data type

{

public ArrayList<T> obj;

public Stack(int size) //Constructor will be invoked from main

{

obj=new ArrayList<T>(size);

}

public void push(T item) //Generic method for PUSH operation

{

obj.add(item);

}

public T pop()//Generic method for POP operation

{

if(obj.isEmpty())

{

System.out.println("\n Stack is Empty");

return null;

}

return obj.remove(obj.size()-1);

}

}

Step 2: Create another Java program in a separate file named StackGeneric.java. It is as given

below -

Java Program[StackGeneric.java]

import java.io.*;

import java.util.*;

public class StackGeneric

{

public static void main(String[] args)

{

int[] iArray={1,2,3,4,5};

char[] cArray = {'A', 'B', 'C', 'D','E'};

Declaring integer and character values to be pushed onto the stack.

ist is an instance for integer stack and cst is an instance for character stack.

The Constructor Stack(size) will be invoked.

Stack<Integer> ist=new Stack<Integer>(5);

Stack<Character> cst=new Stack<Character>(5);

System.out.println("\n Pushing the elements in integer stack");

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

ist.push(iArray[i]);

System.out.println("\n Pushing the elements in character stack");

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

cst.push(cArray[i]);

System.out.println("\n Popping two elements from character stack");

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

System.out.printf("\n%c", cst.pop());-

System.out.println("\n Popping all the elements from integer stack");

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

System.out.printf("\n%d", ist.pop());

System.out.println("\n Performing one more pop for integer stack");

System.out.printf("\n%d",ist.pop());.

}

}

Output

F:\>javac StackGeneric.java

F:\>java StackGeneric

Pushing the elements in integer stack

Pushing the elements in character stack

Popping two elements from character stack

E

D

Popping all the elements from integer stack

5

4

3

2

1

Performing one more pop for integer stack

Stack is Empty

null                                                                      

Program Explanation

In the step 1 we have created a separate Java file, in which a class is written for defining the generic methods push and pop. Note that it is necessary to define the constructor for this class because it will then allow to initialize the class of appropriate data type.

In step 2 we are first creating the separate instances for each of these classes say cst and ist. Then using these instances the generic push and pop methods will be invoked.

The output given in the step 2 is self explanatory.

Ex. 4.6.2 Using generic classes, write a program to perform the following operations on an array i) Add an element in the beginning/middle/end ii) Delete an element from a given position

Sol.:

import java.io.*;

import java.util.*; //Supports the ArrayList

class Arr<T> //T denotes any data type

{

public ArrayList<T> obj;

public Arr(int size) //Constructor will be invoked from main

{

obj=new ArrayList<T>(size);

}

public void insert(int index,T item) //Generic method for insert Operation

{

obj.add(index,item);

}

public void display()

{

System.out.print(" "+obj);

}

public T del(int index)//Generic method for delete Operation

{

return obj.remove(index);

}

}

public class ArrayGeneric

{

public static void main(String[] args)

{

int[] iArray={1,2,3,4,5};

Arr<Integer> iobj=new Arr<Integer>(10);

int i,index;

System.out.println("\n Array of integers is ...");

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

iobj.insert(i,iArray[i]);

iobj.display();

System.out.println("\n Inserting the elements in integer Array");

System.out.println("Enter the element to be inserted: ");

Scanner sc=new Scanner(System.in);

int item = sc.nextInt();

System.out.println("Enter the index at which the element is to be inserted: ");

index = sc.nextInt();

iobj.insert(index,item);

iobj.display();

System.out.println("\n Enter the index of the element to be deleted: ");

index = sc.nextInt();

iobj.del(index);

iobj.display();

double[] dArray={11.11,22.22,33.33,44.44,55.55};

Arr<Double> dobj =new Arr<Double>(10);.

System.out.println("\n Array of doubles is ...");

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

dobj.insert(i,dArray[i]);

dobj.display();

System.out.println("\n Inserting the elements in double Array");

System.out.println("Enter the element to be inserted: ");

sc=new Scanner(System.in);

double ditem = sc.nextDouble();

System.out.println("Enter the index at which the element is to be inserted: ");

index = sc.nextInt();

dobj.insert(index, ditem);

dobj.display();

System.out.println("\n Enter the index of the element to be deleted: ");

index= sc.nextInt();

dobj.del(index);

dobj.display();

}

}

Output

Array of integers is.

[1, 2, 3, 4, 5]

Inserting the elements in integer Array

Enter the element to be inserted:

100

Enter the index at which the element is to be inserted:

2

 [1, 2, 100, 3, 4, 5]

Enter the index of the element to be deleted:

4

[1, 2, 100, 3, 5]

Array of doubles is ...

[11.11, 22.22, 33.33, 44.44, 55.55]

Inserting the elements in double Array

Enter the element to be inserted:

111.222

Enter the index at which the element is to be inserted:

3

 [11.11, 22.22, 33.33, 111.222, 44.44, 55.55]

Enter the index of the element to be deleted:

2

[11.11, 22.22, 111.222, 44.44, 55.55]

Review Questions

1.Explain the generic classes and generic methods with example.

2. Explain in detail about generic classes and methods in java with suitable example.

3. Develop a Java program that will illustrate the use of Generic classes. Give self explanatory comments in your program

Object Oriented Programming: Unit IV: I/O, Generics, String Handling : Tag: : with Example Java Programs - Generic Classes