Date post: | 07-Jul-2018 |
Category: |
Documents |
Upload: | parveen-kumar |
View: | 230 times |
Download: | 0 times |
of 26
8/18/2019 Java Full File
1/26
1Date…………… Practical- 1813344
Aim-Introduction to Java.
Objective- To know the basics of Java and rint te!t on the console.
System Requirements- Java installed s"ste# with Java develo#ent kit $JD%&.
Theory-Java is a 'eneral urose( ob)ect oriented ro'ra##in' lan'ua'e develoed b" *un
+icrosoft *"ste#s in 1,,1.ri'inall" called OAK b" Ja#es oslin'. Java /irtual +achine
'ives Java its latfor# indeendence. In Java the 0)avac co##and is the Java co#iler that isused to co#ile the ro'ra#. The Java co##and is used to run the ro'ra#.
There are #an" features in Java( which are as follows 2
1. Compiled And Interpreted Java co#bines both aroaches thus #akin' Java two-*ta'e
s"ste#. Java co#iler converts the source code in to the b"te code instruction. "te codes
are not )ust the #achine instruction and therefore( in the second sta'e( Java interreter
'enerate the #achine code that can be directl" e!ecuted b" the #achine that is runnin' the )ava ro'ra#.
!. "l#t$orm Independent And "ort#ble Java ro'ra# can be easil" #oved fro# one
co#uter s"ste# to another( an"where and an"ti#e. Java ro'ra# also use the concet of
rite nce 5nd 6un 5n"where.%. Object Oriented Java is a true b)ect riented 7an'ua'e. 5l#ost ever"thin' in Java is
the ob)ect. 5ll ro'ra# code and data resides within ob)ects and classes.
&. Robust And Secure Java is a robust lan'ua'e as is rovides so #an" safe'uards to
ensure the reliable code. It also incororated the concet of e!cetion handlin' which
cature series errors and eli#inates an" risk of crashin' the s"ste#. The absence of ointers in Java ensures ro'ra# can not 'ain access to #e#or" location without
authentication.
'. (ultithre#ded +ultithread #eans handlin' #ultile tasks si#ultaneousl". Java suorts
#ultithreaded ro'ra#. This #eans that we need not wait for the alication to finish onetask before be'innin' the another.
Al)orithm-
1. irstl" we write source code of our ro'ra# in note ad.
9. *ave the ro'ra# with 0.)ava e!tension with sa#e na#e as class na#e.3. en console.
4. o to director" where ro'ra# is saved throu'h 0cd co##and $:han'e Director"
co##and&.
;. :o#ile the ro'ra# throu'h 0)avac co##and.
"ro)r#m Code-
class
@
8/18/2019 Java Full File
2/26
2Date…………… Practical- 1813344
ublic *tatic void #ain$*trin' ar'sAB&
@
*"ste#.out.rintln$CelloC&EF
F
Output-
Conclusion- e have learned how Java ro'ra#s are created( saved( co#iled and run.
8/18/2019 Java Full File
3/26
3Date…………… Practical- 1813344
Aim rite a ro'ra# to find the factorial of a nu#ber usin'
• :o##and line ar'u#ents
• *canner class
Objectives In this ractical we have to find the factorial of a nu#ber usin' co##and inlinear'u#ent and *canner class.
Requirements2 ardware2 *tandard Pc
*oftware2 Gotead( )dk
Theory
Comm#nd line #r)ument in j#v# e can ass values to our ro'ra# at runti#e usin'co##and line ar'u#ent. If we want to ass so#e values to the )ava ro'ra# we can sul" it as
co##and line ar'u#ents.
Sc#nner cl#ss 5 si#le te!t *canner which can arse ri#itive t"es and strin's usin' re'ular e!ression. 5 scanner breaks its inut tokens usin' a deli#iter attern ( which b" default
#atches whitesace the resultin' tokens #a" be converted into values of different t"es usin'
the various ne!t #ethods."ro)r#m Code
Comm#nd line #r)ument
class factorial1
@ublic static void #ain$*trin' ar'sAB&
@inti(sH1Eint nHInte'er.arseInt$ar'sAB&E
for$iH1EiHnEiKK&
@sHsLiE
F*"ste#.out.rintln$Cactorial of a nu#ber is CKs&E
FF
Sc#nner cl#ss
i#ort )ava.util.*cannerE
class factorial
@ublic static void #ain$*trin' ar'sAB&
@int fH1(n(iE
*"ste#.out.rintln$C
8/18/2019 Java Full File
4/26
4Date…………… Practical- 1813344
Output
Comm#nd line #r)ument
Sc#nner cl#sses
Conclusion e have successfull" calculated the factorial of a nu#ber usin' co##and linear'u#ent and usin' *canner class.
8/18/2019 Java Full File
5/26
5Date…………… Practical- 1813344
Aim- Introduction to 5lets.
Objective- To know the basics of 5let and rint te!t on the 5let window.
System Requirements- Java installed s"ste# with Java develo#ent kit $JD%&.
Theory- 5lets are e!tra add-ons rovided b" Java.5n 5let is a s#all alication that
erfor#s one secific task which runs within the scoe of a lar'e ro'ra#. 5let can rovide
web alications with interactive features that canMt be rovided b" T+7.*ince(Java b"te codeis latfor# indeendent(Java 5lets can be e!ecuted b" browsers runnin' under #an"
Platfor#s. hen a Java Technolo'" enabled eb rowser Processes a a'e that contains an
5let(the 5let code is transferred to the client s"ste# and e!ecuted b" the browser Java/irtual +achine$J/+&.
Adv#nt#)es O$ *#v# Applets
1. 5lets are suorted b" #ost eb rowsers.
9. Java 5lets are fast.3. The sa#e alets can work on all installed versions of )ava.
+is#dv#nt#)s o$ Applets
1. It reNuires the )ava lu'ins.
9. *o#er browsers #ostl" #obile browsers do not run )ava alets.3. *o#e alets reNuires a secific )ava runti#e environ#ent.
Al)orithm-
1. I#ort the )ava acka'es.
9. rite the alet code.
3. Definin' the class that inherits the alet class.4. :reate ob)ects if needed b" alets.
"ro)r#m Code-
i#ort )ava.alet.LEi#ort )ava.awt.LE
OLalet codeHCalet1C hei'htH9 widthH9OaletLO
ublic class alet1 e!tends 5let@
ublic void aint$rahics '&
@'.draw*trin'$CelloC(?(1&E
F
F
Output-
8/18/2019 Java Full File
6/26
6Date…………… Practical- 1813344
Conclusion- 5lets have been studied.
8/18/2019 Java Full File
7/26
7Date…………… Practical- 1813344
Aimrite a ro'ra# to find the su# of two nu#bers and disla" the su# as a #essa'e.
Objective-In this ro'ra#( we will learn to use the button and how to erfor# eevent handlin'on buttons.
System Requirements- Java installed s"ste# with Java develo#ent kit $JD%&.
Theory
The Abstr#ct ,indo Toolit $A,T& is JavaMs ori'inal latfor#-deendent
windowin''rahics( and user-interfacewid'et toolkit recedin' *win'. The 5T is art of theJava oundation :lasses$J:& Q the standard 5PI for rovidin' a 'rahical user interface $RI&
for a Java ro'ra#. 5T is also the RI toolkit for a nu#ber of Java +< rofiles.
Al)orithm-
1. irstl" we write source code of our ro'ra# in note ad.9. *ave the ro'ra# with 0.)ava e!tension with sa#e na#e as class na#e.3. en console.
4. o to director" where ro'ra# is saved throu'h 0cd co##and $:han'e Director"
co##and&.;. :o#ile the ro'ra# throu'h 0)avac co##and.
"ro)r#m Code-
i#ort )ava.awt.LEi#ort )ava.alet.LE
i#ort )ava.awt.event.LE
OLalet codeHChelloC widthH; hei'htH3
OaletLO ublic class hello e!tends 5let i#le#ents 5ction7istener
@
Te!tield t1(t9E
7abel l1(l9Eutton bE
*trin' #s'HCCE ublic void init$&
@
l1Hnew 7abel$CGu#ber 1C&El9Hnew 7abel$Cnu#ber 9C&E
8/18/2019 Java Full File
8/26
8/18/2019 Java Full File
9/26
9Date…………… Practical- 1813344
Conclusion- e have learned how to create buttons in )ava and erfor# event handlin' on the#.
8/18/2019 Java Full File
10/26
10Date…………… Practical- 1813344
Aim rite a ro'ra# in )ava to create buttons and erfor# event handlin' on these buttons.
Objective-In this ractical we will learn how to create buttons and erfor# event handlin' onthese buttons.
System Requirements- Java installed s"ste# with Java develo#ent kit $JD%&.
Theory
:han'e in the state of an ob)ect is known as event i2e(event describes the chan'e in the state of source.
"ro)r#m Code-
i#ort )ava.alet.LEi#ort )ava.awt.LE
i#ort )ava.awt.event.LE
OLalet codeHCalet3C hei'htH; widthH3OaletLO
ublic class alet3 e!tends 5let i#le#ents 5ction7istener @
utton b1(b9(b3E
*trin' #s'HCCE ublic void init$&
@
b1Hnew utton$CletC&E b9Hnew utton$CsurriseC&E
b3Hnew utton$Clau'hC&E
add$b1&E
add$b9&E
8/18/2019 Java Full File
11/26
11Date…………… Practical- 1813344
add$b3&E
b1.add5ction7istener$this&E
b9.add5ction7istener$this&E b3.add5ction7istener$this&E
F
ublic void actionPerfor#ed$5ction
8/18/2019 Java Full File
12/26
12Date…………… Practical- 1813344
Conclusion- e have learned how Java ro'ra#s are created( saved( co#iled and run.
8/18/2019 Java Full File
13/26
13Date…………… Practical- 1813344
AimTo insert # record in the d#t#b#se o$ mysql usin) j#v#.
ObjectiveIn this ractical( we will learnto insert records in database of #"sNl in )ava.
System Requirementardware2 Deskto Pc*oftware2 indows Gotead( :o##and Pro#t( Java 1.?( +" *Nl :onnector ;.1.
Theory+"*S7 is an oen source relational database #ana'e#ent s"ste# $6D+*&. In )ul"
913( it was the worlds second #ost widel" used 6D+* and the #ost widel" used clientserver #odel 6D+*. It is na#ed after co-founder #ichael ideniudss dau'hter( +". The *Sl
acron"# stands for *tructured Suer" 7an'ua'e.
"ro)r#m Code
i#ort )ava.util.*cannerE
i#ort )ava.sNl.LE
i#ort )ava!.sNl.LE
ublic class insert@
ublic static void #ain$*trin' ar'AB&
@
*trin' #ecode(userna#e(assword(dburl(Nuer"(#ena#e(#cit"Eint #salE
userna#eHCrootCE asswordHC193CE
dburlHC)dbc2#"sNl2OOlocalhostO#"sNlCE
tr"@
:lass.forGa#e$Cco#.#"sNl.)dbc.DriverC&E
:onnection
conHDriver+ana'er.'et:onnection$dburl(userna#e(assword&E*tate#ent st#tHcon.create*tate#ent$&E
*canner inutHnew *canner$*"ste#.in&E
*canner inut1Hnew *canner$*"ste#.in&E*"ste#.out.rintln$C
8/18/2019 Java Full File
14/26
14Date…………… Practical- 1813344
catch$*S7
@
e.rint*tackTrace$&EF
FF
Output
8/18/2019 Java Full File
15/26
15Date…………… Practical- 1813344
Conclusion In this e!. we have insert a record in the database of #"sNl usin' )ava.
8/18/2019 Java Full File
16/26
16Date…………… Practical- 1813344
Aim,rite # pro)r#m to vie the d#t#b#se o$ mysql usin) j#v# pro)r#m.
Objective2In this ractical( we will learn to view the entries of database of #"sNl in )ava.
System Requirementardware2 Deskto Pc*oftware2 indows Gotead( :o##and Pro#t( Java 1.?( +" *Nl :onnector ;.1.
Theory +"*S7 is an oen source relational database #ana'e#ent s"ste# $6D+*&. In )ul"
913( it was the worlds second #ost widel" used 6D+* and the #ost widel" used clientserver #odel 6D+*. It is na#ed after co-founder #ichael ideniudss dau'hter( +". The *Sl
acron"# stands for *tructured Suer" 7an'ua'e.
Codin)
i#ort )ava.sNl.LE
i#ort )ava!.sNl.LE
ublic class select
@ ublic static void #ain$*trin' ar'AB&
@
*trin' userna#e(assword(dburl(Nuer"(#code(#na#e(#cit"E
int #salEuserna#eHCrootCE
asswordHC193CEdburlHC)dbc2#"sNl2OOlocalhostO#"sNlCE
Nuer"HCselect L fro# e#1CE
tr"@
:lass.forGa#e$Cco#.#"sNl.)dbc.DriverC&E
:onnection con H
Driver+ana'er.'et:onnection$dburl(userna#e(assword&E*tate#ent st#tHcon.create*tate#ent$&E
6esult*et rsHst#t.e!ecuteSuer"$Nuer"&E
while$rs.ne!t$&&@
#codeHrs.'et*trin'$1&E
#na#eHrs.'et*trin'$9&E#salHrs.'etInt$3&E
#cit"Hrs.'et*trin'$4&E
*"ste#.out.rintln$#codeKC(CK#na#eKC(CK#salKC(CK#cit"&E
F
con.close$&E
Fcatch$:lassGotound
@
e.rint*tackTrace$&EF
catch$*S7
@e.rint*tackTrace$&E
8/18/2019 Java Full File
17/26
17Date…………… Practical- 1813344
F
F
F
Output
Conclusion In this e!. we have studied how to view the database of #"sNl usin' )ava ro'ra#.
8/18/2019 Java Full File
18/26
8/18/2019 Java Full File
19/26
19Date…………… Practical- 1813344
i#ort )ava!.servlet.LE
i#ort )ava!.servlet.htt.LE
ublic class elloorld e!tends tt*ervlet@
ublic void doet$tt*ervlet6eNuestreNuest(tt*ervlet6esonse resonse&
throws I..9>Uwebas
Step &. create a folder$sa"
8/18/2019 Java Full File
20/26
20Date…………… Practical- 1813344
Aim ,rite # j#v# pro)r#m to sho ho the client server model ors.
ObjectiveIn this ractical( we will learn the workin' of client server #odel.
System Requirementardware2 Deskto Pc*oftware2 indows Gotead( :o##and Pro#t( Java 1.?( +" *Nl :onnector ;.1.
Theory2
Clientserver model5 co#uter network dia'ra# of clients co##unicatin' with a server via the Internet. The clientV
server #odel of co#utin' is a distributed alication structure that artitions tasks or workloads
between the roviders of a resource or service( called servers( and service reNuesters( calledclients. ften clients and servers co##unicate over a co#uter network on searate hardware(
but both client and server #a" reside in the sa#e s"ste#. 5 server host runs one or #ore server
ro'ra#s which share their resources with clients. 5 client does not share an" of its resources(
but reNuests a serverMs content or service function. :lients therefore initiate co##unicationsessions with servers which await inco#in' reNuests.
"ro)r#m Code
#2 S3R43Ri#ort )ava.io.LE
i#ort )ava.net.LEclass server
@
ublic static void #ain$*trin' ar'AB&throws I
*erver*ocketssHnew *erver*ocket$;;;&E
*ocket sHss.accet$&E
Printriter outHnew Printriter$s.'etutut*trea#$&(true&Euffered6eader br1Hnew uffered6eader$new
Inut*trea#6eader$s.'etInut*trea#$&&&E
uffered6eaderbrHnew uffered6eader$new
Inut*trea#6eader$*"ste#.in&&E
*trin' strE while$true&
@
strHbr1.read7ine$&E
*"ste#.out.rintln$str&Eif$str.eNualsI'nore:ase$Ce!itC&&
@
*"ste#.e!it$&EF
*trin' str1Hbr.read7ine$&E
out.rintln$str1&E
FF
F
8/18/2019 Java Full File
21/26
21Date…………… Practical- 1813344
b2 C5I36T
i#ort )ava.io.LE
i#ort )ava.net.LEclass client
@
ublic static void #ain$*trin' ar'AB&throws I
*ocket sHnew *ocket$C7ocalhostC(;;;&E
uffered6eaderbrHnew uffered6eader$new Inut*trea#6eader$*"ste#.in&&Ewhile$true&
@
*trin' strHbr.read7ine$&E
Printriter outHnew Printriter$s.'etutut*trea#$&(true&Eout.rintln$str&E
if$str.eNualsI'nore:ase$Ce!itC&&
@
*"ste#.e!it$&EF
uffered6eader br1Hnew uffered6eader$new Inut*trea#6eader$s.'etInut*trea#$&&&E*trin' str1Hbr1.read7ine$&E
*"ste#.out.rintln$str1&E
FF
F
Output
SERVER
8/18/2019 Java Full File
22/26
22Date…………… Practical- 1813344
C5I36T
Conclusion In this e!. we have studied a )ava ro'ra# to show how the client server #odel
works.
8/18/2019 Java Full File
23/26
23Date…………… Practical- 1813344
Aim rite a ro'ra# to erfor# )ava strin's.Objective In this ractical( we will learn about strin's in )ava.
System Required ardware reNuired 2 *tandard P:*oftware reNuired 2 Gotead( )dk1.?
Theory*win' is a RIwid'et toolkit for Java. It is art of racleMs Java oundation
:lasses$J:& Q an 5PI for rovidin' a 'rahical user interface $RI& for Java ro'ra#s.
*win' was develoed to rovide a #ore sohisticated set of RI co#onents than the earlier5bstract indow Toolkit $5T&. *win' rovides a native look and feel that e#ulates the look
and feel of several latfor#s( and also suorts a lu''able look and feel that allows alications
to have a look and feel unrelated to the underl"in' latfor#. It has #ore owerful and fle!ibleco#onents than 5T. In addition to fa#iliar co#onents such as buttons( check bo!es and
labels( *win' rovides several advanced co#onents such as tabbed anel( scroll anes( trees(
tables( and lists.
Rnlike 5T co#onents( *win' co#onents are not i#le#ented b" latfor#-secific code.
Instead( the" are written entirel" in Java and therefore are latfor#-indeendent. The ter#Cli'htwei'htC is used to describe such an ele#ent
"ro)r#m Code
i#ort )ava.awt.LE
i#ort )ava!.swin'.LE
ublic class :alculator1@
Jra#e fE
JPanel 1(9(3(4(;E
rivate JTe!tieldtieldE rivate J+enu( nu#?( nu#8(nu#,(nu#E
rivate Juttonb5dd(b*ub(b+ul(bDiv(eNualsE
:alculator1$&@
f H new Jra#e$C:alculatorC&E
+enuar H new J+enuar$&E
8/18/2019 Java Full File
24/26
24Date…………… Practical- 1813344
1 H new JPanel$&E
9 H new JPanel$&E
3 H new JPanel$&E 4 H new JPanel$&E
; H new JPanel$&E
tield H new JTe!tield$3;&E
nu# H new Jutton$CC&E
nu#1 H new Jutton$C1C&Enu#9 H new Jutton$C9C&E
nu#3 H new Jutton$C3C&E
nu#4 H new Jutton$C4C&E
nu#; H new Jutton$C;C&Enu#> H new Jutton$C>C&E
nu#? H new Jutton$C?C&E
nu#8 H new Jutton$C8C&E
nu#, H new Jutton$C,C&Enu#Hnew Jutton$CC&E
b5dd H new Jutton$CKC&E
b*ub H new Jutton$C-C&E
b+ul H new Jutton$C!C&E bDiv H new Jutton$COC&E
eNuals H new Jutton$CHC&E
F
ublic void launchra#e$&
@
tield.setTe!t$C.C&Etield.set
8/18/2019 Java Full File
25/26
25Date…………… Practical- 1813344
;.add$nu#&E
;.add$b5dd&E ;.add$eNuals&E
9.set7a"out$new rid7a"out $1( 3( 9( 9& &E 3.set7a"out$new rid7a"out $1( 3( 9( 9& &E
4.set7a"out$new rid7a"out $1( 3( 9( 9& &E
;.set7a"out$new rid7a"out $1( 3( 9( 9& &E
f.set7a"out$new rid7a"out $>( 1& &E
f.set6esiWable$false&E
f.set*iWe$3(9;&Ef.add$tield&E
f.add$1&E
f.add$9&E
f.add$3&Ef.add$4&E
f.add$;&Ef.set/isible$true&E
f.setJ+enuar$+enuar&E
f.ack$&EF
ublic static void #ain $*trin' ar'sAB&@
:alculator1 s H new :alculator1$&Es.launchra#e$&E
F
F
Output
8/18/2019 Java Full File
26/26
26Date…………… Practical- 1813344
Conclusion e have studied about *trin's in Java and erfor#ed their use.