Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 84

Q. Write a program to display “Hello World” on the screen.

class Hello
{
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
System.out.println("HELLO WORLD");
}
}

OUTPUT :

Q. Write a program to show the syntax for printing a value.

1
class Basic
{
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
Int num=100;
System.out.println("Number's initial value : "+num);
num=num+2;
System.out.println("Updated value of number : "+num);
}
}

OUTPUT :

Q. Write a program to explain the concept of a block.

class Block

2
{
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
intx,y=10;
for(x=0;x<5;x++)
{
System.out.println("Value of x: "+x);
System.out.println("Value of y: "+y);
y=y-2;
}
}
}

OUTPUT :

Q. Write a program to calculate and display the distance covered by


the light in a given time.

classLightSpeed

3
{
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
intlightspeed;
long days;
long seconds;
long distance;
lightspeed=186000; //distance in m.p.h
days=1000;
seconds=days*24*60*60;
distance=lightspeed*seconds;
System.out.println("In "+ days +" light will travel about " + distance + "
miles");
}
}

OUTPUT :

Q. Write a program to show the concept of implicit conversion in char


datatype.

class Character

4
{
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
char ch1,ch2;
ch1=88;
ch2='Y';
System.out.println("Value of ch1 : "+ch1);
System.out.println("Value of ch2 : "+ch2);
}
}

OUTPUT :

Q. Write a program to explain the concept of dynamic initialisation.

classDynamicInitialisation
{

5
public static void main(String args[])
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
double a=3.0,b=4.0;
double c=Math.sqrt(a*a + b*b);
System.out.print("Value of c : "+c);
}
}

OUTPUT :

Q. Write a program to explain the concept of scope of a variable.

class Scope
{
public static void main(String args[])

6
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
int x=10;
if(x==10)
{
int y;
System.out.print("Value of x and y : "+x+" and "+y);
x=y*2;
}
y=100; //The error occurs here
System.out.println("Value of x : " + x);
}
}

OUTPUT :

Q. Write a program to explain the concept of lifetime of a variable.

classLifeTime
{
public static void main(String ar[])

7
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
int x;
for(x=0;x<3;x++)
{
int y=-1;
System.out.println("Value of y (inside if) : "+y);
y=100;
System.out.println("Value of y (inside for) : "+y);
}
}
}

OUTPUT :

Q. Write a program to explain the concept of explicit conversion.

class Conversion
{
public static void main(String args[])
{

8
byte b;
int i=257;
double d=323.142;
System.out.println("Conversion of int to byte");
b=(byte) i;
System.out.println("Value of i and b : "+i+" and "+b);
System.out.println("Conversion of double to int");
i=(int)d;
System.out.println("Value of d and i "+d+" and "+i);
System.out.println("Conversion of double to byte");
b=(byte)d;
System.out.println("Value of d and b "+d+" and "+b);
}
}

OUTPUT :

Q. Write a program to explain the working of do while loop.

classDoWhile
{
public static void main(String ar[])
{

9
int n=4;
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
do
{
System.out.println("tick "+n);
n--;
}while(n>0);
}
}

OUTPUT :

Q. Write a program to explain the working of foreachloop .

classForEach
{
public static void main(String ar[])
{
intnums[]={1,2,3,4};

10
int sum=0;
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
for(int x:nums)
{
System.out.println("Value of k : "+x);
sum+=x;
}
System.out.println("Summation is "+sum);
}
}

OUTPUT :

Q. Write a program to check whether a number is prime or not.

classPrime
{
public static void main(String ar[])
{
intnum;
booleanisprime=true;

11
num=14;
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
System.out.println("Entered number : "+num);
for(int i=2;i<=num/i;i++)
{
if((num%i)==0)
{
isprime=false;
break;
}
}
if(isprime)
System.out.println("Number is Prime");
else
System.out.println("Number is not Prime");
}
}

OUTPUT :

12
Q. Write a program to implement the concept of method overloading .

classOverloadDemo

