8/17/2019 Programs in Java
1/196
List of Java programs
Sl.No Programs
1 Program to Demonstrate simple JAVA program
2 Program to compute the area of a circle
3 Program to demonstrate type casting in JAVA
4 Program to demonstrate the asic Arithmetic !perations
" Program to #nd largest of three numers
$ Program to print #rst 1% &ionacci numers
' Program to (alculate the factorial of a numer using )*(+)S,!N
- Program to print prime numers using (A
/ Program to Demonstrate (ommand ine Argument
1% Program to con0ert temperature from &ahrenheit to (elsius using (A
11 Program to produce con0ersion tale dollar and rupees using (A
12 Program to print area of right angled triangle using (A
gcomputers softare training centrePage 1
8/17/2019 Programs in Java
2/196
13 Program to print all the elements in an array using (A
14 Program to #nd the sum of all the elements in an array
1" Program to print all the elements in a 2D array
1$ Program to #nd the sum of to matrices
1' Program to #nd the dierence of to matrices using (A
1- Program to print the +PP*) and !5*) triangle of a matri6
1/ Program to #nd the trace of a matri6
2% Program to #nd the transpose of a matri6
21 Program to #nd the product of to matrices
22 Program to demonstrate a (ASS
23 Program to demonstrate a (onstructor
24 Program to demonstrate a Parameteri7ed (onstructor
2" Program to Demonstrate 89,S eyord
2$ Program to Demonstrate (onstructor !0erloading
2' Program to pass o:ects as parameters
2- Program to Demonstrate ;ueue using (lass
2/ Program to Demonstrate Stac using (lass
3% Program to Demonstrate Access Speci#ers
31 Program to Demonstrate Static Variales
32 Program to Demonstrate Static
8/17/2019 Programs in Java
3/196
4" Program to Demonstrate ,N8*)&A(*
4$ Program to access implementation through ,nterface references
4' Program to Demonstrate *>8*ND,N? ,N8*)&A(*
4- Program to Demonstrate Stac using interface
4/ Program to Demonstrate ;ueue using ,nterface
"% Program to Demonstrate a S,
8/17/2019 Programs in Java
4/196
'' Program to Pass Parameters perform
8/17/2019 Programs in Java
5/196
1%/ Program to Demonstrate (allale Statement
11% Program to Demonstrate )esult Set
8/17/2019 Programs in Java
6/196
(ompile Program in (ommand y using :a0ac
(MO:a0ac A.:a0a
)un the Program in (ommand y using :a0a
(MO:a0a A
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
!+8P+8 M
5elcome to my 5orld
%2 Program to compute the area of a circle
class A2
F
pulic static 0oid mainBString argsGHC
F
int r1%
System.out.printlnBI)adius of (ircle MI Q rC
System.out.printlnBIArea of (ircle MI Q rRrC
K
K
!+8P+8 M
gcomputers softare training centrePage $
8/17/2019 Programs in Java
7/196
)adius of (ircle M 1%
Area of (ircle M 314
%3 Program to demonstrate type casting in JAVA
class con0ersion
F
pulic static 0oid mainBString argsGHC
F
yte
int i2"'
doule d323.142
System.out.printlnBIn(on0ersion of int to yte.IC
ByteC i
System.out.printlnBIi and I QiQI IQC
System.out.printlnBIn(on0ersion of doule to int.ICiBintCd
System.out.printlnBId and i I Qd QI IQiC
System.out.printlnBIn(on0ersion of doule to yte.IC
ByteCd
System.out.printlnBId and I Qd Q I I QC
K
K
!+8P+8 M
(on0ersion of int to yte.
i and 2"' 1
gcomputers softare training centrePage '
8/17/2019 Programs in Java
8/196
8/17/2019 Programs in Java
9/196
%4 Program to demonstrate the asic Arithmetic!perations
class arith
F
pulic static 0oid mainBString argsGHC
F
int a1%E3
System.out.printlnBIsum is IQBaQCC
System.out.printlnBIdierence is IQBaLCC
System.out.printlnBIproduct is IQBaRCC
System.out.printlnBIuotient is IQBaTCC
System.out.printlnBIremainder is IQBaUCC
K
K
!+8P+8 M
sum is 13
dierence is '
product is 3%
uotient is 3
remainder is 1
%" Program to #nd largest of three numers
class large
gcomputers softare training centrePage /
8/17/2019 Programs in Java
10/196
F
pulic static 0oid mainBString argsGHC
F
int a1%E3%Ec2%
System.out.printlnBI?reatest of gi0en 3 numersIC
ifBaOaOcC
System.out.printlnBQaC
else ifBOcC
System.out.printlnBQC
else
System.out.printlnBQcC
K
K
!+8P+8 M
?reatest of gi0en 3 numers
3%
%$ Program to print #rst 1% &ionacci numers
class #
F
pulic static 0oid mainBString argsGHC
F
int i/Ef%Es1Etemp
System.out.printlnBQfC
hileBiO%C
gcomputers softare training centrePage 1%
8/17/2019 Programs in Java
11/196
8/17/2019 Programs in Java
12/196
%' Program to (alculate the factorial of a numer using)*(+)S,!N
class &actorial
F
int factBint nC
F
int result
ifBn1C
return 1
resultfactBnL1CRn
return result
K
K
class )ecursion
F
pulic static 0oid mainBString argsGHCF
&actorial fne &actorialBC
System.out.printlnBI&actorial of 3 is I Qf.factB3CC
System.out.printlnBI&actorial of 4 is I Qf.factB4CC
System.out.printlnBI&actorial of " is I Qf.factB"CC
K
K
!+8P+8 M
&actorial of 3 is $
&actorial of 4 is 24
&actorial of " is 12%
gcomputers softare training centrePage 12
8/17/2019 Programs in Java
13/196
%- Program to print prime numers using (A
class primecla
F
pulic static 0oid mainBString sGHC
F
int nEr
n,nteger.parse,ntBsG%HC
forBint i2iniQQC
F
int c%
forBint :1:i:QQC
F
riU:
if Br%CcQ1
K
ifBc2C
System.out.printlnBiC
KKK
!+8P+8 M
:a0a primecla 1%
2
3
"
'
gcomputers softare training centrePage 13
8/17/2019 Programs in Java
14/196
%/ Program to Demonstrate (ommand ine Argument
class (ommandine
F
pulic static 0oid mainBString argsGHC
F
forBint i% iargs.length iQQC
System.out.printlnBIargsGI Qi Q IHM IQargsGiHC
K
K
!+8P+8 M
:a0a (ommandine 1 3 4 2 "argsG%HM 1
argsG1HM 3
argsG2HM 4
argsG3HM 2
argsG4HM "
gcomputers softare training centrePage 14
8/17/2019 Programs in Java
15/196
1% Program to con0ert temperature from &ahrenheit to(elsius scale using (A
class ftoccla
F
pulic static 0oid mainBString sGHC
F
int f
doule c
f,nteger.parse,ntBsG%HC
c"RBfL32CT/
System.out.printlnBfQI fahrenheit IQcQI celsiusIC
K
K
!+8P+8 M
:a0a ftoccla 34
34 fahrenheit 1.% celsius
11 Program to produce the con0ersion tale for dollarand rupees
using (A
class dollarcla
F
pulic static 0oid mainBString sGHC
F
int nEd
gcomputers softare training centrePage 1"
8/17/2019 Programs in Java
16/196
n,nteger.parse,ntBsG%HC
System.out.printlnBIDollarstt)upeesI C
forBint i1iniQQC
F
d,nteger.parse,ntBsGiHC
System.out.printlnBdQIttIQBdR4"C C
K
K
K
!+8P+8 M
:a0a dollarcla 4 1 34 3$ 3'
Dollars )upees
1 4"
34 1"3%3$ 1$2%
3' 1$$"
12 Program to print area of right angled triangle using(A
class Area8ri
F
pulic static 0oid mainBString sGHC
F
int aEEarea
a,nteger.parse,ntBsG%HC
gcomputers softare training centrePage 1$
8/17/2019 Programs in Java
17/196
,nteger.parse,ntBsG1HC
areaaRT2
System.out.printlnBIArea of )ight angled 8riangle IQareaC
K
K
!+8P+8 M
:a0a Area8ri " 4
Area of )ight angled 8riangle 1%
13 Program to print all the elements in an array using(A
class matri6cla
F
pulic static 0oid mainBString sGHC
F
int nEm
n,nteger.parse,ntBsG%HC
System.out.printlnBI8he length of the matri6 isM I QnC
System.out.printlnBI8he elements of the matri6 areMIC
forBint i1iniQQC
F
m,nteger.parse,ntBsGiHC
System.out.printlnBmC
K
gcomputers softare training centrePage 1'
8/17/2019 Programs in Java
18/196
8/17/2019 Programs in Java
19/196
System.out.printlnBISum is I Q resultC
K
K
!+8P+8 M
8he *lements of Array are M
1.1
1.2
1.3
1.4
1."
Sum is $."
1" Program to print all the elements in a 2D array
class
8/17/2019 Programs in Java
20/196
K
System.out.printlnBC
K
KK
!+8P+8 M
Numer of )o 2
Numer of (olumn 3
8he elements of the matri6 are M
1 2 3
4 " $
1$ Program to #nd the sum of to matrices
class addmF
pulic static 0oid mainBString argsGHC
F
int aGHGHFF1E2E3KEF4E"E$KEF'E-E/KK
int GHGHFF1E1E1KEF%E1E%KEF%E%E1KK
int cGHGH ne intG3HG3H
int iE:
forBi%i3iQQC
forB :%:3:QQC
cGiHG:HaGiHG:HQGiHG:H
forBi%i3iQQC
F
forB:%:3:QQC
gcomputers softare training centrePage 2%
8/17/2019 Programs in Java
21/196
System.out.printBQcGiHG:HQ I IC
System.out.printlnBC
K
K
K
!+8P+8 M
2 3 4
4 $ $
' - 1%
1' Program to #nd the dierence of to matrices using (A
class
8/17/2019 Programs in Java
22/196
8/17/2019 Programs in Java
23/196
pulic static 0oid mainBString arsgGHC F
int aGHGHFF1E2E3KEF4E"E$KEF'E-E/KK
System.out.printlnBIthe gi0en matri6MIC
forBint i%i3iQQC F
forBint :%:3:QQC
System.out.printBI IQaGiHG:HC
System.out.printlnBC K
System.out.printlnBIupper triangle of matri6MIC
forBint i%i3iQQC F
forBint :%:3:QQC F
ifBi:C
System.out.printBaGiHG:H Q I IC
else
System.out.printBI IC K
System.out.printlnBC K
System.out.printlnBIloer triangle of matri6MIC
forBint i%i3iQQC F
forBint :%:3:QQC F
ifBiO:C
System.out.printBaGiHG:H Q I IC
else
System.out.printBI IC K
System.out.printlnBC
KKK
!+8P+8 M
8he gi0en matri6M
1 2 3
4 " $
gcomputers softare training centrePage 23
8/17/2019 Programs in Java
24/196
' - /
+pper triangle of matri6M
1 2 3
" $
/
oer triangle of matri6M
1
4 "
' - /
1/ Program to #nd the trace of a matri6
class 8race
F
pulic static 0oid mainBString argsGHC
F
int aGHGHFF1E2E3KEF4E"E$KEF'E-E/KK
int iE:Esum%
System.out.printlnBI8he gi0en matri6 M IC
forBint i % i 3 iQQC
F
forBint : % : 3 :QQC
System.out.printBaGiHG:H Q I IC
System.out.printlnBC
K
forBi%i3iQQC
F
forB:%:3:QQC
ifBi:C
gcomputers softare training centrePage 24
8/17/2019 Programs in Java
25/196
sumsumQaGiHG:H
K
System.out.printlnBI8race is M I QsumC
K
K
!+8P+8 M
8he gi0en matri6 M
1 2 3
4 " $
' - /
8race is M 1"
2% Program to #nd the transpose of a matri6
class 8ranspose
F
pulic static 0oid mainBString argsGHC
F
int aGHGHFF1E2E3KEF4E"E$KEF'E-E/KK
int iE:
System.out.printlnBI8he gi0en matri6 M IC
forBint i % i 3 iQQC
F
forBint : % : 3 :QQC
System.out.printBaGiHG:H Q I IC
System.out.printlnBC
gcomputers softare training centrePage 2"
8/17/2019 Programs in Java
26/196
K
System.out.printlnBI8ranspose of
8/17/2019 Programs in Java
27/196
21 Program to #nd the product of to matrices
class
8/17/2019 Programs in Java
28/196
22 Program to demonstrate a (ASS
class Democlass TTDe#ning a class
F
int a34TT,nstance Variale of the class
0oid shoaBC TT
8/17/2019 Programs in Java
29/196
F
doule idth
doule height
doule depth
o6BC
F
System.out.printlnBI(onstructing o6IC
idth1%
height1%
depth1%
K
doule 0olumeBC
F
return idthRheightRdepth
K
Kclass o6Demo$
F
pulic static 0oid mainBString argsGHC F
o6 myo61ne o6BC
o6 myo62ne o6BC
doule 0ol
0olmyo61.0olumeBC
System.out.printlnBI0olume of is I Q0olC
0olmyo62.0olumeBC
System.out.printlnBI0olume of is I Q0olC
KK
!+8P+8 M
gcomputers softare training centrePage 2/
8/17/2019 Programs in Java
30/196
(onstructing o6
(onstructing o6
0olume of is 1%%%.%
0olume of is 1%%%.%
24 Program to demonstrate a Parameteri7ed(onstructor
class o6
F
doule idth
doule height
doule depth
o6Bdoule Edoule hEdoule dC
F
idth
heighth
depthd
K
doule 0olumeBC
F
return idthRheightRdepth
K
K
class o6Demo'
F
pulic static 0oid mainBString argsGHC F
gcomputers softare training centrePage 3%
8/17/2019 Programs in Java
31/196
o6 myo61ne o6B1%E2%E1"C
o6 myo62ne o6B3E$E/C
doule 0ol
0olmyo61.0olumeBC
System.out.printlnBI0olume of is I Q0olC
0olmyo62.0olumeBC
System.out.printlnBI0olume of is I Q0olC
K
K
!+8P+8 M
0olume of is 3%%%.%
0olume of is 1$2.%
2" Program to Demonstrate 89,S eyord
class o6
F
doule idth
doule height
doule depth
o6Bdoule idthEdoule heightEdoule depthC
F
this.idthidth
this.heightheight
this.depthdepth
gcomputers softare training centrePage 31
8/17/2019 Programs in Java
32/196
K
doule 0olumeBC
F
return idthR heightR depth
K
K
class o68his
F
pulic static 0oid mainBString argsGHC
F
doule 0ol
o6 myo61ne o6B1%E2%E1"C
o6 myo62ne o6B3E$E/C
0olmyo61.0olumeBC
System.out.printlnBIVolume is I Q0olC
0olmyo62.0olumeBCSystem.out.printlnBIVolume is I Q0olC
K
K
!+8P+8 M
Volume is 3%%%.%
Volume is 1$2.%
2$ Program to Demonstrate (onstructor !0erloading
class o6
F
gcomputers softare training centrePage 32
8/17/2019 Programs in Java
33/196
doule idth
doule height
doule depth
o6Bdoule Edoule hEdoule dC
F
idth
heighth
depthd
K
o6BC
F
idthL1
heightL1
depthL1
K
o6Bdoule lenCF
idthheightdepthlen
K
doule 0olumeBC
F
return idthRheightRdepth
K
K
class !0erload(ons
F
pulic static 0oid mainBString argsGHC F
gcomputers softare training centrePage 33
8/17/2019 Programs in Java
34/196
o6 myo61ne o6B1%E2%E1"C
o6 myo62ne o6BC
o6 mycuene o6B'C
doule 0ol
0olmyo61.0olumeBC
System.out.printlnBI0olume of myo61 is I Q0olC
0olmyo62.0olumeBC
System.out.printlnBI0olume of myo62 is I Q0olC
0olmycue.0olumeBC
System.out.printlnBI0olume of mycue is I Q0olC
K
K
!+8P+8 M
0olume of myo61 is 3%%%.%0olume of myo62 is L1.%
0olume of mycue is 343.%
2' Program to pass o:ects as parameters
class 8est
F
int aE
8estBint iEint :C
F
ai
gcomputers softare training centrePage 34
8/17/2019 Programs in Java
35/196
:
K
oolean eualsB8est oC
F
ifBo.aa o.C return true
else return false
K
K
class Pass!
F
pulic static 0oid mainBString argsGHC
F
8est o1ne 8estB1%%E22C
8est o2ne 8estB1%%E22C
8est o3ne 8estBL1EL1C
System.out.printlnBIo1 o2 M I Qo1.eualsBo2CCSystem.out.printlnBIo1 o3 M I Qo1.eualsBo3CC
K
K
!+8P+8 M
o1 o2 M true
o1 o3 M false
2- Program to Demonstrate ;ueue using (lass
gcomputers softare training centrePage 3"
8/17/2019 Programs in Java
36/196
8/17/2019 Programs in Java
37/196
class ueF
pulic static 0oid mainBString argsGHCF
A o:1 ne ABC
forBint i%i1%iQQC
o:1.pushBiC
System.out.printlnBI;ueue elements are M IC
forBint i % i1%iQQC
System.out.printlnBo:1.popBCC
K
K
!+8P+8 M
;ueue elements are M
%
12
3
4
"
$
'
-
/
2/ Program to Demonstrate Stac using (lass
class A
gcomputers softare training centrePage 3'
8/17/2019 Programs in Java
38/196
F
int sGH ne intG1%H
int tos
ABC
F
tosL1
K
0oid pushBint itemC
F
ifBtos/C
System.out.printlnBIStac is &ullIC
else
sGQQtosHitem
K
int popBC
FifBtos%C
F
System.out.printlnBIStac is +nderWoIC
return %
K
else
return sGtosLLH
K
K
class stacF
pulic static 0oid mainBString argsGHCF
A o:1 ne ABC
gcomputers softare training centrePage 3-
8/17/2019 Programs in Java
39/196
forBint i%i1%iQQC
o:1.pushBiC
System.out.printlnBIStac elements are M IC
forBint i % i1%iQQC
System.out.printlnBo:1.popBCC
K
K
!+8P+8 M
Stac elements are M
/
-
'$
"
4
3
2
1
%
3% Program to Demonstrate Access Speci#ers
class 8est
F
gcomputers softare training centrePage 3/
8/17/2019 Programs in Java
40/196
int a
pulic int
pri0ate int c
0oid setcBint iC
F
ci
K
int getcBC F
return c
K
K
class Acess8est
F
pulic static 0oid mainBString argsGHC
F
8est one 8estBCTT8hese are !=Ea and may e accessed directly
o.a1%
o.2%
TT8his is not != and ill cause an error
TTo.c1%% TTerrorX
TTyou must access c through its methods
o.setcB1%%C TT!
System.out.printlnBIaEEand cM I Qo.aQI I Qo. QIIQo.getcBCC
K
K
!+8P+8 M
gcomputers softare training centrePage 4%
8/17/2019 Programs in Java
41/196
aEEand cM 1% 2% 1%%
31 Program to Demonstrate Static Variales
class 8est
F
static int count
static
F
System.out.printlnBI5elcomeIC
count%
K
8estBC
F
countQQK
K
class 8eststatic0ariale
F
pulic static 0oid mainBString argsGHC
F
8est t1ne 8estBC
8est t2ne 8estBC
8est t3ne 8estBC
System.out.printlnB8est.count Q I o:ects are createdIC
K
K
gcomputers softare training centrePage 41
8/17/2019 Programs in Java
42/196
!+8P+8 M
5elcome
3 o:ects are created
32 Program to Demonstrate Static
8/17/2019 Programs in Java
43/196
System.out.printlnB
8/17/2019 Programs in Java
44/196
class ,nner(lassDemo
F
pulic static 0oid mainBString argsGHC
F
!uter outerne !uterBC
outer.testBC
K
K
!+8P+8 M
displayM outerY6 1%%
gcomputers softare training centrePage 44
8/17/2019 Programs in Java
45/196
34 Program to Demonstrate 8ype 5rapping
class 5rap F
pulic static 0oid mainBString argsGHC
F
,nteger o: ne ,ntegerB34C
int 6 o:.intValueBC
System.out.printlnBI6 I Q6C
System.out.printlnBIo: I Qo:C
K
K
!+8P+8 M
6 34
o: 34
3" Program to Demonstrate Single ,nheritance
class A
F
int iE:
0oid shoi:BC
F
System.out.printlnBIi and : M I QiQ I I Q:C
K
K
gcomputers softare training centrePage 4"
8/17/2019 Programs in Java
46/196
class e6tends A
F
int
0oid shoBC
F
System.out.printlnBI M IQC
K
0oid sumBC
F
System.out.printlnBIiQ:QM IQBiQ:QCC
K
K
class Simple,nheritance
F
pulic static 0oid mainBString argsGHC
F
A supero ne ABC
suo ne BC
supero.i1%
supero.:2%
System.out.printlnBI(ontents of superoM IC
supero.shoi:BC
System.out.printlnBC
suo.i'
suo.:-
gcomputers softare training centrePage 4$
8/17/2019 Programs in Java
47/196
suo./
System.out.printlnBI(ontents of suoM IC
suo.shoi:BC
System.out.printlnBC
System.out.printlnBISum of iE: in suoM IC
suo.sumBC
K
K
!+8P+8 M
(ontents of superoM
i and : M 1% 2%
(ontents of suoM
i and : M ' -
Sum of iE: in suoM
iQ:QM 24
3$ Program to Demonstrate
8/17/2019 Programs in Java
48/196
K
0oid putrollBC
F
System.out.printlnBI)oll numer M IQrollnoC
K
K
class 8est e6tends Student TT#rst le0el inheritence
F
int mars1Emars2
0oid getmarsBint 6Eint yC
F
mars16
mars2y
K0oid putmarsBC
F
System.out.printlnBI
8/17/2019 Programs in Java
49/196
totalmars1Qmars2
putrollBC
putmarsBC
System.out.printlnBI8otal IQtotalC
K
K
class
8/17/2019 Programs in Java
50/196
class Student
F
int roll
0oid getrollBint 6C
F
roll6
K
0oid putrollBC
F
System.out.printlnBrollC
K
K
class )esult1 e6tends Student
Fint mars1
0oid getmars1Bint yC
F
mars1y
K
0oid shomars1BC
F
System.out.printBI)oll no of 1st student is IC
putrollBC
System.out.printlnBI
8/17/2019 Programs in Java
51/196
class )esult2 e6tends Student F
int mars2
0oid getmars2Bint 7C
F
mars27
K
0oid shomars2BC
F
System.out.printBI)ollno of 2nd student is IC
putrollBC
System.out.printlnBI
8/17/2019 Programs in Java
52/196
K
K
!+8P+8 M
)oll no of 1st student is 3$
8/17/2019 Programs in Java
53/196
8/17/2019 Programs in Java
54/196
ia
:
K
0oid shoBC F
System.out.printlnBIi and :M I Qi Q I I Q:C
KK
class e6tends A F
int
Bint aEint Eint cC
F
superBaEC
c
K
0oid shoBC
FSystem.out.printlnBIM IQC
KK
class !0erride F
pulic static 0oid mainBString argsGHC
F
su!ne B1E2E3C
su!.shoBC
K
K
!+8P+8 M
gcomputers softare training centrePage "4
8/17/2019 Programs in Java
55/196
M 3
4% Program to Demonstrate )untime Polymorphism
class #gure
F
doule dim1
doule dim2
#gureBdoule aEdoule C
F
dim1a
dim2
K
doule areaBC
FSystem.out.printlnBIarea for #gure is unde#nedIC
return %
K
K
class rectangle e6tends #gure
F
rectangleBdoule aEdoule C
F
superBaEC
K
doule areaBC
gcomputers softare training centrePage ""
8/17/2019 Programs in Java
56/196
F
System.out.printlnBI,nside Area for rectangleIC
return dim1Rdim2
K
K
class triangle e6tends #gure F
triangleBdoule aEdoule C
F
superBaEC
K
doule areaBC
FSystem.out.printlnBI,nside area for triangleIC
return Bdim1Rdim2CT2
KK
class &indAreas F
pulic static 0oid mainBString argsGHC F
#gure fne #gureB1%E1%C
rectangle rne rectangleB/E"C
triangle tne triangleB1%E-C
#gure #gref
#grefr
System.out.printlnBIArea is I Q#gref.areaBCC
#greft
gcomputers softare training centrePage "$
8/17/2019 Programs in Java
57/196
System.out.printlnBIArea is I Q#gref.areaBCC
#greff
System.out.printlnBIArea is I Q#gref.areaBCC
KK
!+8P+8 M
,nside Area for rectangle
Area is 4".%
,nside area for triangle
Area is 4%.%
area for #gure is unde#ned
Area is %.%
41 Program to Demonstrate Astract (lass
astract class A
F
astract 0oid callmeBC
0oid callmetooBCF
System.out.printlnBIthis is a concrete method.IC
KK
class e6tends A
F
0oid callmeBC
F
System.out.printlnBIZs implementation of callme.IC
gcomputers softare training centrePage "'
8/17/2019 Programs in Java
58/196
KK
class AstractDemo
F
pulic static 0oid mainBString argsGHC F
ne BC
.callmeBC
.callmetooBC
K
K
!+8P+8 M
Zs implementation of callme.
this is a concrete method.
42 Program to Demonstrate &,NA =eyord
class A
F
#nal int 6 3
int y
ABC
F
TT64this results in error
y4
gcomputers softare training centrePage "-
8/17/2019 Programs in Java
59/196
K
K
class &inaldemo F
pulic static 0oid mainBString argsGHC F
A a ne ABC
System.out.printlnBI6 I Qa.6C
System.out.printlnBIy I Qa.yC
TTa.6"this also results in error
a.y$
System.out.printlnBI6 I Qa.6C
System.out.printlnBIy I Qa.yC
K
K
!+8P+8 M
6 3
y 4
6 3
y $
43 Program to Demonstrate &,NA
8/17/2019 Programs in Java
60/196
ABint aEint C F
ia
:
K
0oid shoBC F
System.out.printlnBIi and :M I Qi Q I I Q:C
K
#nal 0oid displayBC TT(annot e !0erriden
F
System.out.printlnBIi and :M I Qi Q I I Q:C
KK
class e6tends A F
int
Bint aEint Eint cC F
superBaEC
c
K
0oid shoBC F
System.out.printlnBIM IQC
KK
class &inalmeth F
pulic static 0oid mainBString argsGHC
F
su!ne B1E2E3C
System.out.printlnBI!0erriden
8/17/2019 Programs in Java
61/196
!+8P+8 M
!0erriden
8/17/2019 Programs in Java
62/196
K
!+8P+8 M
a34
4" Program to Demonstrate ,N8*)&A(*
interface Sum TT de#ning ,nterface
F
int 72TT#nal 0ariale declared
int addBCTTAstract
8/17/2019 Programs in Java
63/196
8/17/2019 Programs in Java
64/196
return dim1Rdim2
K
K
class triangle implements #gure
F
doule dim1Edim2
triangleBdoule aEdoule C
F
dim1adim2
K
pulic doule areaBC
F
return Bdim1Rdim2CT2
KKTTimplements through interface
class demo
F
pulic static 0oid mainBString argsGHC
F
rectangle rne rectangleB1%%E"%C
triangle tne triangleB1%E2%C
#gure f
fr
System.out.printlnBf.areaBCC
ft
gcomputers softare training centrePage $4
8/17/2019 Programs in Java
65/196
System.out.printlnBf.areaBCC
K
K
!+8P+8 M
"%%%.%
1%%.%
4' Program to Demonstrate *>8*ND,N? ,N8*)&A(*
interface A F
0oid meth1BC0oid meth2BC
K
interface e6tends A F
0oid meth3BC
K
class
8/17/2019 Programs in Java
66/196
K
pulic 0oid meth3BC F
System.out.printlnBImethod3IC
KK
class *6tint F
pulic static 0oid mainBString argsGHC F
8/17/2019 Programs in Java
67/196
F
0oid pushBint itemC
int popBC
K
class A implements stac
F
int sGH
int tos
ABint si7eC
F
s ne intGsi7eH
tos L1
K
pulic 0oid pushBint itemC
FifBtos s.lengthL1C
System.out.printlnBIS8A(= is &+IC
else
sGQQtosHitem
K
pulic int popBC
F
ifBtos%C
F
System.out.printlnBIS8A(= ,S *
8/17/2019 Programs in Java
68/196
return sGtosLLH
KK
class S F
pulic static 0oid mainBString argsGHCF
int i
A stac1 ne AB"C
forBi%i"iQQC
stac1.pushBiC
System.out.printlnBIStac elements are M IC
forBi %i"iQQC
System.out.printlnBstac1.popBCC
K
K
!+8P+8 M
Stac elements are M
4
3
2
1
%
4/ Program to Demonstrate ;ueue using ,nterface
gcomputers softare training centrePage $-
8/17/2019 Programs in Java
69/196
interface ueue
F
0oid pushBint itemC
int popBC
K
class implements ueue
F
int sGH
int fEl
Bint si7eC
F
s ne intGsi7eH
f L1l L1
K
pulic 0oid pushBint itemC
F
ifBf s.lengthL1C
System.out.printlnBI; is &+IC
else
sGQQfHitem
K
pulic int popBC
F
ifBflC
F
gcomputers softare training centrePage $/
8/17/2019 Programs in Java
70/196
System.out.printlnBI ; iS *
8/17/2019 Programs in Java
71/196
3
4
"
$
'
"% Program to Demonstrate a S,
8/17/2019 Programs in Java
72/196
pulic static 0oid mainBstring argsGHC
F
alance8estne alanceBI+day SatyaIE"%%C
test.shoBC
K
K
!+8P+8 M
+day Satya M ["%%
"1 Program to Demonstrate *6ception
class *6cep
F
static 0oid meth1BC
F
int d%
int a1%Td
K
pulic static 0oid mainBString aGHC
F
*6cep.meth1BC
K
K
gcomputers softare training centrePage '2
8/17/2019 Programs in Java
73/196
8/17/2019 Programs in Java
74/196
!+8P+8 M
Array ,nde6 !ut !f ound :a0a.lang.Array,nde6!ut!founds*6ceptionM 34
"3 Program to (A8(9 A),89
8/17/2019 Programs in Java
75/196
K
!+8P+8 M
Di0ision y 7ero
After tryT(atch Statements
"4 Program to demonstrate
8/17/2019 Programs in Java
76/196
F
System.out.printlnBI@our Array si7e is smallIC
K
catchB*6ception eC
F
System.out.printlnBI!ther type of *6ceptionIC
K
KK
!+8P+8 M
l %
Please *nter (ommand line Arguments
"" Program to demonstrate N*S8*D 8)@
class Nest8ry
F
pulic static 0oid mainBString argsGHC
F
try F
int a args.length
int 42 Ta
System.out.printlnBIa IQ aC
try F
ifBa1C a aTBaLaC
gcomputers softare training centrePage '$
8/17/2019 Programs in Java
77/196
if Ba2C F
int cGH F1K
cG42H //
K
K catchBArray,nde6!ut!founds*6ception eC F
System.out.printlnBIArray inde6 outLofLoundsMIQeC
K
K catchBArithmetic*6ception eC F
System.out.printlnBIDi0ide y %MIQeC
K
K
K
!+8P+8 M
Di0ide y %M:a0a.lang.Arithmetic*6ceptionM T y 7ero
"$ Program to demonstrate &,NA@ clause
class trydemoF
pulic static 0oid mainBString argsGHCF
tryF
int largs.length
System.out.printlnBIl I QlC
int 34Tl
int cGHF%K
cG34H1%%
gcomputers softare training centrePage ''
8/17/2019 Programs in Java
78/196
System.out.printlnBI*nd of 8ry locIC
K
catchBArithmetic*6ception eC
F
System.out.printlnBIPlease *nter (ommand lineArgumentsIC
K
catchBArray,nde6!ut!founds*6ception eC
F
System.out.printlnBI@our Array si7e is smallIC
K
catchB*6ception eC
F
System.out.printlnBI!ther type of *6ceptionIC
K
#nallyF
System.out.printlnBI8his ill e e6ecutedIC
K
KK
!+8P+8 M
l %
Please *nter (ommand line Arguments
8his ill e e6ecuted
"' Program to Demonstrate +ser De#ned *>(*P8,!NS
gcomputers softare training centrePage '-
8/17/2019 Programs in Java
79/196
class
8/17/2019 Programs in Java
80/196
called computeB34C
normal e6it
called computeB//C
caught
8/17/2019 Programs in Java
81/196
,nside thro!ne.
(aught :a0a.lang.,llegalAccess*6ceptionM demo
"/ Program to demonstrate
8/17/2019 Programs in Java
82/196
pulic 0oid runBC
F
forBint 1"QQC
System.out.printlnBItfrom 8hread ( M I QC
System.out.printlnBI*6it from (IC
KK
class 8hreadtest
F
pulic static 0oid mainBString argsGHC
F
ne ABC.startBC
ne BC.startBC
ne (BC.startBC
tryF 8hread.sleepB1%C
K
catchB,nterrupted*6ception eCF
System.out.printlnBI*6ception I QeC
K
K
K
!+8P+8 M
from 8hread A M i 1
from 8hread A M i 2
gcomputers softare training centrePage -2
8/17/2019 Programs in Java
83/196
from 8hread A M i 3
from 8hread A M i 4
from 8hread A M i "
*6it from A
from 8hread M : 1
from 8hread ( M 1
from 8hread M : 2
from 8hread ( M 2
from 8hread M : 3
from 8hread ( M 3
from 8hread M : 4
from 8hread ( M 4
from 8hread M : "
from 8hread ( M "
*6it from
*6it from (
$% Program to Demonstrate )unnale ,nterface
class A implements )unnale
F
pulic 0oid runBC
F
forBint i1i"iQQC
System.out.printlnBItfrom 8hread A M i I QiC
System.out.printlnBI*6it from AIC
KK
gcomputers softare training centrePage -3
8/17/2019 Programs in Java
84/196
class implements )unnale
F
pulic 0oid runBC
F
forBint :1:":QQC
System.out.printlnBItfrom 8hread M : I Q:C
System.out.printlnBI*6it from IC
KK
class ( implements )unnaleF
pulic 0oid runBCF
forBint 1"QQC
System.out.printlnBItfrom 8hread ( M I QCSystem.out.printlnBI*6it from (IC
KK
class )unnaletestF
pulic static 0oid mainBString argsGHCF
A a ne ABC
ne BC
( c ne (BC
8hread t1 ne 8hreadBaC
t1.startBC
8hread t2 ne 8hreadBC
t2.startBC
8hread t3 ne 8hreadBcC
gcomputers softare training centrePage -4
8/17/2019 Programs in Java
85/196
t3.startBC
tryF
8hread.sleepB1%C
K
catchB,nterrupted*6ception eCF
System.out.printlnBI*6ception I QeC
KKK
!+8P+8 M
from 8hread A M i 1
from 8hread M : 1
from 8hread ( M 1
from 8hread A M i 2
from 8hread M : 2
from 8hread ( M 2
from 8hread A M i 3 from 8hread M : 3
from 8hread ( M 3
from 8hread A M i 4
from 8hread M : 4
from 8hread ( M 4
from 8hread A M i "
from 8hread M : "
from 8hread ( M "
*6it from A
*6it from
*6it from (
gcomputers softare training centrePage -"
8/17/2019 Programs in Java
86/196
8/17/2019 Programs in Java
87/196
class Prioritytest F
pulic static 0oid mainBString argsGHCF
A a ne ABC
ne BC
( c ne (BC
c.setPriorityB8hread.YP),!),8@C
.setPriorityBa.getPriorityBCQ1C
a.setPriorityB8hread.
8/17/2019 Programs in Java
88/196
from 8hread ( M 3
from 8hread M : 3
from 8hread A M i 3
from 8hread ( M 4
from 8hread M : 4
from 8hread A M i 4
from 8hread ( M "
from 8hread M : "
from 8hread A M i "
*6it from (
*6it from
*6it from A
gcomputers softare training centrePage --
8/17/2019 Programs in Java
89/196
$2 Program to Demonstrate Synchroni7ed loc
class (allme
F
0oid callBString msgC
F
System.out.printBIGI QmsgC
try F
8hread.sleepB1%%%C
K
catchB,nterrupted*6ception eC
F
System.out.printlnBI,nterruptedIC
K
System.out.printlnBIHICK
K
class (aller implements )unnale
F
String msg
(allme target
8hread t
pulic (allerB(allme targE String sC
F
targettarg
msgs
gcomputers softare training centrePage -/
8/17/2019 Programs in Java
90/196
tne 8hreadBthisC
t.startBC
K
pulic 0oid runBC
F
synchroni7edBtargetC F
target.callBmsgC
KKK
class Synchloc
F
pulic static 0oid mainBString argsGHC
F
(allme targetne (allmeBC
(aller o1ne (allerBtargetE I9elloIC(aller o2ne (allerBtargetE ISynchroni7edIC
(aller o3ne (allerBtargetE I5orldIC
try
F
o1.t.:oinBC
o2.t.:oinBC
o3.t.:oinBC
K
catchB,nterrupted*6ception eC
F
System.out.printlnBI,nterruptedIC
K
K
gcomputers softare training centrePage /%
8/17/2019 Programs in Java
91/196
8/17/2019 Programs in Java
92/196
(allme target
8hread t
pulic (allerB(allme targE String sC
F
targettarg
msgs
tne 8hreadBthisC
t.startBC
K
pulic 0oid runBC
F
target.callBmsgC
KK
class Synch
8/17/2019 Programs in Java
93/196
F
System.out.printlnBI,nterruptedIC
K
K
K
!+8P+8 M
G9elloH
GSynchroni7edH
G5orldH
$4 Program to Demonstrate Deadloc
class A F
synchroni7ed 0oid fooB C F
String name8hread.current8hreadBC.getNameBC
System.out.printlnBnameQ I entered A.fooIC
try F
8hread.sleepB1%%%C
K
catchB*6ception eC F
System.out.printlnBIA ,nterruptedIC
K
System.out.printlnBnameQ I trying to call .lastBCIC
.lastBC
K
gcomputers softare training centrePage /3
8/17/2019 Programs in Java
94/196
synchroni7ed 0oid lastBC
F
System.out.printlnBI,nside A.lastIC
KK
class F
synchroni7ed 0oid arBA aC F
String name8hread.current8hreadBC.getNameBC
System.out.printlnBnameQ I entered .arIC
try F
8hread.sleepB1%%%C
K
catchB*6ception eC F
System.out.printlnBI ,nterruptedIC
K
System.out.printlnBnameQ I trying to call A.lastBCICa.lastBC
K
synchroni7ed 0oid lastBC F
System.out.printlnBI,nside A.lastIC
KK
class Deadloc implements )unnale
F
A ane ABC
ne BC
gcomputers softare training centrePage /4
8/17/2019 Programs in Java
95/196
DeadlocBC
F
8hread.current8hreadBC.setNameBImain8hreadIC
8hread tne 8hreadBthisE I)acing8hreadIC
t.startBC
a.fooBC
System.out.printlnBIac in main threadIC
K
pulic 0oid runBC
F
.arBaC
System.out.printlnBIac in other threadIC
K
pulic static 0oid mainBString argsGHC
F
ne DeadlocBCK
K
!+8P+8 M
main8hread entered A.foo
)acing8hread entered .ar
main8hread trying to call .lastBC
)acing8hread trying to call A.lastBC
gcomputers softare training centrePage /"
8/17/2019 Programs in Java
96/196
$" Program to Demonstrate 8hread
8/17/2019 Programs in Java
97/196
notifyBC
KK
class Producer implements )unnale F
;
ProducerB; C F
this.
ne 8hreadBthisE IProducerIC.startBC
K
pulic 0oid runBC F
int i%
hileBtrueC F
.putBiQQC
KKK
class (onsumer implements )unnale F;
(onsumerB; C F
this.
ne 8hreadBthisE I(onsumerIC.startBC
K
pulic 0oid runBC F
int i%
hileBtrueC F
.getBC
KKK
class P(&i6ed F
pulic static 0oid mainBString argsGHC F
gcomputers softare training centrePage /'
8/17/2019 Programs in Java
98/196
; ne ;BC
ne ProducerBC
ne (onsumerBC
System.out.printlnBIPress (ontrolL( to stop.IC
KK
!+8P+8 M
Put M 1
?ot M 1
Put M 2
?ot M 2
Put M 3
?ot M 3
Put M 4
?ot M 4Put M "
?ot M "
gcomputers softare training centrePage /-
8/17/2019 Programs in Java
99/196
$$ Program to (reate 8hreads using 8hread (lass
class A e6tends 8hread
F
pulic 0oid runBC
F
forBint i1i"iQQC
System.out.printlnBItfrom 8hread A M i I QiC
System.out.printlnBI*6it from AIC
K
K
class e6tends 8hreadF
pulic 0oid runBC
F
forBint :1:":QQC
System.out.printlnBItfrom 8hread M : I Q:C
System.out.printlnBI*6it from IC
K
K
class 8hreadtest
F
gcomputers softare training centrePage //
8/17/2019 Programs in Java
100/196
pulic static 0oid mainBString argsGHC
F
ne ABC.startBC
ne BC.startBC
tryF
8hread.sleepB1%C
K
catchB,nterrupted*6ception eC
F
System.out.printlnBI*6ception I QeC
K
K
K
!+8P+8 M
from 8hread A M i 1
from 8hread M : 1
from 8hread A M i 2
from 8hread M : 2
from 8hread A M i 3
from 8hread M : 3
from 8hread A M i 4
from 8hread M : 4
from 8hread A M i "
from 8hread M : "
*6it from A
*6it from
gcomputers softare training centrePage 1%%
8/17/2019 Programs in Java
101/196
8/17/2019 Programs in Java
102/196
System.out.printlnBI8hread !ne is ali0eM IQo1.t.isAli0eBCC
System.out.printlnBI8hread 8o is ali0eM IQo2.t.isAli0eBCC
System.out.printlnBI8hread 8hree is ali0eM IQo3.t.isAli0eBCC
try F
System.out.printlnBI5aiting for threads to #nish.IC
o1.t.:oinBC
o2.t.:oinBC
o3.t.:oinBC
K
catch B,nterrupted*6ception ieC F
System.out.printlnBI
8/17/2019 Programs in Java
103/196
8hreeM "
8hread 8o is ali0eM true
8hread 8hree is ali0eM true
5aiting for threads to #nish.
8oM 4
!neM 4
8hreeM 4
8oM 3
!neM 3
8hreeM 3
8oM 2
!neM 2
8hreeM 2
8oM 1
!neM 1
8hreeM 1 8oe6iting.
!nee6iting.
8hreee6iting.
8hread !ne is ali0eM false
8hread 8o is ali0eM false
8hread 8hree is ali0eM false
8/17/2019 Programs in Java
104/196
import :a0a.io.R
class ?et+ser,nput
F
pulic static 0oid mainBStringGH argsC
F
TTthe data that ill e entered y the user
String name
TTan instance of the uered)eader class
TTill e used to read the data
uered)eader reader
TTspecify the reader 0ariale
TTto e a standard input uer
reader ne uered)eaderBne,nputStream)eaderBSystem.inCC
TTas the user for their name
System.out.printBI5hat is your name\ IC
tryF
TTread the data entered y the user using
TTthe readineBC method of the uered)eader class
TTand store the 0alue in the name 0ariale
name reader.readineBC
TTprint the data entered y the user
System.out.printlnBI@our name is I Q nameC
K
catch B,!*6ception ioeCF
TTstatement to e6ecute if an inputToutput e6ceptionoccurs
System.out.printlnBIAn une6pected error occured.IC
K
K
gcomputers softare training centrePage 1%4
8/17/2019 Programs in Java
105/196
8/17/2019 Programs in Java
106/196
pulic 0oid initBC F
fne &rame8estBIA frame 5indoIC
f.setSi7eB3%%E3%%C
f.setVisileBtrueC
Kpulic 0oid startBC F
f.setVisileBtrueC
K
pulic 0oid stopBC F
f.setVisileBfalseC
K
pulic 0oid paintB?raphics gC F
g.draStringBI8his is a 8est appletIE1%E2%C
KK
gcomputers softare training centrePage 1%$
8/17/2019 Programs in Java
107/196
'% Program to Demonstrate ife cycle of methods inApplet
import :a0a.at.R
import :a0a.applet.R
TR
applet codeISampleI idth4%% height2%%O
TappletORT
pulic class Sample e6tends Applet F
String msg
pulic 0oid initBC F
setacgroundB(olor.cyanC
set&oregroundB(olor.redC
msgI,nside initBCLLI
K
pulic 0oid startBC F
msg QI,nside startBCLLI
K
pulic 0oid paintB?raphics gC F
msg QI,nside paintBCLLI
g.draStringBmsgE1%E3%C
KK
gcomputers softare training centrePage 1%'
8/17/2019 Programs in Java
108/196
gcomputers softare training centrePage 1%-
8/17/2019 Programs in Java
109/196
'1 Program to display Parameters of 98
8/17/2019 Programs in Java
110/196
fontSi7e%
K
catchBNumer&ormat*6ception eC F
fontSi7eL1
K
param getParameterBIleadingIC
try F
ifBparam X nullC
leading &loat.0alue!fBparamC.WoatValueBC
else
leading%
K
catchBNumer&ormat*6ception eC F
leadingL1
K
paramgetParameterBIaccount*naledICifBparam X nullC
acti0e oolean.0alue!fBparamC.ooleanValueBC
K
pulic 0oid paintB?raphics gC F
g.draStringBI&ont nameM IQfontNameE%E1%C
g.draStringBI&ont si7eM IQfontSi7eE%E2$C
g.draStringBIeadingM IQleadingE%E42C
g.draStringBIAccount Acti0eM IQacti0eE%E"-C
K
K
gcomputers softare training centrePage 11%
8/17/2019 Programs in Java
111/196
'2 Program to Demonstrate all shapes in ?raphics class
import :a0a.at.R
import :a0a.applet.R
TR
applet codeI&igPaintI idth4%% height2%%O
TappletO
RT
pulic class &igPaint e6tends Applet
F
pulic 0oid paintB?raphics gC
F
g.draineB%E%E1%%E1%%Cg.dra)ectB1%E1%E$%E"%C
gcomputers softare training centrePage 111
8/17/2019 Programs in Java
112/196
g.#ll)ectB1%%E1%E$%E"%C
g.dra!0alB1%E1%E"%E"%C
g.#ll!0alB1%%E1%E'"E"%C
g.draArcB1%E4%E'%E'%E%E'"C
g.#llArcB1%%E4%E'%E'%E%E'"C
K
K
gcomputers softare training centrePage 112
8/17/2019 Programs in Java
113/196
'3 Program to Sho a 9utE
8/17/2019 Programs in Java
114/196
g.draineB1-%E34%E22%E1"%C
g.draineB22%E1"%E2$%E32%C
g.draineB2$%E32%E31"E1$"C
g.draineB31"E1$"E33"E21%C
g.dra!0alB33%E3"%E13%E4%C
g.dra!0alB""%E$%E$%E$%C
g.dra!0alB"$"E-%E1%E1%C
g.dra!0alB"-"E-%E1%E1%C
g.draArcB"$$E-1E3%E3%E%EL1-%C
g.draineB"-%E12%E"-%E3%%C
g.draineB"2%E1/%E"-%E1$%C
g.draineB"-%E1$%E$4%E1/%C
g.draineB"3%E34%E"-%E3%%C
g.draineB"-%E3%%E$3%E34%C
K
K
gcomputers softare training centrePage 114
8/17/2019 Programs in Java
115/196
'4 Program to Sho Status of Applet 5indo
import :a0a.at.R
import :a0a.applet.R
TR
applet codeIStatus5indoI idth4%% height2%%O
TappletO
RT
pulic class Status5indo e6tends Applet F
pulic 0oid initBC F
setacgroundB(olor.cyanC
K
pulic 0oid paintB?raphics gCF
g.draStringBI8his is in the applet indo.IE1%E2%C
shoStatusBI8his is shon in the status indo.IC
K
K
gcomputers softare training centrePage 11"
8/17/2019 Programs in Java
116/196
'" Program to Sho Position of the 3%E mouse@3%
pulic App5indoBC
F
add=eyistenerBne
8/17/2019 Programs in Java
117/196
8/17/2019 Programs in Java
118/196
App5indo app5indo
pulic
8/17/2019 Programs in Java
119/196
import :a0a.at.R
import :a0a.applet.R
TR
applet codecolordemo idth4%% height2%%O
TappletO
RT
pulic class (olordemo e6tends AppletF
pulic 0oid paintB?raphics gCF
(olor c1 ne (olorB2""E1%%E1%%C
(olor c2 ne (olorB1%E2""E1%%C
(olor c3 ne (olorB1%%E2%%E2""C
g.set(olorBc1C
g.draineB11%E"%E1%%E1%%C
g.draineB13%E1%%E1%%E4%C
g.set(olorBc2C
g.#ll)ectB$%E12%E"%E"%Cg.set(olorBc3C
g.#ll!0alB2%E3%E'%E'%C
g.set(olorB(olor.lacC
g.#ll)ectB12%E"%E1-%E/%C
KK
gcomputers softare training centrePage 11/
8/17/2019 Programs in Java
120/196
8/17/2019 Programs in Java
121/196
g.draStringBI a IQa QI IQE2%E"%C
g.draStringBI
8/17/2019 Programs in Java
122/196
8/17/2019 Programs in Java
123/196
TR
applet codeIDra,mageApp.classI height2%%idth4%%O
TappletO
RT
pulic class Dra,mageApp e6tends Applet F
,mage i
pulic 0oid startBC
F
i get,mageBgetDocumentaseBCEIsunWoer.:pgIC
K
pulic 0oid paintB?raphics gC
F
g.dra,mageBiE"%E"%EthisC
K
K
gcomputers softare training centrePage 123
8/17/2019 Programs in Java
124/196
-% Program to Sho an ,mage
import :a0a.at.R
import :a0a.applet.R
TR
applet codeISimpleannerI idth4%% height2%%O
TappletO
RT
pulic class Simpleanner e6tends Applet implements)unnale
F
String msg I A Simple
8/17/2019 Programs in Java
125/196
pulic 0oid runBC F
char ch
TT Display anner
forB C F
try F
repaintBC
8hread.sleepB2"%C
ch msg.charAtB%C
msg msg.sustringB1E msg.lengthBCC
msg Q ch
ifBstop&lagC
rea
K catchB,nterrupted*6ception eC FK
K
KTT Pause the anner.
pulic 0oid stopBC F
stop&lag true
t null
K
TT Display the anner.
pulic 0oid paintB?raphics gC F
g.draStringBmsgE "%E 3%C
K
K
gcomputers softare training centrePage 12"
8/17/2019 Programs in Java
126/196
-1 Program to demonstrate an Adapter
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet codeIAdapterDemoI idth3%% height1%%O
TappletO
RT
pulic class AdapterDemo e6tends Applet F
gcomputers softare training centrePage 12$
8/17/2019 Programs in Java
127/196
8/17/2019 Programs in Java
128/196
gcomputers softare training centrePage 12-
8/17/2019 Programs in Java
129/196
-2 Program to Demonstrate utton
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
TRapplet codedemoutton.class height2%% idth4%%O
TappletORT
pulic class demoutton e6tends Applet implementsActionistener
F
String msgII
utton 1E2
pulic 0oid initBC
F
1ne uttonBI!=IC
2ne uttonBI(AN(*IC
addB1C
addB2C
1.addActionistenerBthisC
2.addActionistenerBthisC
K
pulic 0oid actionPerformedBAction*0ent eC
F
String se.getAction(ommandBC
ifBs.eualsBI!=ICC
gcomputers softare training centrePage 12/
8/17/2019 Programs in Java
130/196
F
msgI@ou pressed !=I
K
else
F
msgI@ou pressed (AN(*I
K
repaintBC
K
pulic 0oid paintB?raphics gCF
g.draStringBmsgE4%E4%C
K
K
gcomputers softare training centrePage 13%
8/17/2019 Programs in Java
131/196
gcomputers softare training centrePage 131
8/17/2019 Programs in Java
132/196
-3 Program to Demonstrate (heco6
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
TRapplet codedemocheco6.class height2%%idth4%%O
TappletORT
pulic class democheco6 e6tends Applet implements
,temistener F
String msgII
(heco6 c1Ec2Ec3
pulic 0oid initBC F
c1ne (heco6BIJAVAIEnullEtrueC
c2ne (heco6BIVIC
c3ne (heco6BI!)A(*IC
addBc1C
addBc2C
addBc3C
c1.add,temistenerBthisC
c2.add,temistenerBthisC
c3.add,temistenerBthisC
K
pulic 0oid itemState(hangedB,tem*0ent eC F
repaintBC
K
gcomputers softare training centrePage 132
8/17/2019 Programs in Java
133/196
pulic 0oid paintB?raphics gC F
msgIJAVAMIQc1.getStateBC
g.draStringBmsgE4%E4%C
msgIVMIQc2.getStateBC
g.draStringBmsgE4%E$%C
msgI!)A(*MIQc3.getStateBC
g.draStringBmsgE4%E-%C
KK
-4 Program to Demonstrate )adio uttonsB(heco6?roupC
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
gcomputers softare training centrePage 133
8/17/2019 Programs in Java
134/196
TRapplet codedemocheco6grp.class height2%%idth4%%O
TappletORT
pulic class democheco6grp e6tends Appletimplements ,temistener F
String msgII
(heco6 c1Ec2Ec3
(heco6?roup cg
pulic 0oid initBC F
cgne (heco6?roupBC
c1ne (heco6BIJAVAIEcgEtrueC
c2ne (heco6BIVIEcgEfalseC
c3ne (heco6BI!)A(*IEcgEfalseC
addBc1C
addBc2C
addBc3C
c1.add,temistenerBthisC
c2.add,temistenerBthisC
c3.add,temistenerBthisC
K
pulic 0oid itemState(hangedB,tem*0ent eC F
repaintBC
K
pulic 0oid paintB?raphics gC F
msgIcurently
selectedMIQcg.getSelected(heco6BC.getaelBC
gcomputers softare training centrePage 134
8/17/2019 Programs in Java
135/196
g.draStringBmsgE4%E4%C
K
K
gcomputers softare training centrePage 13"
8/17/2019 Programs in Java
136/196
-" Program to Demonstrate (hoice
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
TRapplet codedemochoice.class height2%% idth4%%O
TappletORT
pulic class demochoice e6tends Applet implements,temistener F
(hoice c
pulic 0oid initBC F
cne (hoiceBC
c.add,temBI)edIC
c.add,temBI?reenIC
c.add,temBIlueIC
addBcC
c.add,temistenerBthisC K
pulic 0oid itemState(hangedB,tem*0ent eC F
String stc.getSelected,temBC
ifBst.eualsBI)edICC F
setacgroundB(olor.redC K
ifBst.eualsBI?reenICC F
setacgroundB(olor.greenC K
ifBst.eualsBIlueICC F
setacgroundB(olor.lueCKKK
gcomputers softare training centrePage 13$
8/17/2019 Programs in Java
137/196
-$ Program to Demonstrate &ile Dialog
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TRapplet codeDemo
8/17/2019 Programs in Java
138/196
set
8/17/2019 Programs in Java
139/196
i'.addActionistenerBhandlerC
i-.addActionistenerBhandlerC
i1%.addActionistenerBhandlerC
i11.addActionistenerBhandlerC
oro.add,temistenerBhandlerC
K
pulic 0oid paintB?raphics gC F
g.draStringBmsgE1%E1%%C
ifBoro.getStateBCC
g.draStringBI(hecedIE1%E2%%C
else
g.draStringBI+nchecedIE1%E2%%C
KK
class
8/17/2019 Programs in Java
140/196
else ifBs.eualsBI!pen....ICC F
&ileDialog fopenne &ileDialogBtmEI!pen a&ileIE&ileDialog.!ADC
fopen.setVisileBtrueC
msgI@ou selected IQfopen.getDirectoryBCQIIQfopen.get&ileBC
K
else ifBs.eualsBISa0e....ICC F
&ileDialog fsa0ene &ileDialogBtmEISa0e a&ileIE&ileDialog.SAV*C
fsa0e.setVisileBtrueC
KK
pulic 0oid itemState(hangedB,tem*0ent ieC F
tm.repaintBC
KK
class 8estDialog e6tends Dialog implements Actionistener F
&rame f
String s
utton E1
8estDialogB&rame fEString titleC F
superBfEtitleEfalseC
this.ff
this.stitle
setayoutBne &loayoutBCC
setSi7eB2%%E4%%C
addBne aelBIAre you sure you ant to e6itICC
addBne uttonBI@esICC
addB1ne uttonBINoICC
gcomputers softare training centrePage 14%
8/17/2019 Programs in Java
141/196
.addActionistenerBthisC
1.addActionistenerBthisC
K
pulic 0oid actionPerformedBAction*0ent eC F
ifBe.getSourceBC CF
disposeBC
f.setVisileBfalseC
K
else F
disposeBC
KKK
pulic class Demo&ileDialog e6tends Applet F
&rame f
pulic 0oid initBC F
fne 8est
8/17/2019 Programs in Java
142/196
-' Program to Demonstrate aels
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
TRapplet codedemolael.class height2%%idth4%%O
TappletORT
pulic class demolael e6tends Applet
gcomputers softare training centrePage 142
8/17/2019 Programs in Java
143/196
F
pulic 0oid initBC
F
ael nmne aelBINameIC
ael addrne aelBIAddressIC
TTadd laels to indo
addBnmC
addBaddrC
K
K
-- Program to Demonstrate ists
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
gcomputers softare training centrePage 143
8/17/2019 Programs in Java
144/196
applet codeIistDemoI idth4%% height2%%O
TappletO
RT
pulic class istDemo e6tends Applet implementsActionistener F
ist osEroser
String msg II
pulic 0oid initBC F
osne istB4EtrueC
roserne istB4EfalseC
os.addBIindos /-IC
os.addBI5indos N8IC
os.addBISolarisIC
os.addBI
8/17/2019 Programs in Java
145/196
pulic 0oid actionPerformedBAction*0ent aeC F
repaintBC
K
pulic 0oid paintB?raphics gC F
int id6GH
msgI(urrent !SMI
id6os.getSelected,nde6esBC
forBint i% iid6.length iQQC
msg Q os.get,temBid6GiHC Q I I
g.draStringBmsgE$E12%C
msgI(urrent roserM Imsg Q roser.getSelected,temBC
g.draStringBmsgE$E14%C
KK
gcomputers softare training centrePage 14"
8/17/2019 Programs in Java
146/196
-/ Program to Demonstrate
8/17/2019 Programs in Java
147/196
set
8/17/2019 Programs in Java
148/196
i'.addActionistenerBhandlerC
i-.addActionistenerBhandlerC
i1%.addActionistenerBhandlerC
i11.addActionistenerBhandlerC
oro.add,temistenerBhandlerC
K
pulic 0oid paintB?raphics gC
F
g.draStringBmsgE1%E2%%C
ifBoro.getStateBCC
g.draStringBI(hecedIE1%E2%%C
else
g.draStringBI+nchecedIE1%E2%%C
KK
class
8/17/2019 Programs in Java
149/196
8/17/2019 Programs in Java
150/196
else F
disposeBC
KKK
pulic class Demo
8/17/2019 Programs in Java
151/196
gcomputers softare training centrePage 1"1
8/17/2019 Programs in Java
152/196
/% Program to Demonstrate 8e6t Area
import :a0a.applet.R
import :a0a.at.R
import :a0a.at.e0ent.R
pulic class Demo8e6tArea e6tends Applet implementsActionistener F
8e6tArea remar
utton 1
String msgII
pulic 0oid initBC F
remarne 8e6tAreaBI*nter your remarsIE"E"%C
1ne uttonBIShoIC
addBremarC
addB1C
1.addActionistenerBthisCK
pulic 0oid actionPerformedBAction*0ent eC F
String se.getAction(ommandBC
ifBs.eualsBIShoICC F
msgremar.get8e6tBC
repaintBC KK
pulic 0oid paintB?raphics gCF
g.draStringBmsgE13%E1$%C KK
gcomputers softare training centrePage 1"2
8/17/2019 Programs in Java
153/196
gcomputers softare training centrePage 1"3
8/17/2019 Programs in Java
154/196
/1 Program to
8/17/2019 Programs in Java
155/196
msgIogin SuccessfulI
K
else
msgI@ou cannot login.(hec your username andpassordI
K
elseF
un.set8e6tBIIC
pass.set8e6tBIIC
msgII
K
repaintBC
K
pulic 0oid paintB?raphics gCF
g.draStringBmsgE4%E4%C
KKTTend of class
gcomputers softare training centrePage 1""
8/17/2019 Programs in Java
156/196
gcomputers softare training centrePage 1"$
8/17/2019 Programs in Java
157/196
8/17/2019 Programs in Java
158/196
un.set8e6tBI*nter +serNameIC
KK
pulic 0oid focusostB&ocus*0ent feC F
ifBfe.getSourceBCpassC F
ifBpass.get8e6tBC.eualsBIICC F
msgI*nter the passord to loginI
repaintBC
KKK
pulic 0oid actionPerformedBAction*0ent eC F
String se.getAction(ommandBC
ifBs.eualsBIVerifyICCF
String userun.get8e6tBC
String pdpass.get8e6tBC
ifBuser.eualsBIstudentIC pd.eualsBIloyalaICC F
msgIogin SuccessfulI
Kelse
msgI@ou cannot login.(hec your username andpassordI
K
else F
un.set8e6tBIIC
pass.set8e6tBIIC
msgII
K
repaintBC
K
pulic 0oid paintB?raphics gCF
g.draStringBmsgE4%E4%C
gcomputers softare training centrePage 1"-
8/17/2019 Programs in Java
159/196
KKTTend of class
gcomputers softare training centrePage 1"/
8/17/2019 Programs in Java
160/196
/3 Program to Demonstrate
8/17/2019 Programs in Java
161/196
gcomputers softare training centrePage 1$1
8/17/2019 Programs in Java
162/196
/4 Program to Demonstrate
8/17/2019 Programs in Java
163/196
msgImousepressedI
repaintBCK
pulic 0oid mouse)eleasedB
8/17/2019 Programs in Java
164/196
/" Program to Demonstrate the ey e0ent handlers
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet codeISimple=eyI idth4%% height2%%O
TappletO
RT
pulic class Simple=ey e6tends Applet
implements =eyistener F
String msg II
int > 1%E @ 2% TT output coordinates
pulic 0oid initBC F
add=eyistenerBthisC
gcomputers softare training centrePage 1$4
8/17/2019 Programs in Java
165/196
reuest&ocusBC TT reuest input focus
K
pulic 0oid eyPressedB=ey*0ent eC F
shoStatusBI=ey DonIC
K
pulic 0oid ey)eleasedB=ey*0ent eC F
shoStatusBI=ey +pIC
K
pulic 0oid ey8ypedB=ey*0ent eC F
msg Q e.get=ey(harBC
repaintBC
K
TT Display eystroes.
pulic 0oid paintB?raphics gC F
g.draStringBmsgE >E @C
KK
gcomputers softare training centrePage 1$"
8/17/2019 Programs in Java
166/196
gcomputers softare training centrePage 1$$
8/17/2019 Programs in Java
167/196
/$ Program to Demonstrate order ayout
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet code8est idth4%% height2%%O
TappletO
RT
pulic class 8est e6tends Applet F
pulic 0oid initBC F
setayoutBne orderayoutBCC
addBneScrollarBScrollar.9!),^!N8ACEorderayout.N!)89C
addBne
ScrollarBScrollar.9!),^!N8ACEorderayout.S!+89C
addBne ScrollarBScrollar.V*)8,(ACEorderayout.*AS8C
addBne ScrollarBScrollar.V*)8,(ACEorderayout.5*S8C
add Bne 8e6tAreaBI, am in thecenterICEorderayout.(*N8*)C
KK
gcomputers softare training centrePage 1$'
8/17/2019 Programs in Java
168/196
/' Program to Demonstrate (ard ayout
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet code8est( idth4%% height1"%O
TappletO
RT
pulic class 8est( e6tends Applet implementsActionistener F
Panel pEp1Ep2
(ardayout cl1
gcomputers softare training centrePage 1$-
8/17/2019 Programs in Java
169/196
ael l1
utton 1E2
(heco6?roup cg
(heco6 c1Ec2
pulic 0oid initBC F
l1ne aelBIicesence AgreementIC
1ne uttonBIAcceptIC
2ne uttonBI*6itIC
cl1ne (ardayoutBC
pne PanelBC
p.setayoutBcl1C
p1ne PanelBC
p1.addBl1C
p1. addB1C
p1.addB2C
p2ne PanelBC cgne (heco6?roupBC
c1ne (heco6BI8ypicalIEcgEtrueC
c2ne (heco6BI(ustomIEcgEfalseC
p2.addBc1C
p2.addBc2C
p.addBp1EIpanel1IC
p.addBp2EIpanel2IC
addBpC
1.addActionistenerBthisC
K
pulic 0oid actionPerformedBAction*0ent eC F
ifBe.getSourceBC 1C F
cl1.shoBpEIpanel2IC
gcomputers softare training centrePage 1$/
8/17/2019 Programs in Java
170/196
KK
gcomputers softare training centrePage 1'%
8/17/2019 Programs in Java
171/196
8/17/2019 Programs in Java
172/196
case 2M
tfonts.fne &ontBISansSerifIE&ont.!DE22C
rea
case 3M
tfonts.fne &ontBISerifIE&ont.!DE24C
rea
case 4M
tfonts.fne &ontBI
8/17/2019 Programs in Java
173/196
// Program to Demonstrate ?rid ayouts
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet code8est? idth4%% height2%%O
TappletO
RT
pulic class 8est? e6tends AppletF
pulic 0oid initBCF
setayoutBne &loayoutB&loayout.(*N8*)CC
addBne 8e6t&ieldBI%ICC
setayoutBne ?ridayoutB3E3CC
set&ontBne &ontBISansSerifIE&ont.!DE1$CC
forBint i%i3iQQCF
forBint :%:3:QQCF
gcomputers softare training centrePage 1'3
8/17/2019 Programs in Java
174/196
int iR3Q:
ifBO%C
addBne uttonBIIQCC
KKKK
gcomputers softare training centrePage 1'4
8/17/2019 Programs in Java
175/196
1%% Program to Demonstrate Scrollar
import :a0a.at.R
import :a0a.at.e0ent.R
import :a0a.applet.R
TR
applet code8estScrollar.class idth4%% height2%%O
TappletO
RT
pulic class 8estScrollar e6tends Applet implementsAd:ustmentistener
F
Scrollar s1Es2Es3
pulic 0oid initBC
F
s1ne ScrollarBScrollar.9!),^!N8AE1%%E1%E%E2""C
s2ne ScrollarBScrollar.V*)8,(AE"%E1%E%E2""C
s3ne ScrollarBScrollar.V*)8,(AE1"%E1%E%E2""C
setayoutBne orderayoutBCC
addBs1Eorderayout.S!+89C
addBs2Eorderayout.*AS8C
addBs3Eorderayout.5*S8C
TTregister to recie0e ad:ustment e0ents
s1.addAd:ustmentistenerBthisC
s2.addAd:ustmentistenerBthisC
s3.addAd:ustmentistenerBthisC
K
gcomputers softare training centrePage 1'"
8/17/2019 Programs in Java
176/196
8/17/2019 Programs in Java
177/196
1%1 Program to (hec JD( (onnection
import :a0a.sl.R
import :a0a.util.R
pulic class JD(Demo1
F
pulic static 0oid mainBStringGHargsC thros *6ception
F
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection c Dri0er
8/17/2019 Programs in Java
178/196
!+8P+8 M
(onnection Successful
1%2 Program to retrie0e )ecords from Dataase
import :a0a.sl.R
pulic class 8est)etrie0e)ecords F
pulic static 0oid mainBStringGH argsC thros *6ception F
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnectionconDri0er
8/17/2019 Programs in Java
179/196
System.out.printlnBIRRR *mployeeZs Details RRR nIC
hileBrs.ne6tBCC F
System.out.printlnBI*mployee NumerM I Q rs.get,ntB1CC
System.out.printlnBI*mployee NameM I Q rs.getStringB2CC
System.out.printlnBI*mployee SalaryM I Q rs.get,ntB3C Q InIC
K
rs.closeBC
st.closeBC
con.closeBC
K K
!+8P+8 M
RRR *mployeeZs Details RRR
*mployee NumerM 1
*mployee NameM +day
*mployee SalaryM $%%%
*mployee NumerM 2
*mployee NameM Venat
*mployee SalaryM 4"%%
*mployee NumerM 3
*mployee NameM Satish
*mployee SalaryM '%%%
*mployee NumerM 4
gcomputers softare training centrePage 1'/
8/17/2019 Programs in Java
180/196
*mployee NameM Vi:ay
*mployee SalaryM 2%%%
1%3 Program to demonstrate ,nsertion into Dataase
import :a0a.sl.R
import :a0a.io.R
pulic class ,nsertDemo F
pulic static 0oid mainBStringGH argsC thros *6ception F
int no/
String nameISatyaI
doule sal1%%%%
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection con Dri0er
8/17/2019 Programs in Java
181/196
1%4 Program to demonstrate +pdate into Dataase
import :a0a.util.R
import :a0a.sl.R
import :a0a.io.R
pulic class JD(+pdateDemo F
pulic static 0oid mainBStringGHargsC thros *6ceptionF
int eno/
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection c Dri0er
8/17/2019 Programs in Java
182/196
8/17/2019 Programs in Java
183/196
PreparedStatement pst con.prepareStatementBIinsert into*mpB*mpidENameESalaryC 0aluesB\E\E\CIC
int no1%4
pst.set,ntB1EnoC
String nameI)+VNI
pst.setStringB2EnameC
int sal14%%%
pst.set,ntB3EsalC
int i pst.e6ecute+pdateBC
System.out.printlnBiQILL)ecord insertedIC
pst.closeBC
con.closeBC
K K
!+8P+8 M
1LL)ecord inserted
1%$ Program to Demonstrate Data *ntry using Applet
gcomputers softare training centrePage 1-3
8/17/2019 Programs in Java
184/196
import :a0a.sl.R
import :a0a.at.R
import :a0a.at.e0ent.R
pulic class JD(Data*ntry e6tends &rame implementsActionistener
F
utton oEcancel
8e6t&ield t1Et2Et3
JD(Data*ntryBC
F
setayoutBne &loayoutBCC
one uttonBI!=IC
cancelne uttonBI(ancelIC
t1ne 8e6t&ieldB2%C
t2ne 8e6t&ieldB2%C
t3ne 8e6t&ieldB2%C
addBne aelBI*mployee Numer M ICC
addBt1C
addBne aelBI*mployee Name M ICC
addBt2C
addBne aelBI*mployee Salary M ICC
addBt3C
addBoC
addBcancelC
o.addActionistenerBthisC
cancel.addActionistenerBthisC
set8itleBI*mployeeZs )egistration &ormIC
gcomputers softare training centrePage 1-4
8/17/2019 Programs in Java
185/196
setSi7eB3"%E3%%C
setVisileBtrueC
K
pulic 0oid actionPerformedBAction*0ent aeC
F
ifBae.getSourceBCcancelC
F
t1.set8e6tBIIC
t2.set8e6tBIIC
t3.set8e6tBIIC
K
ifBae.getSourceBCoC
F
int no,nteger.parse,ntBt1.get8e6tBCCString namet2.get8e6tBC
int sal,nteger.parse,ntBt3.get8e6tBCC
try F
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection con
Dri0er
8/17/2019 Programs in Java
186/196
catchB*6ception eC
F
System.out.printlnBI
8/17/2019 Programs in Java
187/196
RRRRRRR)egistraiton performedRRRRRRR
gcomputers softare training centrePage 1-'
8/17/2019 Programs in Java
188/196
1%' Program to Demonstrate )esult Set
8/17/2019 Programs in Java
189/196
No of columns in the taleM 3
*mpid M ,N8*?*)
Name M VA)(9A)
Salary M ,N8*?*)
1%- Program to Demonstrate 8ransaction handling
import :a0a.sl.R
pulic class JD(8ransDemo
F
pulic static 0oid mainBString aGHC
F
tryF
Properties pne PropertiesBCp.setPropertyBIuidIEIscottIC
p.setPropertyBIpassordIEItigerIC
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection con Dri0er
8/17/2019 Programs in Java
190/196
int : st.e6ecute+pdateBIinsert into *mp 0aluesBIQ no Q IEZIQ nameQIZEIQ sal QICIC
System.out.printlnBIsecond ro inserted ut not commitedIC
TTcommit the trans
con.commitBC
System.out.printlnBItrans commitedIC
st.closeBC
con.closeBC
KcatchB*6ception eC
F
System.out.printlnBI*rrorM IQeC
K
K
K
1%/ Program to Demonstrate (allale Statement
import :a0a.sl.R
import :a0a.util.R
pulic class JD((allaleDemo
F
pulic static 0oid mainBStringGHargsC thros *6ception
F
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection c Dri0er
8/17/2019 Programs in Java
191/196
cs.set,ntB1E'-3/C
TTregister the out parameters
cs.register!utParameterB2E8ypes.VA)(9A)C
cs.register!utParameterB3E8ypes.VA)(9A)C
TTprocess the stored procedure
cs.e6ecuteBC
TTretrie0e the data
enamecs.getStringB2C
e:ocs.getStringB3C
TTdisplay the data
System.out.printlnBI*mployee nameM IQenameC
System.out.printlnBI*mployee JoM IQe:oC
cs.closeBC
c.closeBC
K
K
11% Program to Demonstrate )esult Set
8/17/2019 Programs in Java
192/196
(lass.forNameBIsun.:dc.odc.Jdc!dcDri0erIC
(onnection c Dri0er
8/17/2019 Programs in Java
193/196
(onnection c Dri0er
8/17/2019 Programs in Java
194/196
8/17/2019 Programs in Java
195/196
K
catchB *6ception e C
F
System.out.printlnB e C
K
K
K
11" implementation for the interface
import :a0a.rmi.R
import :a0a.rmi.ser0er.+nicast)emote!:ect
pulic class )
8/17/2019 Programs in Java
196/196
K
11$ (reate a Policy to Allo Permission for *0eryone
grant {
// Allow everything
permission java.security.AllPermission;
};