Sie sind auf Seite 1von 75

Rajalakshmi Institute of Technology

Department of Information Technology Subject Code: IT 2305

JAVA Programming LAB Manual

Name Reg No

: : : :

Branch

Year & Semester

JAVA LAB

EX NO:1.1 E!!I"IENT RE#RE$ENT%TION !OR R%TION%& N'()ER D%TE: %I( To write a JAVA program for finding simplified form of the given rational n m!er" %&*ORIT+( #" $mport %ava" til and %ava"io pac&age" '" (efine Rational class with re) ired varia!les " *" +et the n merator and denominator val es" ," -sing the o!%ect of the Rational class call the method efficientrep./" 0" $f the n merator is greater than the denominator then i/" Ret rn the inp t as it is" 1lse i/" 2ind the Rational val e" ii/" Ret rn .n merator34543denominator/" ,. (ispla6 the Simplified form of rational n m!er on screen li&e n merator5denominator" RE-IE. /'E$TION$ 1. .hat is )yteco0e1 1ach %ava program is converted into one or more class files" The content of the class file is a set of instr ctions called !6tecode to !e e7ec ted !6 Java Virt al 8achine .JV8/"Java introd ces !6tecode to create platform independent program" 2. .hat is 3-(1
JV8 is an interpreter for !6tecode which accepts %ava !6tecode and prod ces res lt"

4. +o5 many 3-(s can run on a single machine1 No limitation" A JV8 is % st li&e an6 other process" 6. .hat is the use of $tringTokeni7er class1 The class allows an application to !rea& a string into to&ens" 8. Differentiate ja9a from "::. Java doesn9t s pport pointers" Java doesn9t provide operator overloading option for programmers tho gh it internall6 se it for string concatenation" Java doesn9t s pport m ltiple class inheritance" :owever; it s pports m ltiple inheritance sing interface" Java doesn9t glo!al varia!les; glo!al f nctions; t6pedef; str ct res or nions" Java ses final &e6word to avoid a class or method to !e overridden"

Page 1 of

38

JAVA LAB ,. .hat are the three 0ifferent types of comments in ja9a1 Java comments ma&e 6o r code eas6 to nderstand; modif6 and se" Java s pports three different t6pes of comment st6les" #" 1ver6 thing !etween initial slash <asteris& and ending asteris&=slash is ignored !6 the %ava compiler".5>>5/ '" (o !le slash .55/mar& is ignored !6 %ava compiler"
*" 1ver6 thing !etween initial slash < asteris& = asteris& and ending asteris&=slash is ignored !6 the %ava compiler and another program called JAVA(?@"1A1 that ships with the J(B ses these comments to constr ct :T8C doc mentation files that descri!e 6o r pac&ages; classes and methods as well as all the varia!les sed" .5>>>5/

#RO*R%(

Page 2 of

38

JAVA LAB

RE$'&T Th s the %ava program for finding simplified form of the given rational n m!er was compiled and e7ec ted s ccessf ll6"

Page 3 of

38

JAVA LAB

EX NO:1.2 D%TE: %I(

!I)ON%""I $ERIE$

To write a JAVA program for finding first N 2i!onacci n m!ers" %&*ORIT+( #" import %ava"io pac&age" '" Cength of 2i!onacci series DN9 m st !e got as a &e6!oard inp t !6 $np tStreamReader class" B fferedReader !rEnew B fferedReader.new $np tStreamReader.S6stem"in// 0" 2?R iE# to N do the following 2$BE 2# 3 2' 2# E 2' 2' E2$B FR$NT 2$B 1N( 2?R G" Stop the Frogram" RE-IE. /'E$TION$ 1. .hat is class1 A class is a !l eprint or protot6pe from which o!%ects are created" He can create an6 n m!er of o!%ects for the same class" 2. .hat is o;ject1 ?!%ect is an instance of class" ?!%ect is real world entit6 which has state; identit6 and !ehavior" 4. .hat are 0ifferent types of access mo0ifiers1 pu;lic: An6thing declared as p !lic can !e accessed from an6where" pri9ate: An6thing declared as private can9t !e seen o tside of its class" protecte0: An6thing declared as protected can !e accessed !6 classes in the same pac&age and s !classes in the other pac&ages" 0efault mo0ifier : @an !e accessed onl6 to classes in the same pac&age" 6. .hat is encapsulation1 1ncaps lation is the mechanism that !inds together code and the data it manip lates; and &eeps !oth safe from o tside interference and mis se" 8. .hat is 0ata hi0ing1 $mplementation details of methods are hidden from the ser"

sing

*" @onvert the inp t into an integer sing parse$nt. / method which is availa!le in $nteger class" ," (eclare two integer data t6pe varia!les 2# & 2' and assign val es as 2#E=# & 2'E#"

Page 4 of

38

JAVA LAB

,. Define DataInput$tream < DataOutput$tream. (ata$np tStream is sed to read %ava primitive datat6pes directl6 from the stream"
(ata? tp tStream is sed to write the %ava primitive datat6pes directl6 to the stream"

#RO*R%(

RE$'&T Th s the %ava program for finding first N 2i!onacci n m!ers was compiled and e7ec ted s ccessf ll6"

Page 5 of

38

JAVA LAB EX NO:1.4 D%TE: %I( To write a JAVA program that reads an arra6 of N integers and print whether each one is prime or not" %&*ORIT+( $n main method do the following #" Read N integers from &e6!oard and store it in an arra6 a" :ere N is the n m!er of entries in an arra6" '" 2?R iE# to N do the following $2 $sFrime.aIiJ/ is TR-1 then Frint aIiJ is Frime 1CS1 Frint aIiJ is not Frime 1N( $2 1N( 2?R
8ethod $sFrime is ret rns Boolean val e" $f it is Frime then ret rn tr e otherwise ret rn false"

#RI(E N'()ER "+E"=IN*

1. 2?R iE' to N5' $2 N is divisi!le !6 i ret rn 2ACS1 1CS1 @?NT$N-1 2?R loop 1N( $2
1N( 2?R ret rn TR-1

RE-IE. /'E$TION$ 1. .hat is casting1 @asting is sed to convert the val e of one data t6pe to another" 2. .hat is a package1 A pac&age is a collection of classes and interfaces that provides a high=level la6er of access protection and name space management" 4. .hat is the use of >classpath? en9ironment 9aria;le1
@lasspath is the list of directories thro gh which the JV8 will search to find a class"

6. .hy 0oes the main metho0 nee0 a static i0entifier1 Beca se static methods and mem!ers don9t need an instance of their class to invo&e them and main is the first method which is invo&ed"

Page 6 of

38

JAVA LAB

8. .hat is the 0ifference ;et5een constructor an0 metho01 @onstr ctor will !e a tomaticall6 invo&ed when an o!%ect is created whereas method has to !e called e7plicitl6" #RO*R%(

RE$'&T
Th s the %ava program for chec&ing prime n m!er was compiled and e7ec ted s ccessf ll6"

Page

of

38

JAVA LAB EX NO:2 D%TE: %I( To write a JAVA program for finding whether the given 6ear is a leap 6ear or not" %&*ORIT+( #" '" *" ," $mport %ava" til pac&age" $nside the class (ate;define the methods isvalid./;isleap6ear./;compareto./" The method isvalid./ is sed to chec& the form of the date;month;6ear " The method isleap6ear./ is a Boolean methods it ret rn tr e when the given 6ear is a leap 6ear otherwise it ret rn false" 0" The method compareto./ compares the val e of the o!%ect with that of date"ret rn K if the val es are e) al"Ret rns a negative val e if the invo&ing o!%ect is earlier than date"Ret rns a positive val e if the invo&ing o!%ect is later than date" G" $nside the main./ method we perform the leap 6ear calc lation" L" Stop the program" RE-IE. /'E$TION$ A. .hat is class1 A class is a !l eprint or protot6pe from which o!%ects are created" He can create an6 n m!er of o!%ects for the same class" B. .hat is o;ject1 ?!%ect is an instance of class" ?!%ect is real world entit6 which has state; identit6 and !ehavior" C. .hat are 0ifferent types of access mo0ifiers1 pu;lic: An6thing declared as p !lic can !e accessed from an6where" pri9ate: An6thing declared as private can9t !e seen o tside of its class" protecte0: An6thing declared as protected can !e accessed !6 classes in the same pac&age and s !classes in the other pac&ages" 0efault mo0ifier : @an !e accessed onl6 to classes in the same pac&age" 1D. .hat is encapsulation1 1ncaps lation is the mechanism that !inds together code and the data it manip lates; and &eeps !oth safe from o tside interference and mis se" 11. .hat is 0ata hi0ing1 $mplementation details of methods are hidden from the ser" &E%# @E%R "%&"'&%TION

Page 8 of

38

JAVA LAB

12. Define DataInput$tream < DataOutput$tream. (ata$np tStream is sed to read %ava primitive datat6pes directl6 from the stream"
(ata? tp tStream is sed to write the %ava primitive datat6pes directl6 to the stream"

#RO*R%(

Page ! of

38

JAVA LAB

RE$'&T Th s the %ava program for finding whether the given 6ear is leap or not was compiled and e7ec ted s ccessf ll6"

Page 10 of

38

JAVA LAB EX NO:4 D%TE: %I( &I$# I(#&E(ENT%TION

To write a JAVA program to implement !asic operations of C$SF" %&*ORIT+( #" Cin&edCist class can !e accessed !6 importing %ava" til pac&age" '" @reate a class C$SFCist with the !asic operations of C$SF Cist s ch as Dcons9; Dcar9; Dcdr9 " *" (efine the C$SFCist class with an arra6 of string and three methods" Void cons. / String car. / String cdr. / ," cons./ method sed to append item to the list at first location" Cist"add2irst.String item/ 0" car./ method is sed to view the first item in the list" FR$NT Cist"get2irst./ G" cdr./ method is sed to view the part of the list that follows the first item" RE-IE. /'E$TION$ 1. +o5 thisE metho0 use0 5ith constructors1
this./ method within a constr ctor is sed to invo&e another constr ctor in the same

class" 2. +o5 superE metho0 use0 5ith constructors1 s per./ method within a constr ctor is sed to invo&e its immediate s perclass constr ctor" 4. .hat is reflection in ja9a1 Reflection is the a!ilit6 of a program to anal6Me itself" The %ava"lang"reflect pac&age provides the a!ilit6 to o!tain information a!o t the fields; constr ctors; methods; and modifiers of a class" " 6. .hat is EFceptions in ja9a1 An e7ception is an a!normal condition that arises in a code se) ence at r n time" $n other words; an e7ception is a r n=time error" All e7ception t6pes are s !classes of the ! ilt=in class Throwa!le"

Page 11 of

38

JAVA LAB

8. .hat is Error1
This class descri!es internal errors; s ch as o t of dis& space etc"""The ser can onl6 !e informed a!o t s ch errors and so o!%ects of these cannot !e thrown 17ceptions"

,. .hat is the &ist interface1


The &ist interface e7tends "ollection and declares the !ehavior of a collection that stores a

se) ence of elements" 1lements can !e inserted or accessed !6 their position in the list; sing a Mero=!ased inde7" A list ma6 contain d plicate elements" A. .hat is the &inke0&ist class1 The Cin&edCist class e7tends A!stractSe) entialCist and implements the Cist interface" $t provides a lin&ed=list data str ct re" Cin&edCist class defines some sef l methods of its own for manip lating and accessing lists"
void add2irst.?!%ect o!%/ void addCast.?!%ect o!%/ ?!%ect get2irst. / ?!%ect getCast. / ?!%ect remove2irst. / ?!%ect removeCast. /

B. .hat is the purpose of gar;age collection in 3a9aG an0 5hen is it use01 The p rpose of gar!age collection is to identif6 and discard o!%ects that are no longer needed !6 a program so that their reso rces can !e reclaimed and re sed" A Java o!%ect is s !%ect to gar!age collection when it !ecomes nreacha!le to the program in which it is sed" #RO*R%(

Page 12 of

38

CS58 " JAVA LAB

RE$'&T Th s the %ava program for implementing the !asic operations of C$SF was compiled and e7ec ted s ccessf ll6"

Page 13 of

38

JAVA LAB EX NO:6 D%TE: %I( To write a JAVA program to design a Vehicle class hierarch6 and a test program to demonstrate pol6morphism concept" %&*ORIT+(
#" (efine a s perclass vehicle with three private integer data mem!ers . speed;color; wheel/"

#O&@(OR#+I$( H (ET+OD O-ER&O%DIN*

'" *" ," 0"

(efine a f nction disp./ to displa6 the details a!o t vehicle" (efine a constr ctor to initialiMe the data mem!er cadence; speed and gear" (efine a s !class ! s which e7tends s perclass vehicle" ?verridden disp./ method in vehicle class" Additional data a!o t the s spension is incl ded to the o tp t" G" (efine a s !class train which e7tends s perclass vehicle" L" There are three classes: ! s; train; and car The three s !classes override the disp method and print ni) e information" N" (efine a sample class program that creates three vehicle varia!les" 1ach varia!le is assigned to one of the three vehicle classes" 1ach varia!le is then printed" RE-IE. /'E$TION$ 1. .hat is polymorphism1 Fol6morphism means the a!ilit6 to ta&e more than one form" S !classes of a class can define their own ni) e !ehaviors and 6et share some of the same f nctionalit6 of the parent class" 2. .hat is o9erloa0ing1
Same method name can !e sed with different t6pe and n m!er of arg ments .in same

class/ 4. .hat is o9erri0ing1 Same method name; similar arg ments and same n m!er of arg ments can !e defined in s per class and s ! class" S ! class methods override the s per class methods" 6. .hat is the 0ifference ;et5een o9erloa0ing an0 o9erri0ing1 ?verloading related to same class and overriding related s !=s per class @ompile time pol6morphism achieved thro gh overloading; r n time pol6morphism achieved thro gh overriding"

Page 14 of

38

CS58 " JAVA LAB

8. .hat is hierarchy1 ?rdering of classes .or inheritance/ #RO*R%(

Page 15 of 38

CS58 " JAVA LAB

RE$'&T Th s the %ava program for design a Vehicle class hierarch6 was compiled and e7ec ted s ccessf ll6"

Page 16 of

38

JAVA LAB EX NO:8 $T%"= I(#&E(ENT%TION '$IN* %RR%@ %ND &IN=ED &I$T D%TE: %I( To write a JAVA program to implement stac& sing arra6 and lin&ed list" %&*ORIT+( $nterface: Stac&A(T #" @reate a Stac&A(T interface with !asic stac& operations" Void F-S:.int/ & int F?F. / @lass: Stac&Arra6 #" @reate a class Stac&Arra6 which implements Stac&Adt sing Arra6" '" (efine the Stac&Arra6 class with two integer varia!les.top & siMe/ and one integer arra6" *" $nitialiMe the varia!le top E=#" $t indicates that the stac& is empt6" ," F-S: method read the element and p sh it into stac&"
Void F-S:.data t6pe element/ $2 topEEsiMe T:1N

Frint Stac& 2 ll 1CS1


top33 stac&ItopJEelement

1N( $2 0" F?F method ret rn the data popped" (ata t6pe F?F./ $2 topEE=# Frint Stac& is 1mpt6 1CS1 ret rn stac&Itop==J 1N( $2 @lass: Stac&Cin&edlist #" Cin&edCist class can !e accessed !6 importing %ava" til pac&age" '" @reate a class Stac&Cin&edlist which implements Stac&A(T interface" *" add2irst./ method in Cin&edCist class sed to p sh element in the stac&" ," remove2irst./ method in Cin&edCist class sed for deleting an element from stac&" RE-IE. /'E$TION$ 1. .hat is Inheritance1 $nheritance is the process !6 which one o!%ect ac) ires the properties of another o!%ect" This is important !eca se it s pports the concept of hierarchical classification" 2. .hy inheritance is important1 Re sa!ilit6 is achieved thro gh inheritance" 4. .hich types of inheritances are a9aila;le in 3a9a1 Simple; :ierarchical and 8 ltilevel

Page 1 of

38

CS58 " JAVA LAB

6. "an 5e achie9e multiple inheritances in 3a9a1 Hith the help of interfaces we can achieve m ltiple inheritances" 8. .hat is interface1
$nterface is a collection of methods declaration and constants that one or more classes of

o!%ect will #RO*R%(

se" All methods in an interface are implicitl6 a!stract" All varia!les are final and static" 8ethods sho ld not !e declared as static; final; private and protected"

Page 18 of 38

CS58 " JAVA LAB

RE$'&T Th s the %ava program for implementing stac& sing arra6 and lin&ed list was compiled and e7ec ted s ccessf ll6"

Page 1! of

38

JAVA LAB

EX NO:, D%TE: %I(

"'RREN"@ "ON-ER$ION

To write a JAVA program that randoml6 generates one n m!er which is stored in a file then read the dollar val e from the file and converts to r pee" %&*ORIT+( #" Random n m!ers in %ava can !e generated either !6 sing the Random class in %ava" til pac&age or !6 sing the random./ method in the 8ath class in %ava" '" -sing the random./ method in 8ath class do !le randomVal e E 8ath"random./O -sing the Random class import %ava" til"RandomO Random random E new Random./O $nt randomVal eErandom"ne7t$nt./O *" Hrite the generated random n m!er in a file sing 2ileHriter stream" B fferedHriter o t E new B fferedHriter.new 2ileHriter.4test"t7t4//O o t"write.randomVal e/O o t"close./O ," Read the data from the file sing 2ileReader stream" B fferedReader in E new B fferedReader.new 2ileReader.4test"t7t4//O String line E in"readCine./O $nt dollar E $nteger"parse$nt.line/O 0" @onvert the dollar val e into r pee !6 e) ation and displa6 it on screen" RE-IE. /'E$TION$ 1. Define $tream
Stream prod ces and cons mes information" $t is the comm nication path !etween the so rce and the destination "$t is the ordered se) ence of data shared !6 $? devices"

2. &ist out the types of stream. B6te Stream @haracter Stream 4. Differentiate ;et5een ;yte stream < character stream1 )yte stream "haracter stream

Page 20 of

38

JAVA LAB A !6te stream class provides s pport for handling $5? operations on !6tes" B6te stream classes are $np tStream and ? tp tStream" $t provides s pport for managing $5? operations on characters" Reader and Hriter classes are character stream classes"

6. .hat is the use of output streams1 $t is sed to perform inp t and o tp t operations on o!%ects sing the o!%ect streams" $t is sed to declare records as o!%ects and se the o!%ects classes to write and read these o!%ects from files" 8. .hat is Ran0om class1 The Random class is a generator of pse dorandom n m!ers" Random. / creates a n m!er generator that ses the c rrent time as the starting; or seed; val e" $ntegers can !e e7tracted via the ne7t$nt. / method" #RO*R%(

Page 21 of

38

JAVA LAB

RE$'&T
Th s the %ava program for c rrenc6 conversion was compiled and e7ec ted s ccessf ll6"

Page 22 of

38

JAVA LAB EX NO:A D%TE: %I( To write a JAVA program to print all n m!ers !elow #KK;KKK that are !oth prime and 2i!onacci n m!er" %&*ORIT+( 2or creating a thread a class has to e7tend the Thread @lass" 2or creating a thread !6 this proced re 6o have to follow these steps: #" 17tend the ja9a.lang.Threa0 @lass" '" ?verride the runE method in the s !class from the Thread class to define the code e7ec ted !6 the thread" *" @reate an instance of this s !class" This s !class ma6 call a Thread class constr ctor !6 s !class constr ctor" ," $nvo&e the startE method on the instance of the class to ma&e the thread eligi!le for r nning" The proced re for creating threads !6 implementing the R nna!le $nterface is as follows: #" A @lass implements the Runna;le $nterface; override the r n./ method to define the code e7ec ted !6 thread" An o!%ect of this class is R nna!le ?!%ect" '" @reate an o!%ect of Threa0 @lass !6 passing a R nna!le o!%ect as arg ment" *" $nvo&e the startE method on the instance of the Thread class" Ci&e creation of a single thread; create more than one thread .m ltithreads/ in a program sing class Threa0 or implementing interface Runna;le" RE-IE. /'E$TION$ 1. .hat is threa01 Thread is similar to a program that has single flow of control" 1ach thread has separate path for e7ec tion" 2. .hat is multithrea0ing1 Sim ltaneo s e7ec tion of threads is called m ltithreading" 4. .hat are the a09antages of (ultithrea0ing o9er multitasking1 Red ces the comp tation time $mproves performance of an application Threads share the same address space so it saves the memor6" @ost of comm nication !etween is relativel6 low" 6. +o5 to create threa01 B6 creating instance of Thread class or implementing R nna!le interface" ('T&IT+RE%DIN*

Page 23 of

38

JAVA LAB

8. &ist out metho0s of Threa0 class1 c rrentThread. /; setName. /; getName. /; setFriorit6. /; getFriorit6. /; %oin. /; isAlive. / ,. .hat is joinE metho0 of Threa01 @om!ines two or more threads r nning process and wait till their e7ec tion is completed" A. .hat are the states in Threa0 &ife "ycle1 Read6; r nning; !loc&; wait; dead" #RO*R%(

Page 24 of

38

JAVA LAB

RE$'&T
Th s the %ava program for creating m ltithread was compiled and e7ec ted s ccessf ll6"

Page 25 of

38

JAVA LAB EX NO:B D%TE: %I( To design a scientific calc lator sing event driven programming paradigm of JAVA" %&*ORIT+( #" $mport applet; awt and awt"event pac&ages" '" @reate a class calc lator which e7tends Applet and implements ActionCistener" *" @reate o!%ect for Te7t2iled; Cist; Ca!el and B tton classes" Add all these o!%ects to the window sing add.o!%/ method"
," -se addActionerCistener.o!%/ method to receive action event notification from component" Hhen the ! tton is pressed the generated event is passed to ever6 1ventCistener o!%ects that receives s ch t6pes of events sing the addActionCistener./ method of the o!%ect"

$"IENTI!I" "%&"'&%TOR

0" $mplement the ActionCistener interface to process ! tton events sing actionFerformed.Action1vent o!%/ method" G" Align the ! ttons in container !6 sing +rid Ca6o t"The format of grid la6o t constr ctor is p !lic +ridCa6o t.int n mRows; int N m@ol mns; int horM; int vert/ L" Set the la6o t for the window sing setCa6o t.grido!%.row;col mn// method" RE-IE. /'E$TION$ 1. .hat is an applet1 Applet is a d6namic and interactive program that r ns inside a we! page displa6ed !6 a %ava capa!le !rowser" 2. .hat is the 0ifference ;et5een applications an0 applets1 Application m st !e r n on local machine whereas applet needs no e7plicit installation on local machine" 4. .hat is %;stract .in0o5ing Toolkit E%.T 1 A li!rar6 of %ava pac&ages that forms part of %ava AF$; sed to for +-$ design"

6. .hat is a layout manager an0 5hat are 0ifferent types of layout managers a9aila;le in ja9a %.T1 A la6o t manager is an o!%ect that is sed to organiMe components in a container" The different la6o ts are availa!le are 2lowCa6o t BorderCa6o t @ardCa6o t +ridCa6o t and +ridBagCa6o t"

Page 26 of

38

JAVA LAB

8. .hich package is reIuire0 to 5rite *'I E*raphical 'ser Interface programs1 Java"awt ,. .hat is e9ent listener1 A listener is an o!%ect that is notified when an event occ rs" The methods that receive and process events are defined in a set of interfaces fo nd in %ava"awt"event" 2or e7ample; the 8o se8otionCistener interface defines two methods to receive notifications when the mo se is dragged or moved" #RO*R%(

Page 2 of

38

JAVA LAB

RE$'&T Th s the %ava program for design a scientific calc lator was compiled and e7ec ted s ccessf ll6"

Page 28 of

38

JAVA LAB EX NO:C D%TE: %I( To develop a simple Ci!rar6 8anagement s6stem for li!rar6 sing event driven programming paradigm of JAVA and it is connected with !ac&end data!ase sing J(B@ connection" %&*ORIT+( #" @reate a data!ase ta!le Boo&s with following fields !iel0 Name Data Type Title String A thor String 1dition $nt $SBN String '" (efine a class Ci!rar6 that m st have the following methods for accessing the information a!o t the !oo&s availa!le in li!rar6" a" addBoo&. / !" deleteBoo&. / c" viewBoo&s. / *" (esign the ser interface screen with necessar6 te7t !o7es and ! ttons" This page is formatted sing grid la6o t" 8ethod: addBoo&. / This method ta&es the !oo& title; a thor name;edition and $SBN as inp t parameters and store them in Boo&s ta!le"
#" 1sta!lish connection with data!ase @lass"forName.Ps n"%d!c"od!c"Jd!c?d!c(riverQ/O

&I)R%R@ (anagement $ystem

@onnection conE(river8anager"get@onnection.P%d!c:od!c:(SNQ;QQ;QQ/O '" Add the details in Boo&s ta!le" FreparedStatement psEcon"prepareStatement.Pinsert into Boo&s val es.R;R;R;R/Q/O Fs"setString.#;title/O Fs"setString.';a thor/O Fs"set$nt.*;edition/O Fs"setString.,;$SBN/O
*" 17ec te the statement" ps"e7ec te-pdate./O

," @lose the connection" 8ethod :deleteBoo&./ #" 1sta!lish connection with data!ase '" (elete the details from Boo&s ta!le" FreparedStatement psEcon"prepareStatement.Pdelete from Boo&s where a thorERQ/O Fs"setString.#;a thor/O
*" 17ec te the statement" ps"e7ec teS er6./O

," @lose the connection"

Page 2! of 38

JAVA LAB 8ethod :viewBoo&./ #" 1sta!lish connection with data!ase '" read the details in Boo&s ta!le" FreparedStatement psEcon"prepareStatement.Pselect > from Boo&s where titleERQ/O Fs"setString.#;title/O
*" 17ec te the statement" ps"e7ec teS er6./O

," @lose the connection" RE-IE. /'E$TION$ 1. .hat 0o you mean ;y 3D)"1 J(B@ is a part of the Java (evelopment Bit which defines an application=programming interface that ena!les %ava programs to e7ec te SSC statements and retrieve res lts from data!ase" 2. &ist 0o5n the 5ays OD)" 0iffer from 3D)"1 ?(B@ is for 8icrosoft and J(B@ is for %ava applications" ?(B@ canTt !e directl6 sed with Java !eca se it ses a @ interface" ?(B@ re) ires man al installation of the ?(B@ driver manager and driver on all client machines" 4. .hat are 0ri9ers a9aila;le1 J(B@=?(B@ Bridge driver Native AF$ Fartl6=Java driver J(B@=Net F re Java driver Native=Frotocol F re Java driver 6. .hat are the steps in9ol9e0 for making a connection 5ith a 0ata;ase or ho5 0o you connect to a 0ata;ase1 #/ Loading the driver: @lass" forName.Qs n" %d!c" od!c" Jd!c?d!c(riverQ/O Hhen the driver is loaded; it registers itself with the %ava" s)l" (river8anager class as an availa!le data!ase driver" '/ Making a connection with database: @onnection con E (river8anager" get@onnection .Q%d!c:od!c:somed!Q; P serQ; PpasswordQ/O */ Executing SQL statements : Statement stmt E con" createStatement./O Res ltSet rs E stmt" e7ec teS er6.QS1C1@T > 2R?8 some ta!leQ/O ,/ Process the results : Res ltSet ret rns one row at a time" Ne7t./ method of Res ltSet o!%ect can !e called to move to the ne7t row" The getString./ and get?!%ect./ methods are sed for retrieving col mn val es"

Page 30 of

38

CS58 " JAVA LAB

8. Define OD)". ?(B@ is a standard for accessing different data!ase s6stems" There are interfaces for Vis al Basic; Vis al @33; SSC and the ?(B@ driver pac& contains drivers for the Access; Farado7; dBase; Te7t; 17cel and Btrieve data!ases #RO*R%(

Page 31 of

38

CS58 " JAVA LAB

RE$'&T Th s the %ava program for develop a simple Ci!rar6 8anagement s6stem for li!rar6 was compiled and e7ec ted s ccessf ll6"

Page 32 of

38

JAVA LAB EX NO:1D D%TE: %I( To implement a simple chat application sing JAVA" %&*ORIT+( $er9er #" $mport io and net pac&ages" '" -sing $np tStreamReader.S6stem"in/ class read the contents from &e6!oard and pass it to B fferedReader class" *" @reate a server soc&et sing (atagramSoc&et.port no/ class" ," -se (atagramFac&et./ class to create a pac&et" 0" To send and receive the datagram pac&ets; se send.pac&eto!%/ and receive.pac&eto!%/" "lient #" $mport io and net pac&ages" '" -sing $np tStreamReader.S6stem"in/ class read the contents from &e6!oard and pass it to B fferedReader class" *" @reate a client soc&et sing (atagramSoc&et.port no/ class" ," -se (atagramFac&et./ class to create a pac&et" 0" To send and receive the datagram pac&ets; se send.pac&eto!%/ and receive.pac&eto!%/" RE-IE. /'E$TION$ 1. Define socket.
The soc&et is a software a!straction sed to represent the terminals of a connection !etween

"+%TTIN*

two machines or processes" .?r/ it is an o!%ect thro gh which application sends or receives pac&ets of data across networ&" 2. .hat are the ;asic operations of client sockets1 #"@onnect to a remote machine '"Send data
*"Receive data ,"@lose a connection

4. .hat are the ;asic operations of $er9er socket1 Bind to a port Cisten for incoming data Accept connections from remote machines on the !o nd port 6. .hat is meant ;y 0atagram1 The data9s are transmitted in the form of pac&ets that is called as datagram" 2inite siMe data pac&ets are called as datagram"

Page 33 of

38

JAVA LAB

8. &ist all the socket classes in ja9a. Soc&et ServerSoc&et (atagram Soc&et 8 lticast Soc&et Sec re soc&ets #RO*R%(

Page 34 of

38

CS58 " JAVA LAB

RE$'&T Th s the %ava program for developing chat application was compiled and e7ec ted s ccessf ll6"

Page 35 of

38

JAVA LAB EX NO:11 D%TE: %I( To develop a -ser defined (ate class in %ava" %&*ORIT+(
#" $mport s n" til"calendar"@alendar(ate;s n" til"calendar;%ava"te7t"(ate2ormat and s n" til"calendar"Base@alendar pac&ages" '" @reate an instance to the class Base@alendar in til pac&age"
*" +et the (ate and time sing get+regorian@alendar./ method" Base@alendar gcal E @alendarS6stem"get+regorian@alendar./O

D%TE "&%$$ I(#&E(ENT%TION

," c rrentTime8illis./ method is sed to ret rn the c rrent time in milliseconds" 0" +et 6ear sing getYear./ method in @alenderS6stem class and displa6 it on screen" RE-IE. /'E$TION$ 1. &ist some functions of Date class. (ate class has two important f nctions" long get ime! " Ret rns the n m!er of milliseconds that have elapsed since Jan ar6 #; #ULK" void set ime!long time" Sets the time and date as specified !6 time; which represents an elapsed time in milliseconds from midnight; Jan ar6 #; #ULK" 2. .hat is the "ollections %#I1 The @ollections AF$ is a set of classes and interfaces that s pport operations on collections of o!%ects" 17ample of classes: :ashSet; :ash8ap; Arra6Cist; Cin&edCist; TreeSet and Tree8ap" 17ample of interfaces: @ollection; Set; Cist and 8ap" 4. .hat is the *regorian"alen0ar class1 +regorian@alendar is a concrete implementation of a @alendar that implements the normal +regorian calendar with which 6o are familiar" The get$nstance. / method of @alendar ret rns a +regorian@alendar initialiMed with the c rrent date and time in the defa lt locale and time Mone" +regorian@alendar defines two fields: A( and B@" These represent the two eras defined !6 the +regorian calendar" 6. .hat is the $impleTimeJone class1
The TimeVone class allows 6o to wor& with time Mone offsets from +reenwich mean time .+8T/; also referred to as @oordinated -niversal Time .-T@/"

8. .hat is the $impleTimeJone class1 The SimpleTimeVone class is a convenient s !class of TimeVone" $t implements TimeVone9s a!stract methods and allows 6o to wor& with time Mones for a +regorian calendar"

Page 36 of

38

CS58 " JAVA LAB ,. .hich package is reIuire0 to use *regorian "alen0ar programs1 Java" til"calendar"+regorian #RO*R%(

RE$'&T Th s the %ava program for implementing (ate class was compiled and e7ec ted s ccessf ll6"

Page 37 of

38

Das könnte Ihnen auch gefallen