13
{
void test()
{
System.out.println("No Parameters");
}
void test(int a)
{
System.out.println("Value of a : "+a);
}
void test(int a, int b)
{
System.out.println("Value of a and b : "+a+" "+b);
}
double test(double a)
{
System.out.println("Value of double a : "+a);
return a*a;
}
}
class Overload
{
public static void main(String[] args)
{
OverloadDemoobj= new OverloadDemo();
double result;
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
obj.test();
obj.test(10);
obj.test(10,20);

14
result=obj.test(123.25);
System.out.println("Result of obj.test(123.25) : "+result);
}
}

OUTPUT :

Q. Write a program to show the concept of passing objects as


parameters.

15
class Test
{
inta,b;
Test(int i, int j)
{
a=i;
b=j;
}
booleanequalTo(Test o)
{
if((o.a==a) && (o.b==b))
return true;
else
return false;
}
}
classPass_obj
{
public static void main(String[] args)
{
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
Test obj1 = new Test(100,22);
Test obj2 = new Test(100,22);
Test obj3 = new Test(-1,-1);
System.out.println("obj1 == obj2 : "+obj1.equalTo(obj2));
System.out.println("obj1 == obj2 : "+obj1.equalTo(obj3));
}
}

16
OUTPUT :

Q. Write a program to implement the concept of constructor


overloading.

class Area

17
{
doublelength,width;
Area()
{
length=10;
width=20;
}
Area(double l, double w)
{
length=l;
width=w;
}
double result()
{
return length*width;
}
}
classArea_rect
{
public static void main(String[] args)
{
double area;
System.out.println("Name : SamyakAgarwal");
System.out.println("Enrollment No : A50105217067");
Area obj1 = new Area();
Area obj2 = new Area(30,40);
area = obj1.result();
System.out.println("Area of rectangle by obj1 : "+area);
area=obj2.result();
System.out.println("Area of rectangle by obj2 : "+area);

18
}
}

OUTPUT :

Q. Write a program to show the syntax of if-else statement.

classifelse
{
public static void main(String[] args)
{

19
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
int i=10;
if(i>10)
System.out.println("Value is greater than 10.");
else
System.out.println("Value is less than 10");
}
}

OUTPUT :

Q. Write a program to show the syntax of switch-case statement.

classswitch_case
{
public static void main(String args[])
{

20
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
for(int i=3; i<4; i++)
switch(i)
{
case 0: System.out.println("i is zero.");
break;
case 1:System.out.println("i is one.");
break;
case 2:System.out.println("i is two.");
break;
case 3:System.out.println("i is three.");
break;
default: System.out.println("i is greater than 3.");
}
}
}

OUTPUT :

Q. Write a program to show the use of break keyword.

class Break
{
public static void main(String args[])

21
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
for(int i=0; i<10; i++)
{
if(i==5)
break;
else
System.out.println("i: " + i);
}
}
}

OUTPUT :

Q. Write a program to explain the concept of labelled break.

classLabelled_break
{
public static void main(String args[])

22
{
boolean t = true;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second;
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}

OUTPUT :

Q. Write a program to show the use of continue keyword.

class Continue
{
public static void main(String args[])

23
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
for(int i=0; i<10; i++)
{
System.out.print(i + " ");
if (i%2 == 0)
continue;
System.out.println("");
}
}
}

OUTPUT :

Q. Write a program to show the use of return keyword.

class Return
{
public static void main(String args[])

24
{
boolean t = true;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
System.out.println("Before the return.");
if(t)
return;
System.out.println("This won't execute.");
}
}

OUTPUT :

Q. Write a program to show the syntax of while loop.

classwhileloop
{
public static void main(String[] args)

25
{
int i=5;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
System.out.println("Natural numbers less than 5 : ");
while(i<6)
{
System.out.print(i);
i--;
}
}
}

OUTPUT :

Q. Write a program to explain the concept of empty loop.

classNoBody
{
public static void main(String args[])

26
{
inti,j;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
i=100;
j=200;
while(++i<--j);
System.out.println("Mid Point is "+i);
}
}

OUTPUT :

Q. Write a program to explain the concept of nested loop.


classNestedloop
{
public static void main(String args[])

27
{
int i, j;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
for(i=0; i<10; i++)
{
for(j=i; j<10; j++)
System.out.print("*");
System.out.println();
}
}
}

