/* Understand the Generic class
* Write a Java Program to demonstrate a Generic Class
* Author: Vikram
* rollno:
* div:
* */ Solution
public class GenericClassExample <T>{
private T dvariable;
public T getDvariable() {
return dvariable;
}
public void setDvariable(T dvariable) {
this.dvariable = dvariable;
}
public static void main(String []args)
{
GenericClassExample<Integer> intvar=new GenericClassExample<>(); intvar.setDvariable(10);
System.out.println(“Variable value=“+intvar.getDvariable());
GenericClassExample<String> strvar=new GenericClassExample<>(); strvar.setDvariable(“Sukhiram”);
System.out.println(“Variable value=“+strvar.getDvariable());
}
}
/*
* Practical b1
* Write a program in java that will use Generic methods
* There are following kind of Generic Types
* T Type
* E Element
* k Key
* v Value
* N Number
*/
public class GenericMethod1 {
//print array is generic method
public static <E> void printArray(E[] inputArray)
{
//display array element
for(E element:inputArray)
{
System.out.printf(“%s,”,element);
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub Integer [] intArray= {1,2,3,4,5,6};
Double [] doubleArray= {1.45,2.35,4.63,8.56}; Character [] charArray= {‘H’,’E’,’L’,’L’,’0’}; printArray(intArray);
printArray(doubleArray); printArray(charArray);
}
}
/*
* Write a program in java that will use Generic methods
* There are following kind of Generic Types
* T Type
* E Element
* k Key
* v Value
* N Number
* understand the use of k,v types
* S,V
*/
class Pair<K,V>
{
private K key; private V value;
public Pair(K key, V value)
{
this.key = key;
this.value = value;
}
public K getKey()
{return key;}
public void setKey(K key)
{this.key = key;}
public V getValue()
{return value;}
public void setValue(V value)
{this.value = value;}
public class GenericMethod2 {
public static <K,V> boolean compare(Pair<K,V>p1,Pair<K,V>p2) {return p1.getKey().equals(p2.getKey()) &&
p1.getValue().equals(p2.getValue());
};
public static void main(String[] args) {
// TODO Auto-generated method stub Pair<Integer,String>p1=new Pair<>(1,”aaa”); Pair<Integer,String>p2=new Pair<>(2,”bbb”); boolean result=GenericMethod2.compare(p1, p2); System.out.println(result);
}
}
Write a program to demonstrate unbound Wildcard in List Interface
import java.util.ArrayList; import java.util.Iterator; import java.util.List;
/*
* Write a program in java to understand the use
* of unbound wildcards ?
*/
public class unboundWildcardExample {
public static void printArray(List<?> mylist)
{
Iterator itr=mylist.iterator(); while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println(“ “);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList <Integer>mynumberlist=new ArrayList<Integer>(); mynumberlist.add(1);
mynumberlist.add(2); mynumberlist.add(3); printArray(mynumberlist);
ArrayList <String>mynamelist=new ArrayList<String>(); mynamelist.add(“Suresh”);
mynamelist.add(“Ramesh”); mynamelist.add(“Jayesh”); mynamelist.add(“Mahesh”); printArray(mynamelist);
}
}
Write a program to demonstrate upper Wildcard in List Interface
import java.util.ArrayList; import java.util.Iterator; import java.util.List;
/*
* Write a program in java to understand the use
* of upperbound wildcards ? extends
*/
public class upperboundWildcardExample {
public static void sumofElements(List<? extends Number> numberlist)
{
for (Number n : numberlist) sum += n.doubleValue(); System.out.println(“Sum of all elements is=“+sum);
}
public static void main(String []args) {
ArrayList <Integer>intlist=new ArrayList<Integer>(); intlist.add(1);
intlist.add(2); intlist.add(3); sumofElements(intlist);
ArrayList <Double>dblist=new ArrayList<Double>(); dblist.add(24.56);
dblist.add(21.24); dblist.add(124.16); dblist.add(121.14); sumofElements(dblist);
}
}