Java and J2EE (06IS753)
Java and J2EE Scheme and Syllabus Sub ject Code: 06IS753
I.A. Mark s: 25
Hours / Week : 04
Exam Hours: 03
Total Hours: 52
Exam Mark s: 100 PAR T - A
UNIT - 1 INTR ODUCTION TO JAVA: Java and Java a p plications; Java Develo pment K it (JDK ); Java is inter pr eted, Byte Code, JVM; O b ject-or iented pr ogr amming; Sim ple Java pr ogr ams. Data ty pes and other tok ens: Boolean var ia bles, int, long, char , o per ator s, ar r ays, white s paces, liter als, assigning values; Cr eating and destr oying o b jects; Access s pecif ier s. O per ator s and Ex pr essions: Ar ithmetic O per ator s, Bitwise o per ator s, R elational o per ator s, The Assignment O per ator , The? O per ator ; O per ator Pr ecedence; Logical ex pr ession; Ty pe casting; Str ings Contr ol Statements: Selection statements, iteration statements, Jum p Statements. 6 Hours UNIT - 2 CLASSES, INHER ITANCE, EXCEPTIONS, APPLETS: Classes: Classes in Java; Declar ing a class; Class name; Su per classes; Constr uctor s; Cr eating instances of class; Inner classes. Inher itance: Sim ple, multi ple, and multilevel inher itance; Over r iding, over loading. Exce ption handling: Exce ption handling in Java. The A p plet Class: Two ty pes of A p plets; A p plet basics; A p plet Ar chitectur e; An A p plet sk eleton; Sim ple A p plet dis play methods; R equesting r e painting; Using the Status Window; The HTML APPLET tag; Passing par ameter s to A p plets; getDocument base() and getCode base(); A pletContext and showDocument(); The AudioCli p Inter f ace; The A p pletStu b Inter f ace; Out put to the Console. 6 Hours UNIT - 3 MULTI THR EADED PR OGR AMMING, EVENT HANDLING: Multi Thr eaded Pr ogr amming: What ar e thr eads? How to mak e the classes thr eada ble; Extending thr eads; Im plementing r unna ble; Synchr onization; Changing state of the thr ead; Bounded buf f er pr o blems, r ead-wr ite pr o blem, pr oducer -consumer pr o blems. Event Handling: Two event handling mechanisms; The delegation event model; Event classes; Sour ces of events; Event listener inter f aces; Using the delegation event model; Ada pter classes; Inner classes. 7 Hours
UNIT - 4 SWINGS: Swings: The or igins of Swing; Two k ey Swing f eatur es; Com ponents and Container s; The Swing Pack ages; A sim ple Swing A p plication; Cr eate a Swing A p plet; Jla bel and Dept . of ISE , SJBIT
Java and J2EE (06IS753)
ImageIcon; JTextField;The Swing Buttons; JTa b bed pane; JScr ollPane; JList; JCom boBox; JTa ble. 7 Hours
PAR T - B UNIT - 5 JAVA 2 ENTER PR ISE EDITION OVER VIEW, DATABASE ACCESS: Over view of J2EE and J2SE. The Conce pt of JDBC; JDBC Dr iver Ty pes; JDBC Pack ages; A Br ief Over view of the JDBC pr ocess; Data base Connection; Associating the JDBC/ODBC Br idge with the Data base; Statement O b jects; R esultSet; Tr ansaction Pr ocessing; Metadata, Data ty pes; Exce ptions. 6 Hours UNIT - 6 SER VLETS: Back gr ound; The Lif e Cycle of a Ser vlet; Using Tomcat f or Ser vlet Develo pment; A sim ple Ser vlet; The Ser vlet API; The Javax.ser vlet Pack age; R eading Ser vlet Par ameter ; The Javax.ser vlet.htt p pack age; Handling HTTP R equests and R es ponses; Using Cook ies; Session Tr ack ing. 7 Hours UNIT - 7 JSP, R MI: Java Ser ver Pages (JSP): JSP, JSP Tags, Tomcat, R equest Str ing, User Sessions, Cook ies, Session O b jects. Java R emote Method Invocation: R emote Method Invocation conce pt; Ser ver side, Client side. 6 Hours UNIT - 8 ENTER PR ISE JAVA BEANS: Enter pr ise java Beans; De ployment Descr i ptor s; Session Java Bean, Entity Java Bean; Message-Dr iven Bean; The JAR File. 7 Hours TEXT BOOK S: th 1. Java - The Complete R ef erence – Her ber t Schildt, 7 Edition, Tata McGr aw Hill, 2007. 2. J2EE - The Complete R ef erence – Jim K eogh, Tata McGr aw Hill, 2007. R EFER ENCE BOOK S: th 1. Introduction to JAVA Programming – Y. Daniel Liang, 6 Edition, Pear son Education, 2007. nd 2. The J2EE Tutorial – Ste phanie Bodof f et al, 2 Edition, Pear son Education, 2004. Dept . of ISE , SJBIT
Java and J2EE (06IS753)
UNIT No. 1
2
3
Table of Contents INTR ODUCTION TO JAVA
Page No. 1-12
Java and Java a p plications; Java Develo pment K it (JDK ); Java is inter pr eted, Byte Code.
1
JVM , O b ject-or iented pr ogr amming; Sim ple Java pr ogr ams
1
Data ty pes and other tok ens: Boolean var ia bles, int, long, char , o per ator s, ar r ays, white s paces, liter als,
3
Assigning values ,Cr eating and destr oying o b jects; Access s pecif ier s.
4
o per ator s and Ex pr essions: Ar ithmetic O per ator s, Bitwise o per ator s, R elational o per ator s, The Assignment O per ator , The ? O per ator ; O per ator Pr ecedence; Logical ex pr ession;
4
Ty pe casting; Str ings , Contr ol Statements: Selection statements, iter ation statements, Jum p Statements
6
CLASSES, INHER ITANCE, EXCEPTIONS, APPLETS
13-28
Classes: Classes in Java; Declar ing a class; Class name; Su per classes; Constr uctor s; Cr eating instances of class; Inner classes
13
Inher itance: Sim ple, multi ple, and multilevel inher itance; Over r iding, over loading.
15
Exce ption handling: Exce ption handling in Java.
18
The A p plet Class: Two ty pes of A p plets; A p plet basics; A p plet Ar chitectur e; An A p plet sk eleton; Sim ple A p plet dis play method
20
R equesting r e painting; Using the Status Window; The HTML APPLET tag; Passing par ameter s to A p plets; getDocument base() and getCode base()
22
A pletContext and showDocument(); The AudioCli p Inter f ace; The A p pletStu b Inter f ace; Out put to the Console.
23
MULTI THR EADED PR OGR AMMING, EVENT HANDLING
Multi Thr eaded Pr ogr amming: What ar e thr eads? How to mak e the classes thr eada ble;
Dept . of ISE , SJBIT
29-41
29
Java and J2EE (06IS753)
4
5
Extending thr eads; Im plementing r unna ble; Synchr onization; Changing state of the thr ead; Bounded buf f er pr o blems
30
Read-wr ite pr o blem, pr oducer -consumer pr o blems
33
Two event handling mechanisms
35
The delegation event model; Event classes; Sour ces of events
36
Event listener inter f aces; Using the delegation event model
37
Ada pter classes; Inner classes
39
SWINGS
Swings: The or igins of Swing; Two k ey Swing f eatur es
42
Com ponents and Container s
42
The Swing Pack ages; A sim ple Swing A p plication
45
Cr eate a Swing A p plet
46
Jla bel and ImageIcon
47
JTextField;The Swing Buttons; JTa b bed pane
48
JScr ollPane; JList; JCom boBox; JTa ble
49
JAVA 2 ENTER PR ISE EDITION OVER VIEW, DATABASE ACCESS:
Over view of J2EE and J2SE.
6
42-49
50-53 50
The Conce pt of JDBC; JDBC Dr iver Ty pes; JDBC Pack ages
50
A Br ief Over view of the JDBC pr ocess; Data base Connection;
50
Associating the JDBC/ODBC Br idge with the Data base;
51
Statement O b jects ,R esultSet;
52
Tr ansaction Pr ocessing Metadata,. Data ty pes; Exce ptions.
53
SER VLETS
54-60
Back gr ound
54
The Lif e Cycle of a Ser vlet; Using Tomcat f or Ser vlet Develo pment; A sim ple Ser vlet
54
The Ser vlet API; The Javax.ser vlet Pack age
55
Dept . of ISE , SJBIT
Java and J2EE (06IS753)
Reading Ser vlet Par ameter ; The Javax.ser vlet.htt p pack age
7
8
56
Handling HTTP R equests and R es ponses
57
Using Cook ies
58
Session Tr ack ing.
59
JSP, R MI
61-65
Java Ser ver Pages (JSP): JSP, JSP Tags
61
Tomcat, R equest Str ing, User Sessions
62
Cook ies, Session O b jects
64
Java R emote Method Invocation: Remote Method Invocation conce pt
64
Ser ver side
65
Client side.
65
KSIMC
65
Dic single windows agency SISI N , SIC,SIDBI,K SFC
65
ENTER PR ISE JAVA BEANS
66-68
Enter pr ise java Beans;
66
De ployment Descr i ptor s
67
Session Java Bean
67
Entity Java Bean;
67
Message-Dr iven Bean, The JAR File.
68
Dept . of ISE , SJBIT
Java and J2EE (06IS753) UNIT-1 INTR ODUCTION TO JAVA
Java and Java a p plications; Java Develo pment K it (JDK ); Java is inter pr eted, Byte Code, JVM; O b jector iented pr ogr amming; Sim ple Java pr ogr ams. Data types and other tok ens:
Boolean var ia bles, int, long, char , o per ator s, ar r ays, white s paces, liter als, assigning values; Cr eating and destr oying o b jects; Access s pecif ier s. Operators and Expressions: Ar ithmetic O per ator s,
Bitwise o per ator s, R elational o per ator s, The Assignment O per ator , The? O per ator ; O per ator Pr ecedence; Logical ex pr ession; Ty pe casting; Str ings Contr ol Statements: Selection statements, iter ation statements, Jum p Statements.
SJBIT, Dept . of ISE
Page 1
Java and J2EE (06IS753)
1.1. Introduction to Java
Java is an o b ject-or iented pr ogr amming language develo ped by Sun Micr osystems, a com pany best k nown f or its high-end Unix wor k stations. Java is modeled af ter C++ Java language was designed to be small, sim ple, and por ta ble acr oss platf or ms and o per ating systems, both at the sour ce and at the binar y level (mor e a bout this later ). Java also pr ovides f or por ta ble pr ogr amming with a p plets. A p plets a p pear in a We b page much in the same way as images do, but unlik e images, a p plets ar e dynamic and inter active. A p plets can be used to cr eate animations, f igur es, or ar eas that can r es pond to in put fr om the r eader , games, or other inter active ef f ects on the same We b pages among the text and gr a phics.
1.1.2 Java Is Platf orm-Independent Pl at f or m-i nd e pend ence is a program's capability of moving easily f rom one computer system to another.
Platf or m inde pendence is one of the most signif icant advantages that Java has over other pr ogr amming languages, par ticular ly f or systems that need to wor k on many dif f er ent platf or ms.
Java is platf or m-inde pendent at both the sour ce and the binar y level.
1.1.2 Java Development K it (JDK )- Byte code
B yt ecod e s ar e a set of instr uctions that look a lot lik e machine code, but ar e not s pecif ic to any one pr ocessor
Platf or m-inde pendence doesn't sto p at the sour ce level, however . Java binar y f iles ar e also platf or m-inde pendent and can r un on multi ple platf or ms without the need to r ecom pile the sour ce. Java binar y f iles ar e actually in a f or m called bytecodes.
1.1.3 Ob ject-Oriented Programming
Many of Java's o b ject-or iented conce pts ar e inher ited f r om C++, the language on which it is based, but it bor rows many conce pts f r om other o b ject-or iented languages as well.
SJBIT, Dept . of ISE
Page 2
Java and J2EE (06IS753) Java includes a set of class li br ar ies that pr ovide basic data ty pes, system in put and out put ca pa bilities, and other utility f unctions.
These ba sic cla sses a r e p a r t of the Ja va de ve lo pme nt k it, which als o ha s class es to s u p p or t netw or k ing, com mon Inter net p r otoc ols, and user i nte r f ace tool k it f unctions.
Because t he se c las s li b r a r ie s a r e w r itte n i n Ja va, t he y a r e p or ta ble acr oss p latf or m s as all J ava a p p lica tio ns a r e.
1.1.4 Creating a simple Java Program H ello Wor ld exam ple :
clas s Hel loWor ld { p u b lic stat ic void ma in (St r i ng a r gs[] ) { Sys tem.out. p r i ntl n(" Hell o Wor ld! " ); } }
This p r ogr a m ha s two ma in pa r ts : All t he pr ogr am i s e ncl osed i n a clas s def initi on —he r e, a c lass called H ello Wor ld.
The b ody of the p r ogr am (he r e, j ust the one line ) is containe d in a met hod (f unction) ca lled main(). In Java a p plications, as in a C or C ++ pr ogr am, main() is the f ir st meth od (f uncti on ) that i s r un whe n the p r ogr am is executed.
1.1.5 Compiling the above program :
In Sun's J D K , the Ja va co m pi ler is called j avac.
j a v a c H e l l o W o r l d . j a v a Whe n the p r o gr am com p iles wit hout e r r or s , a f ile ca lled He lloW or ld.cla ss is cr eated, i n the same di r ect or y a s the s our ce f ile. T his is the Ja va b ytecode f ile.
Then r un that b ytecode f ile using t he Ja va inte r p r ete r . In t he J DK , t he Ja va inter p r ete r i s ca lle d sim p ly j a va.
j a va Hell oWor l d If the p r ogr am was ty pe d and com p iled cor r ectly, the out put will be : " Hello Wor ld!"
1.2
Variables an d Data Types
Var ia ble s a r e l ocati ons i n memor y in which val ues can b e stor ed. They have a name,
SJBIT, Dept . of ISE
Page 3
Java and J2EE (06IS753) a ty pe, and a val ue. Ja va has thr ee k i nds of var ia b les : insta nce var ia b les, c lass var ia ble s, and local va r ia b les. In stance va r ia ble s, a r e u sed t o def i ne attr i b utes or the s ta te f or a pa r tic ula r o b j ect. Clas s va r ia b les a r e s imila r t o instance va r ia b les, e xce pt their values a p pl y to all tha t clas s' s i nsta nces (and t o t he c lass its elf ) r athe r than ha vin g dif f e r ent val ues f or eac h o b ject.
Local va r ia bles ar e decla r ed and used i ns ide met hod def initions, for exam ple, f or index counter s in loo p s, a s tem por a r y va r ia bles, or to hold values t hat yo u need onl y insi de the method def i nit ion itself
Var ia b le declar ations consist of a ty pe a nd a var ia ble name: Exam ples : int myAge; S t r i n g m y N a m e ; b o o l e a n i s T i r e d ; 1 . 2 . 1 I n t e ge r t y pe s.
T y p e S i z e R a n g e b yte 8 b its —128 to 127 s hor t 16 b its —32,768 to 32,767 i nt 3 2 bit s —2, 147, 483,6 48 to 2,147,483,647 — 92233 720368 54775808 to 9223372036854775807 long 64 b its
1. 2.2 Fl o ati ng -poi n t
This is used f or num ber s with a decimal p ar t. Java f loa ting- p oint num ber s ar e com pliant w ith IEEE 754 (an inter nati ona l sta nda r d f or def ini ng f loat ing - p oi nt num b er s a nd a r it hmet ic). Ther e ar e two f loating- point ty p es: f loat (32 bits, single- pr ecision) and dou b le (64 bits, dou ble - pr eci si on). 1. 2.3 C h ar
SJBIT, Dept . of ISE
Page 4
Java and J2EE (06IS753) The char ty pe is used f or individual char acter s. Because Ja va uses the Unicode char acte r se t, the char t y pe has 16 bi ts of p r ecisi on, unsi gne d. 1. 2.4 Bo ole an
The boolean t y p e ca n ha ve one of t wo value s, t r ue or f alse. Note t hat unl ik e i n o the r C lik e languages, boolean is not a num ber , nor can it be tr eated as one. All tests of Boolean va r ia b les s houl d test f or tr ue or f a lse. 1. 2. 5 Lite r als
Liter als ar e used to indicate sim ple va lues in your Java pr ogr ams. N um be r Lite r al s The r e a r e se ve r al inte ge r l iter a ls. 4, f or e xam p le, is a decima l i nte ger l ite r al of t y p e i nt
A de cimal i nte ge r lite r al la r ge r tha n a n int is aut omat ically of ty pe long.
Fl oati ng- poi nt lite r a ls us uall y ha ve t wo par ts: the inte ge r p a r t and t he dec imal par t— for exam ple, 5.677777.
Boolean Lite r al s Boolean liter als consist of the k eywor ds tr ue a nd f alse. Thes e k eywor ds can be used anywhe r e needed a test or as the onl y possi ble va lues f or b oolea n var ia b le s. 1 . 2 . 6 C ha r a c t e r L i t e r a l s
Char acter liter a ls ar e ex p r esse d by a single char acter sur r ounded by single quotes: 'a', '#', ' 3', and s o on. Cha r acte r s a r e stor ed as 16 - bi t Un ic ode char acte r s. 1. 3 Express ions and Operat ors
Ex pr e ssi ons a r e the s im plest f or m of stateme nt i n Ja va tha t act uall y acc om pl ishes some thi ng. E x p r e s s i o n s a r e stateme nts t hat r et ur n a va lue.
O p e r a t o r s a r e s p ecial s ym bols tha t a r e commonl y us ed in e x p r es si ons.
A r ithmetic a nd te sts f or equali t y and ma gnitude a r e c ommon exa m ple s of ex p r ess ions. B ecause the y r e tur n a va luet he va lue can be ass i gne d to a var ia ble or te st that va lue in other Ja va sta tements. O per ator s in Java include ar ithmetic, var ious f or ms of ass ignment, incr ement and de cr eme nt, and logical o pe r ati ons. 1. 3.1 Ar i thme tic SJBIT, Dept . of ISE
Page 5
Java and J2EE (06IS753) Ja va has f i ve o per ato r s f or b as ic ar it hmet ic A ri t hme t ic oper ator s.
O p e r a t o r M e a n i n + A d di t i o n — S u bt ract io n *
E xam le 3+4 5 —
M ul ti p licati on 5 * 5
/
D i vi s i o n
%
14 / 7
Modul us 20 % 7
E x a m p l e p r o g r a m : c l a s s A r i t h m e t i c T e s t { p u b l i c s t a t i c v o i d m a i n ( S t r i n g a r g s [ ] ) { s o r t x = 6 ; nt y = 4; f l o a t a = 1 2 . 5 f ; f l o a t b = 7 f ; S ys t e m. out. p r nt n " x + y = " + S ys t e m. out. p r i nt ln(" x - y = " + S ys t e m. out. p r i nt ln(" x / y = " + S ys t e m . o u t . p r n t n " a s " + a + " , }}
+ ", x ( x
S s t e m . o u t . r i n t l n " a / b = " + a / b
y is "
+ y);
+ y) ); - y)); / y)) ; s"
+ b ;
Assignment operators.
E x p r e s s i o n M e a n i n g x += y x
x=x+y
—= y x = x — y
x *= y
x=x*y
x = x / y x /= y
Incrementing and Decrementing
x++ incr ements the value of x b y 1 j ust as if you had used the ex p r ession x = x + 1. Si m il a r l y x -- de c r em e n ts t he va lue o f x b y 1.
SJBIT, Dept . of ISE
Page 6
Java and J2EE (06IS753) E x e r c i s e : w r i t e t h e d i f f e r e n c e b e t w e e n : y = x++; y
= ++x;
Comparison operators.
O p e r a t o r M e a n i n g
E x a m p l e
==
x == 3
!= < > <=
Equa l
N ot equal L e s s t ha n G r ea ter t han Les s tha n or equal
x x < x > x
>= Gr ea ter tha n or e qua l t o x >= 3 Logical Operators
E x pr e ssi ons t hat r esult i n b oolean va lues (f or e xa m ple, the c om p ar ison o pe r ator s ) ca n be com b ined b y usi ng logical o pe r ator s t hat r e p r e sent the logical com b inat ions A ND, OR , XOR , and logi cal NOT. F or A ND com b inations, use eit her the & or &&. The e x p r es sion will b e t r ue onl y if b ot h ex p r es si ons a r e als o t r ue
F or OR ex p r ess ions, use eit her | or ||. OR ex p r es si ons r es ult i n tr ue if eit he r or b oth of t he o pe r ands is al so tr ue In addit ion, the r e is the XOR o per a tor ^, whic h r etur ns t r ue onl y if i ts o p er a nds a r e dif f er ent (one tr ue and one f alse, or vice ver sa) and f alse other wise (even if both ar e t r ue).
In ge ner a l, onl y t he && a nd | | a r e commonl y used as actua l lo gical c om b inat ions . &, |, and ^ ar e mor e c omm onl y use d f or b it wise logical o p er at ions.
F or NO T, use t he ! o per ator wi th a si ngle e x p r es si on a r gume nt. The val ue of t he NOT ex p r e ss io n is the ne gation of the ex p r es sion; if x i s tr ue, !x is f alse.
Bitwise Operators These are used to perf orm operations on individual bits in integers.
O p e r a t o r M e a n i n g &
Bi t wise A ND
|
Bi t wise OR
^
Bi t wise XOR
SJBIT, Dept . of ISE
Page 7
Java and J2EE (06IS753) <<
Lef t shif t
>>
Ri ght shif t
>>>
Ze r o f ill r i ght shif t
~
Bi t wise com p le me nt
<<=
Lef t shif t as si gnment (x = x << y)
>>=
Ri ght shif t assi gnme nt (x = x >> y)
>>>= Zer o f ill r ight s hif t a ssignment (x = x >>> y ) x& =y A ND ass i gnment (x = x & y) x |= y
OR assi gnment (x + x | y)
x^ =y
XO R a ssignment (x = x ^ y)
O pe r at or P r e c e de nc e
O per ator pr ecedence deter mines the or der in which ex pr essions ar e evaluated. This, in s ome ca s es, ca n de ter m ine the over a ll value o f the e x p r es si on. For exam p le, tak e the followi ng ex p r ess ion: y=6+4/2 De pending on whether the 6 + 4 ex pr ession or the 4 / 2 ex pr ession is evaluated f ir st, the va lue of y can end u p b eing 5 or 8. In ge ner al, i nc r ement and decr eme nt a r e e va luate d be f or e a r i thme tic, a r it hmetic e x p r es si ons a r e e val uated bef or e com pa r i sons, a nd com pa r i sons ar e e va lua ted bef or e l ogic al ex p r ess ions. Assi gnment ex p r es sions a r e e va l u a t e d l a st . 1. 4 Arrays
Ar r ays in Java ar e actual o b jects that can be passed ar ound and tr eated just lik e other o b jects . A r r a y s a r e a wa y to st or e a li st of items. Each slot of the ar r a y hol ds an indivi dua l element, and you can place elements into or change the contents or those slots as you ne e d t o. Thr ee ste p s to cr eate an a r r ay : 1.
Declar e a var ia b le t o hold the ar r a y.
SJBIT, Dept . of ISE
Page 8
Java and J2EE (06IS753) 2.
Cr eate a new ar r a y o b j ect and assi gn it to the ar r a y va r ia ble.
3.
St or e thi ngs in tha t a r r ay.
E . g. r n g names; names = new r ng n a me s = n ; names = n ;
;
... 1 . 4. 1 Mul ti di me nsi o na l Ar r a y s
Java doe s not su p p or t multidimensional ar r ays. Howeve r , you can declar e and cr eate an a r r a y of ar r a ys (and thos e a r r a ys ca n contai n ar r ays, and so on, f or howe ver ma ny dimensi ons you need), and ac cess them as you woul d C-s t yle mult idi me nsional ar r a ys: i nt c oor d s [ ] [] = n ew i nt [ 12] [12] ; c oor d s [ 0] [ 0] = 1; c oor ds [ 0] [1] = 2;
1. 5 Contr ol State ment 1 . 5 . 1 i f C o n d i t i o n a l s
The if conditi ona l, which e na b les you to e xecute dif f er ent bi ts of code b ase d on a s im p le test in Ja va, is nea r ly i dent ical to if stateme nts i n C.
if condi tional s contai n the k e ywor d if , f oll owed b y a b oolean tes t, f oll owed b y a s tatement (of ten a block s tatement ) to execute if the tes t is tr ue:
if (x < y) Sys tem.out. p r i ntl n(" x is smal ler than y" );
A n o pti onal el se k eywor d p r ovi de s t he state ment to execu te if the test i s f alse: if (x < y) System.out. pr intln(" x is smaller than y" ); el se Sys tem.out. p r i ntl n(" y is bi gge r " );
1. 5.2 Th e Co nd i ti onal O pe rat or
An alter native to us ing the if and else k eywor ds in a conditional statement is to use the conditional o pe r ator , somet imes calle d the ter na r y o per a tor .
SJBIT, Dept . of ISE
Page 9
Java and J2EE (06IS753) The cond it ional o pe r at or is a t er nar y o pe r at or becau se it ha s thr ee te r ms. S ynt a x : t e s t ? t r ue r e su l t : f a l se r e s ul t The t e st is an ex pr e ssion that r etur ns tr ue or f alse, just lik e the test in the if sta tement. If t he tes t i s t r ue, the condi tional o pe r at or r e tur ns t he va lue of t r uer e s ul t ; if it's f alse, it r etur ns t he val ue of f al se r e s ul t . For exam ple, the f ollowing conditional tests the values of x and y, r etur ns the smaller of t he two, and as signs that val ue to the va r ia ble smal ler : i nt s ma l l e r = x < y ? x : y; The c ondit ional o pe r ator has a ve r y l ow p r ecede nce; that is , i t's us uall y e valua ted onl y af te r a ll its su bex pr essions ar e evaluated. The only o per ator s lower in p r ecedence ar e the as si gnme nt o per at ors. . 1. 5. 3 switc h Co ndit ional s
This is the s witc h or case s tatemen t; in Ja va it 's s wi tch and b e ha ve s a s it does in C: switch (te st ) va lu eOn e :
{
ca se
res u lt One ; b r e a k ; c ase val ue Two : r e s u lt Tw o; b r e a k ; c a s e v a l u e T h r e e : r e s u l t T h r e e ; b r e a k ; . . . d e f a u l t : d e f a u l t r e s u l t ; } In the switch statement, the test (a p r imitive ty p e of byte, c har , shor t, or int) is com par ed with each of the case values in tur n. If a match is f ound, the statement, or statements af ter the test is executed. If no match is f ound, the def ault statement is executed. The def ault is o p tional, s o if t her e is n't a matc h i n a ny of the ca ses and def a ult doesn't exist, the s witc h s tatement com p letes wit hout doing a nythi ng.
SJBIT, Dept . of ISE
Page 10
Java and J2EE (06IS753) 1 . 5. 4 f or Lo o ps
The f or loo p, as i n C, r e peat s a sta tement or b loc k of stateme nts some num be r of times until a condi tion is matc hed. f or loo p s a r e f r eque ntly used f or sim ple ite r ati on in whic h you r e peat a block of sta tements a cer tain num ber of times and then sto p, b ut you can use for loo p s f or j ust a b out any k i nd of loo p . The f or loo p in Ja va look s r ou ghl y li k e thi s: f or (i ni ti ali zat i on; te s t ; i nc r e me nt ) { s t a t e m e n t s ; } The sta r t of the f or loo p has t hr e e pa r t s: Initia li zation is a n ex p r es si on that i nitia li zes the sta r t of t he l oo p. If you ha ve a loo p index, this ex pr ession might declar e and initia lize it, f or exam ple, int i = 0. Var ia b les that you decla r e in this p ar t of the f or loo p ar e local to the loo p itself ; t he y cease existi ng af te r t he loo p is f i nis hed exec uting. Test is t he te st t hat occur s af te r eac h pas s of the loo p . The te st mus t be a b oolean ex p r ession or f unction that r etur ns a boolean value, f or exam ple, i < 10. If the test is tr ue, the l oo p exec utes. Once the test i s f alse, the l oop s to ps executing • Inc r eme n t is a ny ex p r e ssi on or f uncti on call. Commonl y, t he inc r ement is used to change the value of the loo p index to br ing the state of the loo p closer to r etur ning f alse and com pleti ng.
The stateme nt pa r t of the f or l oo p is the s ta te me nts tha t a r e exec uted each ti me the loo p i ter ate s. Just a s wit h if , you ca n i ncl ude ei ther a single sta te me nt her e or a bl ock ; t he pr evious exam ple used a block because that is mor e common. Her e 's an e xam ple of a f or l oo p that i nitia li zes all the va lues of a St r ing a r r a y to nul l st r i ngs : Str ing str Ar r ay[] = new Str ing[10]; in t i ; // loo p inde x for (i = 0; i < str Ar r ay. length; i++) s t r Ar r a y[i] = " " ;
1. 5. 5 while and do Loops
Finally, the r e ar e while and do loo ps. while and do loo ps, lik e f or loo ps, ena ble a b lock of Ja va code to b e executed r e peated ly until a s pec if ic c ondition is met. Whe the r you use a for l oo p, a while, or a do is mos tly a mat ter of your p r o gr a mmi ng s tyl e. while and do loo p, ar e exactly the same as in C and C ++ exce pt their test condition must be a boolean . 1. 5. 6 while Loo ps SJBIT, Dept . of ISE
Page 11
Java and J2EE (06IS753) The while loo p is used to r e peat a statement or block of statements as long a s a par ticular condition is t r ue . w hile l oo ps l ook lik e this: w h il e (c o nd i t i o n ) b odyOf Loo p; }
{
The c o n d i t i o n is a boolean ex pr ession. If it r etur ns tr ue, the while loo p executes the s tatements i n bod yOf Lo o p a nd then tes ts t he conditi on a ga in, r e pe ating unt il the condition is f als e: i nt c ou nt = 0; while ( count < ar r ay 1 .length && ar r ay 1 [count] !=0) { a r r a y2[c ount] = (f l oat ) a r r a y1[c ount ++]; } 1. 5. 7 do. .. whi le Loops
The do loo p is j ust lik e a while loo p, exce pt that do e xecutes a given statement or block until the condition is f alse. The main dif f er e nc e is t hat while loo p s test the con dition be f or e l oo ping, ma k ing i t p os si ble that t he b ody of the loo p w ill ne ve r exec ute if the condition is f alse the f ir st time it's tested. do loo p s run the body of the loo p at least once be f or e testi ng the condition. do loo p s l ook lik e t his : do { b odyOf Loo p; } wh ile ( c o n d i t i o n ) ; H er e, the b odyOf Loo p pa r t is the s ta teme nt s that ar e executed with e ach i nt x = 1 ; do { System.out. pr intln(" Loo ping, r ound " + x); x++; } wh il e ( x < = 1 0 ) ; H er e 's the out p ut of these s tatements: Loo pi ng, r ound 1 Loo pi ng, r ound 2 Loo p ing, r ound 3 Loo p ing, r ound 4 Loo pi ng, r ound 5 Loo pi ng, r ound 6 Loo p i n g, r ou nd Loo p i n g, r ou nd Loo p i ng, r o un d Lo o p i n g, r o u n d SJBIT, Dept . of ISE
Page 12
Java and J2EE (06IS753)
UNIT-2: CLASSES, INHERITANCE, EXCEPTIONS, APPLETS
Classes:
Classes in Java; Declar ing a class; Class name; Su per classes; Constr uctor s; Cr eating instances of class; Inner classes. Inher itance: Sim ple, multi ple, and multilevel inher itance; Over r iding, over loading.
Exception handling: Exce ption handling in Java.
The Applet Class:
Two ty pes of A p plets; A p plet basics; A p plet Ar chitectur e; An A p plet sk eleton; Sim ple A p plet dis play methods; R equesting re painting; Using the Status Window; The HTML APPLET tag; Passing par ameter s to A p plets; getDocument base() and getCode base(); A pletContext and showDocument(); The AudioCli p Inter f ace; The A p pletStu b Inter f ace; Out put to the Console.
SJBIT, Dept . of ISE
Page 13
Java and J2EE (06IS753)
2.1. Def ining Classes, Class Name
To def ine a class, use the class k eywor d and the name of the class:
class MyClass Name { ... }
If this class is a su bclass of another class, use extends to indicate the su per class of this
class: class myClass Name extends mySu per Class Name { ... } If this class im plements a s pecif ic inter f ace, use im plements to r ef er to that inter f ace: class MyR unna bleClass Name im plements R unna ble {
... } Super Classes
Each class has a su per class (the class a bove it in the hier ar chy), and each class can have one or mor e su bclasses (classes below that class in the hier ar chy). Classes f ur ther down in the hier ar chy ar e said to inher it f r om classes f ur ther u p in the hier ar chy.
Su bclasses inher it all the methods and var ia bles f r om their su per classes—that is, in any par ticular class, if the su per class def ines behavior that your class needs, you don't have to r edef ine it or co py that code f r om some other class. Your class automatically gets that behavior f r om its su per class, that su per class gets behavior f r om its su per class, and so on all the way u p the hier ar chy.
At the top of the Java class hierarchy is the class Ob ject; all classes inher it f r om this one su per class. O b ject is the most gener al class in the hier ar chy; it def ines behavior inher ited by all the classes in the Java class hier ar chy. Each class f ar ther down in the hier ar chy adds mor e inf or mation and becomes mor e tailor ed to a s pecif ic pur pose.
E.g.
pu blic class HelloAgainA p plet extends java.a p plet.A p plet { }
SJBIT, Dept . of ISE
Page 14
Java and J2EE (06IS753) 2.2 Constructors, Creating instances of a class
The f ollowing exam ple demonstr ates the cr eation of classes, cr eating o b jects and the usage of constr uctor s class Motor cycle { } cr eate some instance var ia bles f or this class Str ing mak e; Str ing color ; boolean engineState; Add some behavior (methods) to the class. void star tEngine() { if (engineState == tr ue) System.out. pr intln("The engine is alr eady on."); else { engine State = tr ue; System.out. pr intln("The engine is now on."); } } The pr ogr am look s lik e this now : class Motor cycle { Str ing mak e; Str ing color ;
engine is alr eady on."); engine on.");
is
now
boolean engineState; void star tEngine() { if (engineS tate == tr ue) System.out. pr intln("The } The showAtts method pr ints the cur r ent values of the instance var ia bles in an instance of your Motor cycle class. Her e's what it look s lik e: void showAtts() { System. out p . r intln ("This motor cycle is a " + color + " " + mak e); if (engineState == tr ue) System.out. pr intln("The engine is on."); else System.out. pr intln("The engine is of f .");
SJBIT, Dept . of ISE
Page 15
Java and J2EE (06IS753) The showAtts method pr ints two lines to the scr een: the mak e and color of the motor cycle o b ject, and whether or not the engine is on or of f . 2.2.1 Add the main method
pu blic static void main (Str ing ar gs[]) { Motor cycle m = new Motor cycle(); m.mak e = "Yamaha RZ350"; m.color = "yellow"; System.out. pr intln("Calling showAtts..."); m.showAtts(); System.out p . r intln(" — — —
— "); System.out. pr intln("Star ting engine..."); m.star tEngine(); System.out. pr intln(" — — — — "); System.out. pr intln("Calling showAtts..."); m.showAtts(); System.out p . r intln(" — — —
— "); System.out. pr intln("Star ting engine..."); m.star tEngine(); } With the main() method, the Motor cycle class is now an a p plication, and you can com pile it again and this time it'll r un. Her e's how the out put should look : Calling showAtts... This motor cycle is a yellow Yamaha RZ350 The engine is of f . Star ting engine... The engine is now on. Calling showAtts... This motor cycle is a yellow Yamaha R Z350 The engine is on. Star ting engine... The engine is alr eady on. 2.3. Inheritance
Inher itance is a power f ul mechanism that means when you wr ite a class you only have to s pecif y how that class is dif f er ent f r om some other class; inher itance will give you automatic access to the inf or mation contained in that other class. With inher itance, all classes—those you wr ite, those f r om other class li br ar ies that you use, and those f r om the standar d utility classes as well—ar e ar r anged in a str ict hier ar chy
SJBIT, Dept . of ISE
Page 16
Java and J2EE (06IS753) 2.3.1 Single and Multiple Inheritance
Single inher itance means that each Java class can have only one su per class (although any given su per class can have multi ple su bclasses). In other o b ject-or iented pr ogr amming languages, such as C++, classes can have mor e than one su per class, and they inher it com bined var ia bles and methods f r om all those classes. This is called multi ple inher itance. Multi ple inher itance can pr ovide enor mous power in ter ms of being a ble to cr eate classes that f actor just a bout all imagina ble behavior , but it can also signif icantly com plicate class def initions and the code to pr oduce them. Java mak es inheritance simpler by being only singly inherited. 2.3.2 Overriding Methods
When a method is called on an o b ject, Java look s f or that method def inition in the class of that o b ject, and if it doesn't f ind one, it passes the method call u p the class hier ar chy until a method def inition is f ound. Method inher itance ena bles you to def ine and use methods r e peatedly in su bclasses without having to du plicate the code itself . However , ther e may be times when you want an o b ject to r es pond to the same methods but have dif f er ent behavior when that method is called. In this case, you can over r ide that method. Over r iding a method involves def ining a method in a su bclass that has the same signatur e as a method in a su per class. Then, when that method is called, the method in the su bclass is f ound and executed instead of the one in the su per class.
2.3.3 Creating Methods that Override Existing Methods
To over r ide a method, all you have to do is cr eate a method in your su bclass that has the same signatur e (name, r etur n ty pe, and par ameter list) as a method def ined by one of your class's su per classes. Because Java executes the f ir st method def inition it f inds that matches the signatur e, this ef f ectively "hides" the or iginal method def inition. Her e's a sim ple exam ple The PrintClass class.
class Pr intClass { int x = 0; int y = 1; void pr intMe() { System.out. pr intln("X is " + x + ", Y is " + y); System.out. pr intln("I am an instance of the class " + this.getClass().get Name());
SJBIT, Dept . of ISE
Page 17
Java and J2EE (06IS753) Cr eate a class called Pr intSu bClass that is a su bclass of (extends) Pr intClass. class Pr intSu bClass extends Pr intClass { int z = 3; pu blic static void main(Str ing ar gs[]) { Pr intSu bClass o b j = new Pr intSu bClass(); o b j . pr intMe(); } } Her e's the out put f r om Pr intSu bClass: X is 0, Y is 1 I am an instance of the class Pr intSu bClass In the main() method of Pr intSu bClass, you cr eate a Pr intSu bClass o b ject and call the pr intMe() method. Note that Pr intSu bClass doesn't def ine this method, so Java look s f or it in each of Pr intSu bClass's su per classes—and f inds it, in this case, in Pr intClass. because pr intMe() is still def ined in Pr intClass, it doesn't pr int the z instance var ia ble. To call the or iginal method f r om inside a method def inition, use the su per k eywor d to pass the method call u p the hier ar chy: void myMethod (Str ing a, Str ing b) { // do stuf f her e su per .myMethod(a, b); // may be do mor e stuf f her e } The su per k eywor d, somewhat lik e the this k eywor d, is a placeholder f or this class's su per class. You can use it anywher e you can use this, but to r ef er to the su per class r ather than to the cur r ent class. 2.4 Exception handling
An exce ption is an event that occur s dur ing the execution of a pr ogr am that disr u pts the nor mal flow of instr uctions. 2.4.1 The Three K inds of Exceptions
Check ed exce ptions ar e sub ject to the Catch or S pecif y R equir ement. All exce ptions ar e check ed exce ptions, exce pt f or those indicated by Er r or , R untimeExce ption, and their su bclasses.
Er r or s ar e not sub ject to the Catch or S pecif y R equir ement. Er r or s ar e those exce ptions indicated by Er r or and its su bclasses.
R untime exce ptions ar e not sub ject to the Catch or S pecif y R equir ement. R untime exce ptions ar e those indicated by R untime Exce pt ion and its su bclasses.
SJBIT, Dept . of ISE
Page 18
Java and J2EE (06IS753) Valid Java pr ogr amming language code must honor the C at ch or S peci f y Requir ement . This means that code that might thr ow cer tain exce ptions must be enclosed by either of the following: • A tr y statement that catches the exce ption. The tr y must pr ovide a handler f or the exce ption, as descr i bed in Catching and Handling Exce ptions. • A method that s pecif ies that it can thr ow the exce ption. The method must pr ovide a thr ows clause that lists the exce ption, as descr i bed in S pecif ying the Exce ptions Thr own by a Method. Code that f ails to honor the Catch or S pecif y R equir ement will not com pile. This exam ple descr i bes how to use the thr ee exce ption handler com ponents — the tr y, cat ch, and f inally block s 2.4.2 try block
• The f ir st ste p in constr ucting an exce ption handler is to enclose the code that might thr ow an exce ption within a tr y block . In gener al, a tr y block look s lik e the f ollowing. tr y { cod e } cat ch and f inal l y bl ock s . . . Exam ple : pr ivate Vector vector ; pr ivate static f inal int SIZE = 10; Pr intWr iter out = null; tr y { System.out. pr intln("Enter ed tr y statement"); out = new Pr intWr iter (new FileWr iter ("OutFile.txt")); for (int i = 0; i < SIZE; i++) { out p . r intln("Value at: " + i + " = " + vector .elementAt(i)); } } The catch Block s
SJBIT, Dept . of ISE
Page 19
Java and J2EE (06IS753) You associate exce ption handler s with a tr y block by pr oviding one or mor e catch block s dir ectly af ter the tr y block . No code can be between the end of the tr y block and the beginning of the f ir st catch block . tr y { } catch ( E xce pt ionT y pe name ) { } catch ( E xce pt ionT y pe name ) { } Each catch block is an exce ption handler and handles the ty pe of exce ption indicated by its ar gument finally block
The r untime system always executes the statements within the f inally block r egar dless of what ha p pens within the tr y block . So it's the per f ect place to per f or m cleanu p. The f ollowing f inally block f or the wr ite Li st method cleans u p and then closes the Pr intWr iter. finally { if (out != null) { System.out. pr intln("Closing Pr intWr iter "); out . close(); } else { System.out. pr intln("Pr intWr iter not o pen"); } } 2.5 The Applet Class Applet Basics
An a p plet is a s pecial k ind of Java pr ogr am that a br owser ena bled with Java technology can download f r om the inter net and r un. An a p plet is ty pically em bedded inside a we b- page and r uns in the context of the br owser . An a p plet must be a su bclass of the java.a p plet.A p plet class, which pr ovides the standar d inter f ace between the a p plet and the br owser envir onment. Sim ple exam ple :
pu blic class HelloWor ld extends java.a p plet.A p plet { pu blic void paint( java.awt.Gr a phics g) {
SJBIT, Dept . of ISE
Page 20
Java and J2EE (06IS753) g.dr awStr ing("Hello Wor ld!",50,25); System.out. pr intln("Hello Wor ld!"); } } An a p plet can be included in an HTML page, much in the same way an image is included in a page. When Java technology ena bled Br owser is used to view a page that contains an a p plet, the a p plet's code is tr ansf er r ed to your system and executed by the br owser 's Java Vir tual Machine (JVM) Two Types of Applets
1 .Local a p plet - o per ate in single machine br owser which is not connected in networ k , 2.R emote a p plet - remote a p plet o per ate over inter net via networ k . Applet Architecture
Event dr iven : An a p plet waits until an event occur s. The AW T notif ies the a p plet a bout an event by calling event handler that has been pr ovided by the a p plet. The a p plet tak es a p pr o pr iate action and then quick ly r etur n contr ol to AW T All S win g com ponents descend f r om the AWT Container class User initiates inter action with an A p plet ( and not t he ot her wa y ar ound ) An Applet Sk eleton im por t java.awt.*; im por t javax.swing.*; /*
*/ pu blic class A p pletSk el extends JA p plet { // Called f ir st. pu blic void init() { // initialization } /* Called second, af ter init(). Also called whenever the a p plet is r estar ted. */ SJBIT, Dept . of ISE
Page 21
Java and J2EE (06IS753) pu blic void star t() { // star t or r esume execution } // Called when the a p plet is sto p ped. pu blic void sto p () { // sus pends execution } /* Called when a p plet is ter minated. This is the last method executed. */ pu blic void destr oy() { // per f or m shutdown activities } // Called when an a p plet's window must be r estor ed. pu blic void paint(Gr a phics g) { // r edis play contents of window } } 5.1 Simple Applet Display Methods
void dr awstr ing(Str ing message, int x, int y) - void setBack gr ound(Color newColor ) void setFor egr ound(Color newColor ) Example : pu blic class Sim pleA p plet extends g.dr awStr ing("Fir st A p plet", 50, 50 ); }
A p plet
{ pu blic
void paint
(Gr a phics
g)
{
} Requesting R epainting
r e paint( ) f unction is called when you have changed something and want your changes to show u p on the scr een r e paint( ) is a r eque st --it might not ha p pen When you call r e paint( ), Java schedules a call to u pdate(Gr a phics g) Her e's what u pdate does: pu blic void u pdate(Gr a phics g) { // Fills a p plet with back gr ound color , then
SJBIT, Dept . of ISE
Page 22
Java and J2EE (06IS753) paint(g); Using The Status Window
Syntax : pu blic void showStatus(Str ing status) Parameters:
status - a str ing to dis play in the status window. Requests that the ar gument str ing be dis played in the "status window". Many br owser s and a p plet viewer s pr ovide such a window, wher e the a p plication can inf or m user s of its cur r ent state. Example :
im por t java.a p plet. *; im por t java.awt.*; pu blic class NetExam ple extends A p plet { pr ivate A p pletContext br owser = null; pr ivate Button showStatus = new Button("Show Status"); pu blic void init() { Panel panel = new Panel(); panel.setLayout(new Gr idLayout(1 ,2)); panel.add(showStatus); setLayout(new Bor der Layout()); add("South", panel); br owser = getA p pletContext(); } pu blic boolean action(Event e, O b ject o) { if (e.tar get == showStatus) br owser .showStatus("Her e is something f or your status line ..."); r etur n tr ue; } } 2.5 The HTML Applet Tag
SJBIT, Dept . of ISE
Page 23
Java and J2EE (06IS753)
The APPLET tag is used to star t an a p plet f r om both an HTML document and f r om an a p plet viewer . An a p plet viewer will execute each APPLET tag that it f inds in a se par ate window,while we b br owser s lik e Netsca pe Navigator , Inter net Ex plor er , and HotJava will allow many a p plets on a single page. The syntax f or the standar d APPLET tag is shown her e. Br ack eted items ar e o ptional.
< APPLET [CODEBASE = cod eba seU R L ] CODE = a p pl et F il e [ALT = al t e r nat e T e xt ] [ NAME = a p pl et I n st ance N ame ] WIDTH = pi xel s HEIGHT = pi xel s [ALIG N = al i g nment ] [VSPACE = pi xel s ] [HSPACE = pi xel s] > [< PAR AM NAME = At t r ibut e N ame VALUE = At t r ibut e V al ue>] [< PAR AM NAME = At t r ibut e N ame2 VALUE = At t r ibut e V al ue>] . . . [ H T M L Di s pl a yed in t he ab sence o f J ava ]
CODEBASE is an o ptional attr i bute that s pecif ies the base UR L of the a p plet code, which is the dir ector y that will be sear ched f or the a p plet’s executa ble class f ile (s pecif ied by the CODE tag). The HTML document’s UR L dir ector y is used as the CODEBASE if this attr i bute is not s pecif ied. The CODEBASE does not have to be on the host f r om which the HTML document was r ead. CODE is a r equir ed attr i bute that gives the name of the f ile containing your a p plet’s com piled .class f ile. This f ile is r elative to the code base UR L of the a p plet, which is the dir ector y that the HTML f ile was in or the dir ector y indicated by CODEBASE if set. ALT is an o ptional attr i bute used to s pecif y a shor t text message that should be dis played if the br owser under stands the APPLET tag but can’t cur r ently r un Java a p plets. This is distinct f r om the alter nate HTML you pr ovide f or br owser s that don’t su p por t a p plets. WIDTH AND HEIGHT ar e r equir ed attr i butes that give the size (in pixels) of the a p plet dis play ar ea.
SJBIT, Dept . of ISE
Page 24
Java and J2EE (06IS753)
ALIGN is an o ptional attr i bute that s pecif ies the alignment of the a p plet. This attr i bute is tr eated the same as the HTML IMG tag with these possi ble values:
LEFT, R IGHT, TOP, BOTTOM, MIDDLE, BASELI NE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM. VSPACE AND HSPACE These attr i butes ar e o ptional. VSPACE s pecif ies the s pac e, in p ixel s, a b ove and b elow the a p p let. HS PACE s p ecif ies t he s pace, in pixels, on each s ide of the a p p le t. The y’r e t r ea te d the same as the IMG ta g’s VSP ACE and HSP ACE attr i b utes. PAR AM NAME AND VALUE The PAR AM ta g all ows you to s pecif y a p plet s pecif ic a r gume nts in a n HT ML pa ge. A p plet s ac cess thei r attr i b ute s with the getParameter( ) me t ho d .
Passing Parameters to Applets
Pa r ameter s ar e passed to a p plets in NAME=VALUE pair s in
tags betwee n the o pening a nd closing AP P LET t a gs. • Insi de the a p p let, you r ead the val ues pa ss ed thr ough t he P AR A M ta gs wit h the ge t Pa r a me t e r ( ) me thod of t he j a va . a p p l e t . A p p l e t cla ss. The a p p let pa r ame ter "Me ss age" is the st r in g to be dr awn. im por t java.a p plet. *; im p or t j a va. awt.*; pu blic class Dr awStr ingA p plet exte nds A p plet { p r i va te St r i ng def ault Messa ge = " Hello!" ; pu b lic voi d pa int (Gr a p hics g) { St r i ng i n p ut Fr om Pa ge = this . get Par a mete r (" Mes sa ge" ); if (in putF r omPa ge == null ) in putF r omPage = def aul tMes sa ge; g. dr a wSt r i ng(in p ut Fr omPa ge, 50, 25); } } HT ML f ile tha t r ef er e nces the a b ove a p p let. < H T M L > Dr a w Str ing TITLE> < / HE A D > SJBIT, Dept . of ISE
Page 25
Java and J2EE (06IS753) < B O DY > This is the a p plet: This page will be ver y bor ing if your br owser doesn't under stand Java.