OUTPUT :

Q. Write a program to show the use of comma separator.


class Comma
{
public static void main(String args[])

28
{
int a, b;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
for(a=1, b=4; a<b; a++, b--)
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}

OUTPUT :

Q. Write a program to explain the concept of class.


class Box
{
double width;

29
double height;
double depth;
}
classBoxDemo
{
public static void main(String args[])
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
Box mybox = new Box();
doublevol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

OUTPUT :

Q. Write a program to explain the concept of class with multiple


objects.
class Box

30
{
double width;
double height;
double depth;
}
classBoxDemo
{
public static void main(String args[])
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
Box mybox1 = new Box();
Box mybox2 = new Box();
doublevol;
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

OUTPUT :

31
Q. Write a program to explain the concept of class with parameterized
method.
class Box

32
{
double width;
double height;
double depth;
double volume()
{
return width * height * depth;
}
voidsetDim(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
}
classBoxDemo
{
public static void main(String args[])
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
Box mybox1 = new Box();
Box mybox2 = new Box();
doublevol;
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);

33
}
}

OUTPUT :

Q. Write a program to explain the concept of constructors in a class.


class Box
{

34
double width;
double height;
double depth;
Box()
{
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
double volume()
{
return width * height * depth;
}
}
classBoxDemo
{
public static void main(String args[])
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
Box mybox1 = new Box();
Box mybox2 = new Box();
doublevol;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

35
OUTPUT :

Q. Write a program to explain the concept of call by value.


class Test
{

36
void meth(int i, int j)
{
i*=2;
j/=2;
}
}
classCallByValue
{
public static void main(String[] args)
{
Test obj = new Test();
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
int a=15,b=20;
System.out.println("Value of a and b before call : "+a+" "+b);
obj.meth(a,b);
System.out.println("Value of a and b after call : "+a+" "+b);
}
}

OUTPUT :

Q. Write a program to explain the concept of call by reference.


class Test

37
{
inta,b;
Test(int i, int j)
{
a=i;
b=j;
}
void meth(Test o)
{
o.a*=2;
o.b/=2;
}
}
classCallByRef
{
public static void main(String[] args)
{
Test obj = new Test(15,20);
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
System.out.println("Value of a and b before call : "+obj.a+" "+obj.b);
obj.meth(obj);
System.out.println("Value of a and b after call : "+obj.a+" "+obj.b);
}
}

OUTPUT :

Q. Write a program to explain the concept of method returning


object.

38
class Test
{
int a;
Test(int i)
{
a=i;
}
Test incrByTen()
{
Test temp = new Test(a+10);
return temp;
}
}
classReturnObject
{
public static void main(String[] args)
{
Test obj1 = new Test(2);
Test obj2;
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
obj2=obj1.incrByTen();
System.out.println("obj1.a : "+obj1.a);
System.out.println("obj2.a : "+obj2.a);
obj2=obj2.incrByTen();
System.out.println("obj2.a after second increase : "+obj2.a);
}
}
OUTPUT :

39
Q. Write a program to explain the concept of recursion.
class Factorial

40
{
int fact(int n)
{
int result;
if(n==1) return 1;
result=fact(n-1)*n;
return result;
}
}
class Recursion
{
public static void main(String[] args)
{
Factorial f = new Factorial();
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
System.out.println("Factorial of 4 : "+f.fact(4));
System.out.println("Factorial of 6 : "+f.fact(6));
}
}

OUTPUT :

Q. Write a program to explain the concept of access control.

41
class Test
{
int a;
publicint b;
privateint c;
voidsetc(int i)
{
c=i;
}
intgetc()
{
return c;
}
}
classAccessTest
{
public static void main(String[] args)
{
Test obj = new Test()
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
obj.a=10;
obj.b=20;
//obj.c=30 gives error
obj.setc(100);
System.out.println("a, b and c : "+obj.a+" "b+" "obj.getc());
}
}

OUTPUT :

42
43
Q. Write a program to explain the concept of multi-dimensional
array.
classTwoDArray
{
public static void main(String args[])
{
inttwoD[][]= new int[4][5];
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}

OUTPUT :

44
Q. Write a program to explain the concept of static keyword.
classUseStatic
{
staticint a=3;
staticint b;
static void meth(int x)
{
System.out.println("x : "+x);
System.out.println("a : "+a);
System.out.println("b : "+b);
}
static
{
System.out.println("Static Block Initialized.");
b=a*4;
}
public static void main(String[] args)
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
meth(42);
}
}

OUTPUT :

45
Q. Write a program to explain the concept of simple inheritance.
class A
{
inti,j;
voidshowij()
{
System.out.println("i and j : "+i+" "+j);
}
}
class B extends A
{
int k;
voidshowk()
{
System.out.println("k : "+k);
}
void sum()
{
System.out.println("Value of i+j+k :"+(i+j+k));
}
}
classSimpleInheritance
{
public static void main(String[] args)
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
A superobj = new A();
B subobj = new B();
superobj.i=10;
superobj.j=20;
System.out.println("Contents of superobj : ");
superobj.showij();

46
System.out.println();
subobj.i=7;
subobj.j=8;
subobj.k=9;
System.out.println("Contents of subobj : ");
subobj.showij();
subobj.showk();
System.out.println();
System.out.println("Sum of i,j and k in subobj : ");
subobj.sum();
}
}

OUTPUT :

47
Q. Write a program to explain the concept of order of execution of
constructor in case of inheritance.
class A
{
A()
{
System.out.println("Inside A's constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's constructor");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's constructor");
}
}
classCallingCons
{
public static void main(String[] args)
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
C obj = new C();

48
}
}

OUTPUT :

49
Q. Write a program to explain the concept of method overriding.
class A
{
inti,j;
A(int a, int b)
{
i=a;
j=b;
}
void show()
{
System.out.println("i and j : "+i+" "+j);
}
}
class B extends A
{
int k;
B(int a, int b, int c)
{
super(a,b);
k=c;
}
void show() //overriden method
{
System.out.println("k : "+k);
}
}
class Override
{
public static void main(String[] args)

50
{
B subobj = new B(1,2,3);
subobj.show();
}
}

OUTPUT :

51
Q. Write a program to explain the use of super keyword.
class A
{
int i;
}
class B extends A
{
int i;
B(int a, int b)
{
super.i=a;
i=b;
}
void show()
{
System.out.println("Value of i in super class : "+super.i);
System.out.println("Value of i in sub class : "+i);
}
}
classUseSuper
{
public static void main(String[] args)
{
System.out.println("Name: SamyakAgarwal");
System.out.println("Enrollment Number: A50105217067");
B subobj = new B(10,20);
subobj.show();
}
}

OUTPUT :

52
Q. Write a program to explain the concept of dynamic method
dispatch.
class A
{
voidcallme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
{
voidcallme()
{
System.out.println("Inside B's callme method");
}
}
class C extends A
{
voidcallme()
{
System.out.println("Inside C's callme method");
}
}
class Dispatch
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();

53
A r;
r=a;
r.callme();
r=b;
r.callme();
r=c;
r.callme();
}
}

OUTPUT :

54
Q. Write a program to explain the concept of abstract classes.
abstract class A
{
abstract void callme();
voidcallmetoo()
{
System.out.println("This is a concrete method");
}
}
class B extends A
{
voidcallme()
{
System.out.println("B's implementation of callme");
}
}
classAbstractDemo
{
public static void main(String[] args)
{
B b = new B();
b.callme();
b.callmetoo();
}
}

OUTPUT :

55
Q. Write a program to explain the concept of packages in java.
package MyPack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n;
bal = b;
}
void show()
{
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}

