1: Collections
Q1) Program to demonstrate ListIterator
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
// Create ArrayList object with capacity of 2 elements
ArrayList al = new ArrayList(2);
[Link](al+", size = "+[Link]());
// Add items to the ArrayList
[Link]("R");
[Link]("U");
[Link]("O");
[Link](new String("x"));
[Link](2, new Integer(10));
[Link](al+", size = " + [Link]());
// Remove item
[Link]("U");
[Link](al+", size = " + [Link]());
// Check if the list contains the specified element
Boolean b = [Link]("x");
[Link]("The list contains x = " + b);
b = [Link]("p");
[Link]("The list contains p = " + b);
b = [Link](new Integer(10));
[Link]("The list contains Integer of 10 = " + b);
// Create ListIterator and iterate entries in it
ListIterator li = [Link]();
while ([Link]())
[Link]("From ListIterator = " + [Link]());
// Create Object array from ArrayList
Object a[] = [Link]();
for (int i=0; i<[Link]; i++)
[Link]("From an Array = " + a[i]);
}
}
Q2) Program to demonstrate Iterator
import [Link];
import [Link];
public class Test
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
for (int i = 0; i< 10; i++)
[Link](i);
[Link](al);
// at beginning itr(cursor) will point to
// index just before the first element in al
Iterator itr = [Link]();
// checking the next element availabilty
while ([Link]())
{
// moving cursor to next element
int i = (Integer)[Link]();
// getting even elements one by one
[Link](i + " ");
// Removing odd elements
if (i % 2 != 0)
[Link]();
}
[Link]();
[Link](al);
}
}
2: Multithreading
Q1) Demonstrating Inter-thread communication of a Thread
class SampleThread extends Thread
{
int tBal = 0;
public void run()
{
synchronized (this)
{
[Link]("Thread calculation for total balance");
for (int i = 0; i <= 30; i++)
{
tBal = tBal + i;
}
[Link]("Thread gives notification call");
[Link]();
}
}
}
public class DemoThread
{
public static void main(String[] args) throws InterruptedException
{
SampleThread st = new SampleThread ();
[Link]();
synchronized (st)
{
[Link]("Thread calling wait() Method");
[Link]();
[Link]("Thread got notification");
[Link]("Totol Balance " + [Link]);
}
}
}
Q2) Demonstrating Priority of a Thread
class PriorityDemo extends Thread
{
public void run()
{
[Link]("running thread name
is:"+[Link]().getName());
[Link]("running thread priority
is:"+[Link]().getPriority());
}
public static void main(String args[])
{
PriorityDemo m1=new PriorityDemo();
PriorityDemo m2=new PriorityDemo();
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}
class First
{
synchronized public void display(String msg)
{
[Link] ("["+msg);
try
{
[Link](1000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link] ("]");
}
}
class Second extends Thread
{
String msg;
First fobj;
Second (First fp,String str)
{
fobj = fp;
msg = str;
start();
}
public void run()
{
[Link](msg);
}
}
public class MyThread
{
public static void main (String[] args)
{
First fnew = new First();
Second ss = new Second(fnew, "welcome");
Second ss1= new Second(fnew,"new");
Second ss2 = new Second(fnew, "programmer");
}
}