1)Write a program to illustrate the concept of class with method overloading.
/*Program to implement method overloading*/
class Test
{
void sum(int x, int y)
{
[Link]("The sum is "+(x+y));
}
void sum(int x, int y, int z)
{
[Link]("The sum is "+(x+y+z));
}
void sum(double x, double y)
{
[Link]("The sum is "+(x+y));
}
void sum(double x, double y, double z)
{
[Link]("The sum is "+(x+y+z));
}
}
class OverLoad
{
public static void main(String args[])
{
Test t = new Test();
[Link](10, 20);
[Link](30, 40, 50);
[Link](10.4, 20.6);
[Link](10.9, 11.3, 12.8);
}
}
Output :
2) Write a java program that reads a line of integer and then displays
each integer and sum of all integers(Using String Tokenizer class
of [Link]).
/*Display each integer and sum of integers using String Tokenizer*/
import [Link].*;
public class StrToken
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the integer string : ");
String num = [Link]();
int sum = 0;
StringTokenizer st = new StringTokenizer(num, " ");
[Link]("Number of integer tokens :
"+[Link]());
while([Link]())
{
int val = [Link]([Link]());
sum = sum + val;
[Link](val);
}
[Link]("Sum : "+sum);
}
}
Output :
3)Write a java program to illustrate the concept of Single level and multilevel
inheritance.
/*Single level and Multilevel inheritance*/
class One
{
String s1 = "Class One";
void displayOne()
{
[Link]("Parent : "+s1);
}
}
class Two extends One
{
String s2 = "Class Two";
void displayTwo()
{
[Link]();
[Link]("Single level Inheritance");
[Link]("Child "+s2+"inherited from "+s1);
}
}
class Three extends Two
{
String s3 = "Class Three";
void displayThree()
{
[Link]();
[Link]("Multi level Inheritance");
[Link]("Child "+s3+"inherited from "+s2+"and"+s1);
}
}
class SingleMult
{
public static void main(String args[])
{
Three obj = new Three();
[Link]();
}
}
Output :
4)Write a java program to demonstrate the Interfaces and Abstract Classes.
/*Interface and abstract classes*/
abstract class Demo
{
abstract void show();
}
interface Printable
{
void print();
}
class AbstractClassdemo extends Demo implements Printable
{
void show()
{
[Link]("This is Abstract class Method");
}
public void print()
{
[Link]("This is interface method");
}
}
public class InterAbstr
{
public static void main(String args[])
{
AbstractClassdemo ac = new AbstractClassdemo();
[Link]();
[Link]();
}
}
Output :
5)Write a java program to implement the concept of exception handling.
/*Program to implement exception handling*/
import [Link];
import [Link];
public class Exception
{
public static void main(String args[])
{
int arr[] = {15, 20, 33, 40, 55};
Scanner sc = new Scanner([Link]);
int ch = 0, a, b, res;
while(ch != -1)
{
[Link]("Enter the index of array(-1 to quit):");
try
{
ch = [Link]();
[Link](arr[ch]);
[Link]("Enter two integer : ");
a = [Link]();
b = [Link]();
[Link]("Division result : "+(a/b));
}
catch(ArrayIndexOutOfBoundsException aie)
{
[Link]("Index value is out of bounds");
}
catch(InputMismatchException ime)
{
[Link]("Entered value is not valid,
characters and decimal values are not allowed");
[Link]();
}
catch(ArithmeticException ae)
{
[Link]("Denominator should not be
zero");
}
}
}
}
Output :
6)Write a java program to illustrate the concept of threading using the Thread
class and Runnable interface.
/*Program to illustrate the concept of threading using the thread class and
runnable interface*/
class One extends Thread
{
public void run()
{
int sum = 0;
String name = [Link]().getName();
long id = [Link]().getId();
[Link]("Executed thread = "+name+",Id = "+id);
for(int i = 10; i < 15; i++)
{
sum = sum + i;
[Link](i+"+");
}
[Link]("Sum = "+sum);
}
}
class Two implements Runnable
{
public void run()
{
int sum = 0;
String name = [Link]().getName();
long id = [Link]().getId();;
[Link]("Executed thread = "+name+",Id = "+id);
for(int i = 20; i < 25; i++)
{
sum = sum + i;
[Link](i+"+");
}
[Link]("Sum = "+sum);
}
}
public class ThreadInter
{
public static void main(String args[])throws InterruptedException
{
One obj1 = new One();
Two t = new Two();
Thread obj2 = new Thread(t);
[Link]();
[Link]();
[Link]();
}
}
Output :
7)Write a java program to implement the concept of thread synchronisation.
/*Implement the concept of thread synchronization*/
class Resource
{
synchronized public void m1()
{
String msg[] =
{"This","is","an","example","for","synchronized","thread"};
String str = [Link]().getName();
for(int i = 0; i < [Link]; i++)
{
[Link](str + " : " + msg[i]);
try
{
[Link](250);
}
catch(InterruptedException e){}
}
}
}
class T2 extends Thread
{
Resource r;
public T2(Resource r, String name)
{
super(name);
this.r = r;
}
public void run()
{
r.m1();
}
}
public class ThreadSync
{
public static void main(String args[])
{
Resource r_obj = new Resource();
T2 t1 = new T2(r_obj, "First");
T2 t2 = new T2(r_obj, "Second");
[Link]();
[Link]();
}
}
Output :
8)Write a java program that implements producer consumer problem using the
concept of inter thread communication.
/*Program to implement produce consumer problem using Inter Thread
communication */
class SharedResource
{
int item;
boolean flag = true;
synchronized void putItem(int item)
{
while(flag == false)
{
try
{
wait();
}
catch(InterruptedException e){}
}
[Link] = item;
[Link]("Item placed : " + item);
flag = false;
notify();
}
synchronized int getItem()
{
while(flag == true)
{
try
{
wait();
}
catch(InterruptedException e) {}
}
[Link]("Item consumed : "+item);
flag = true;
notify();
return item;
}
}
class Producer extends Thread
{
SharedResource sr;
Producer(SharedResource sr)
{
[Link] = sr;
start();
}
public void run()
{
int k = 0;
while(true)
{
[Link](k++);
try
{
[Link](500);
}
catch(InterruptedException e) {}
}
}
}
class Consumer extends Thread
{
SharedResource sr;
Consumer(SharedResource sr)
{
[Link] = sr;
start();
}
public void run()
{
while(true)
{
[Link]();
try
{
[Link](500);
}
catch(InterruptedException e) {}
}
}
}
class InterThread
{
public static void main(String args[])
{
SharedResource sr = new SharedResource();
Producer p = new Producer(sr);
Consumer c = new Consumer(sr);
}
}
Output :
9)Write a java program to illustrate collection classes like ArrayList, LinkedList,
TreeMap, HashMap.
(a)ArrayList
/*Collection class ArrayList*/
import [Link].*;
public class Arraylist
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
ArrayList list = new ArrayList();
[Link]("List = " + list);
[Link](10);
[Link](20);
[Link](45.6);
[Link](5.7);
[Link]("abc");
[Link]("xyz");
[Link]("List after insertion is : " + list);
[Link]("Enter a positive index number to fetch an item
: ");
int i = [Link]();
[Link]("Element at " + i + " : " + [Link](i));
[Link]("Enter a positive index to remove item : ");
i = [Link]();
[Link](i);
[Link]("List after removing : " + list);
[Link](i, 100);
[Link]("List after updating value at "+ i + " : " + list);
[Link]();
[Link]("Now list : " + list);
}
}
Output :
(b)LinkedList
/*Linked list in java*/
import [Link].*;
public class Linkedlist
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
LinkedList list = new LinkedList();
[Link]("Linked List is " + list + ",size is " + [Link]());
[Link](10.11);
[Link](7);
[Link](18);
[Link]("MS");
[Link]("LINUX");
[Link](5.9);
[Link]("Linked List is " + list + ",size is " + [Link]());
[Link]("first item");
[Link]("second item");
[Link](3, 50);
[Link]("Linked List after adding elements at first, last,
3 : " + list);
[Link]("Linked List size is " + [Link]());
int i = [Link]();
[Link]("Element at " + i + " is " + [Link](i));
[Link]("Enter element positive index to be removed :
");
i = [Link]();
[Link](i);
[Link]();
[Link]();
[Link]("Linked List after removing " + i + ", first, last
items : " + list + ", size = " + [Link]());
[Link](i, 100);
[Link]("List after updating value at " + i + "is " + list);
[Link]();
[Link]("Now list : " + list + ",size = " + [Link]());
}
}
Output :
(c)TreeMap
/*Tree Map implementation*/
import [Link].*;
public class Treemap
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
TreeMap tm = new TreeMap();
[Link]("TreeMap : " + tm);
[Link](121, "Warner");
[Link](156, "Travis");
[Link](101, "Pujara");
[Link](145 ,"Rahane");
[Link]("TreeMap : " + tm + ", Size : " + [Link]());
[Link]("Enter a key to fetch its values : ");
int key = [Link]();
[Link]("Value for " + key + ":" + [Link](key));
[Link]("First entry in tree map : " + [Link]());
[Link]("Last entry in tree map : " + [Link]());
[Link]("Higher entry than 101 : " +
[Link](101));
[Link]("Lower entry than 145 : " +
[Link](145));
[Link](101, "Shreyas");
[Link]("Tree Map after replacing 101 : " + tm);
[Link](156);
[Link]("Tree Map after removing 156 : " + tm);
[Link]();
[Link]("TreeMap : " + tm + ", Size : " + [Link]());
}
}
Output :
(d)HashMap
/*Hash Map implementation*/
import [Link].*;
public class Hashmap
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
HashMap hm = new HashMap();
[Link]("HashMap : " + hm);
[Link](121, "Warner");
[Link](156, "Travis");
[Link](101, "Pujara");
[Link](145 ,"Rahane");
[Link]("HashMap : " + hm + ", Size : " + [Link]());
[Link]("Enter a key to fetch its values : ");
int key = [Link]();
[Link]("Value for " + key + ":" + [Link](key));
[Link](101, "Shreyas");
[Link]("Hash Map after replacing 101 : " + hm);
[Link](156);
[Link]("Hash Map after removing 156 : " + hm);
[Link]("Enter a key to check if it exists or not : ");
int searchKey = [Link]();
boolean b = [Link](searchKey);
[Link]("Search status : " + b);
[Link]();
[Link]("HashMap : " + hm + ", Size : " + [Link]());
}
}
Output :
10)Write a java program to illustrate Legacy classes like Vector, Hash Table,
Dictionary & Enumeration interface.
(a)Vector
/*Implementation of Vector*/
import [Link].*;
public class VectorDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
Vector v1 = new Vector();
[Link]("Vector = " + v1 + ", Size = " + [Link]());
for(int i = 1; i <= 6; i++)
{
[Link](i);
}
[Link]("Vector1 = " + v1 + ",Size = " + [Link]() +
",capacity = " + [Link]());
[Link]("Vector1 is empty ? " + [Link]());
[Link]("First element = " + [Link]());
[Link]("Last element = " + [Link]());
[Link](100, 5);
[Link]("Vector after inserting 100 = " + v1 + ", size = "
+ [Link]());
[Link](100);
[Link]("Element removed at 0 index = " +
[Link](0));
[Link]("Vector = " + v1 + ", size = " + [Link]() +
",capacity = " + [Link]());
Vector v2 = (Vector)[Link]();
[Link]();
[Link]("Vector1 = " + v1 +", Size = " + [Link]() + ",
Capacity = " + [Link]());
[Link]("Vector2 = " + v2 +", Size = " + [Link]() + ",
Capacity = " + [Link]());
}
}
Output :
(b)Hashtable
/*Implementation of Hashtable*/
import [Link];
public class hashTable
{
public static void main(String args[])
{
Hashtable ht = new Hashtable();
[Link]("Hashtable : " + ht + ", size = " + [Link]());
[Link]("Hashtable is empty?" + [Link]());
[Link](101, "abc");
[Link](201, "xyz");
[Link](302, "ijk");
[Link](321, "jkl");
[Link]("Hashtable : " + ht + ", size = " + [Link]());
[Link]("Value at 101 = " + [Link](101));
[Link](201);
[Link]("Hashtable after removing 201 : " + ht + ", size = " +
[Link]());
[Link](321, "james");
[Link]("Hashtable after replace : " + ht + ", size = " +
[Link]());
boolean status = [Link](201);
[Link]("Status of the key 201 = "+status);
[Link]();
[Link]("Hashtable : " + ht + ", size = " + [Link]());
[Link]("Hashtable is empty?" + [Link]());
}
}
Output :
(c)Dictionary
/*Implementation of dictionary */
import [Link];
import [Link];
public class dictionary
{
public static void main(String args[])
{
Dictionary d = new Hashtable();
[Link]("Dictionary : " + d + ", size = " + [Link]());
[Link]("Dictionary is empty?" + [Link]());
[Link](101, "abc");
[Link](201, "xyz");
[Link](302, "ijk");
[Link](321, "jkl");
[Link]("Dictionary : " + d + ", size = " + [Link]());
[Link]("Value at 101 = " + [Link](101));
[Link](201);
[Link]("Dictionary after removing 201 : " + d + ", size = " +
[Link]());
[Link]("Dictionary : " + d + ", size = " + [Link]());
[Link]("Dictionary is empty?" + [Link]());
}
}
Output :
(d)Enumeration
/*Implementation of enumeration interface */
import [Link];
import [Link];
public class enumeration
{
public static void main(String args[])
{
Vector v = new Vector();
[Link]("Vector = " + v);
for(int i = 1; i <= 5; i++)
{
[Link](i);
}
[Link]("Vector = " + v);
[Link]("Traversing vector using enumeration === ");
Enumeration e = [Link]();
while([Link]())
{
[Link]([Link]());
}
}
}
Output :
11)Write a java program to implement iteration over Collection using Iterator
interface and ListIterator Interface.
/*Implementation of iteration over Collection */
import [Link].*;
public class IterColl
{
public static void main(String args[])
{
ArrayList list = new ArrayList();
for(int i = 10; i <= 15; i++)
{
[Link](i);
}
[Link]("Items in Arraylist = " + list);
[Link]("Traversing Arraylist using Iterator = ");
Iterator it = [Link]();
while([Link]())
{
[Link]([Link]()+"\t");
}
[Link]("Traversing ArrayList in forward direction using
ListIterator = ");
ListIterator lit = [Link]();
while([Link]())
{
[Link]([Link]()+"\t");
}
[Link]("Traversing ArrayList in backward direction using
ListIterator = ");
while([Link]())
{
[Link]([Link]()+"\t");
}
[Link]();
}
}
Output :