56
}

OUTPUT :

Q. Write a program to explain the concept of interfaces in java.


57
interface Callback
{
void callback(int param);
}
class Client implements Callback
{
// Implement Callback's interface
public void callback(int p)
{
System.out.println("callback called with " + p);
}
void nonIfaceMeth()
{
System.out.println("Classes that implement interfaces may also define
other members, too.");
}
}
// Another implementation of Callback.
class AnotherClient implements Callback
{
// Implement Callback's interface
public void callback(int p)
{
System.out.println("Another version of callback");
System.out.println("p squared is " + (p*p));
}
}
class TestIface
{
public static void main(String args[])

58
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
Callback c = new Client();
AnotherClient ob = new AnotherClient();
c.callback(42);
c = ob; // c now refers to AnotherClient object
c.callback(42);
}
}

OUTPUT :

59
Q. Write a program to explain the concept of exception handling in
java using multiple catch clauses.
class MultiCatch
{
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
try
{
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index oob: " + e);
}
catch(Exception e)
{
System.out.println("Exception Caught : "+ e);
}
System.out.println("After try/catch blocks.");
}

60
}

OUTPUT :

61
Q. Write a program to explain the use of throw keyword in java.
class ThrowDemo
{
static void demoproc()
{
try
{
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
try
{
demoproc();
}
catch(NullPointerException e)
{
System.out.println("Recaught: " + e);
}
}
}

62
OUTPUT :

Q. Write a program to explain the use of throws keyword in java.

63
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
try
{
throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
OUTPUT :

Q. Write a program to explain the use of finally keyword in java.

64
class FinallyDemo
{
// Through an exception out of the method.
static void procA()
{
try
{
System.out.println("inside procA");
throw new RuntimeException("demo");
}
finally
{
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB()
{
try
{
System.out.println("inside procB");
return;
}
finally
{
System.out.println("procB's finally");
}
}
// Execute a try block normally.
public static void main(String args[])

65
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
try
{
procA();
}
catch (Exception e)
{
System.out.println("Exception caught");
}
procB();
}
}

OUTPUT :

Q. Write a program to create a new thread using the Runnable


interface.

66
class NewThread implements Runnable
{
Thread t;
NewThread()
{
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run()
{
try
{
for(int i = 3; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo
{

67
public static void main(String args[ ] )
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
new NewThread(); // create a new thread
try
{
for(int i = 3; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT :

Q. Write a program to create a new thread by extending the Thread


class.

68
class NewThread extends Thread
{
NewThread()
{
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread
{
public static void main(String args[])

69
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
new NewThread(); // create a new thread
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT :

Q. Write a program to explain the use of isAlive() and join() method


in java.

70
class NewThread implements Runnable
{
String name; // name of thread
Thread t;
NewThread(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run()
{
try
{
for(int i = 2; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin

71
{
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
// wait for threads to finish
try
{
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Main thread exiting.");
}
}

OUTPUT :

72
Q. Write a program to explain the use synchronize keyword in java.
class Callme
{

73
void call(String msg)
{
System.out.print("[" + msg);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
// synchronize calls to call()
public void run()
{
synchronized(target) // synchronized block

74
{
target.call(msg);
}
}
}
class Synch
{
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}

OUTPUT :

75
Q. Write a program to explain the concept of interthread
communication in java.
class Q
{

76
int n;
boolean valueSet = false;
synchronized int get()
{
while(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n = n;

77
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q = q;
new Thread(this, "Producer").start();
}
public void run()
{
int i = 0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q = q;
new Thread(this, "Consumer").start();
}

78
public void run()
{
while(true)
{
q.get();
}
}
}
class PCFixed
{
public static void main(String args[])
{
System.out.println("Name : Samyak Agarwal");
System.out.println("Enrollment No. : A50105217067");
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}

OUTPUT :

79
Q. Write a program for awt-event handling.
import java.awt.*;
import java.awt.event.*;

80
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}

Q. Write a program for awt-button.


import java.awt.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
Button b=new Button("Click Here");
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);

81
f.setLayout(null);
f.setVisible(true);

}
}
Q. Write a program for swing-button.
import javax.swing.*;
public class ButtonExample2 {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Q. Write a program for swing jFrame (javaTpoint).
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrameExample {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());

82
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Q. Write a program for swing jFrame from samyak file.
import javax.swing.*;
public class TextAreaExample{
TextAreaExample()
{
JFrame f=new JFrame();
JTextArea area=new JTextArea("JAVA LAB PRACTICAL");
area.setBounds(10,30,200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}

83
}

84

You might also like