Vectors

2 comments - Post a comment

/* Implementation Of Vectors */
import java.util.Vector;
import java.io.*;
public class MainClass extends Thread{
public static void main(String args[]) throws InterruptedException{
Vector v = new Vector();
int num,choice,ch;
boolean tr = true;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
do {
System.out.println("1. Add Elements\n2. Delete Elements\n3. Display\n4. Exit");
choice = Integer.parseInt(br.readLine());
switch(choice) {
case 1:
System.out.println("Enter Element : ");
v.add(Integer.parseInt(br.readLine()));
break;
case 2:
int no;
System.out.println("Enter Element : ");
no = Integer.parseInt(br.readLine());
if(v.contains(no))
v.removeElement(no);
break;
case 3:
System.out.println(v);
break;
case 4:
System.out.println("Exiting Application....");
Thread.sleep(2000);
System.exit(0);
break;
default:
System.out.println("ERROR!!! INVALID OPTION!!!");
Thread.sleep(2000);
}
System.out.println("Do YOu want to continue (1 = yes: )");
ch = Integer.parseInt(br.readLine()); }
while(ch==1);
} catch(Exception e){
System.out.println("Error Message: "+e);
}
}}
/**** OutPut ******
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
4
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
6
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
7
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
5
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
3
[4, 6, 7, 5]
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
2
Enter Element :
7
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
3
[4, 6, 5]
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
4
Exiting Application.... */

 

Threads

2 comments - Post a comment

/* Implementation Of Threads */

import java.io.*;
import java.util.*;

class AA extends Thread
{
int i;
public void run()
{
System.out.println("Thread A");
for(i=0;i<5;i++)
{
System.out.println("Thread A : "+i);
yield();
}
}
}
class B extends Thread
{
int i;
public void run()
{
System.out.println("Thread B");
for(i=0;i<5;i++)
{
System.out.println("Thread B : "+i);
yield();
}
}

}
class C extends Thread
{
int i;
public void run()
{
System.out.println("Thread C");
for(i=0;i<5;i++)
{
System.out.println("Thread C : "+i);
yield();
}
}
}
class ThreadLionel
{
public static void main(String []args)
{
AA a = new AA();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}
/*
--------------------OUTPUT--------------------
Thread A
Thread A : 0
Thread A : 1
Thread B
Thread B : 0
Thread C
Thread C : 0
Thread A : 2
Thread B : 1
Thread C : 1
Thread A : 3
Thread B : 2
Thread C : 2
Thread A : 4
Thread B : 3
Thread C : 3
Thread B : 4
Thread C : 4
*/