Académique Documents
Professionnel Documents
Culture Documents
Eello2
worl!O
< TOP >
2+
&
; The best metho! signature for is S"ee!ing is "ublic boolean is S"ee!ing/0
< TOP >
2!
&
@ a3G s3blue is "rinte!.
< TOP >
2,
&
@ ookie is the name of a class an! the cookie class must ha-e a constructor that
has one forma "armeter of t$"e string.
< TOP >
$
&
@ -alue 3 i' this.-alue 3 i' the im"lementation of set>alue is the abo-e butnot
-alue33i.
< TOP >
"ection # : Problems
1
&
im"ort ,a-a.io.&'
"ublic class MatternF
(
"ublic static -oi! main/StringJK args0
(
MatternF "at 3 new MatternF/ 0'
;uffere!9ea!er br 3 new ;uffere!9ea!er/new
1n"utStream9ea!er/S$stem.in00'
String s 3 C C'
tr$
(
s 3 br.rea!%ine/0'
*
catch /1:Exce"tion ex0
(
ex."rintStackTrace/0'
*
int n 3 1nteger."arse1nt/s0'
"at."attern/n0'
*
"ri-ate -oi! "attern/ int n 0
(
for/int i 3 1'iB3n'iDD0
(
S$stem.out."rintln/ 0'
for/int , 3 1',B3i ',DD 0
S$stem.out."rint/i0'
*
*
*
<
TOP
>
2& class Account
(
!ouble rate'
int accnumber'
String accname'
int balance'
int counter30'
account/int a2String n2int b0
(
rate30'
accnumber3a'
accname3n'
balance3b'
counterDD'
*
"ublic -oi! with!raw/ int a 0
(
if/aZbalance0
S$stem.out."rintln/Cnot allowe!C0'
<
TOP
>
else
balance3balance-a'
*
"ublic -oi! a!!interest/ !ouble r 0
(
balanceD3/balance&/r)10000'
S$stem.out."rintln/balance0'
*
"ublic String show/ 0
(
return/CThe current balance isCDbalance0'
*
*
class ;AW[
(
"ublic static -oi! main/String argsJ K 0
(
Account a13new Account/1A32CcurrentC2A0000'
a1.with!raw/10000'
a1.a!!interest/100'
S$stem.out."rintln/a1.show/00'
S$stem.out."rintln/a1.counter0'
*
*.
$& class Threa!1 exten!s Threa!
(
"ublic -oi! run/ 0
(
for/int i30'iBA0'iDD0
(
S$stem.out."rintln/C1IA1 VW1>E9S1T?C0'
tr$
(
Threa!.slee"/10000'
*
catch/Exce"tion e0(*
*
*
*
class Threa!A exten!s Threa!
(
"ublic -oi! run/ 0
(
for/int i30'iBA0'iDD0
(
S$stem.out."rintln/C1IA1 MA @istance %earingC0'
tr$
(
Threa!.slee"/A0000'
*
catch/Exce"tion e0(*
*
*
<
TOP
>
*
class Threa!3 exten!s Threa!
(
"ublic -oi! run/0
(
for/int i30'iBA0'iDD0
(
S$stem.out."rintln/C%ASS :I A008C0'
tr$
(
Threa!.slee"/30000'
*
catch/Exce"tion e0(*
*
*
*
class TestThrea!
(
"ublic static -oi! main/ String argsJ K 0
(
Threa! t1 3 new threa!1/ 0'
Threa! tA 3 new threa!A/ 0'
Threa! t3 3 new threa!3/ 0'
t1.start/ 0'
tA.start/ 0'
t3.start/ 0'
*
*
'& AW AMM%ET T: @9A+ A SEAME I1%%E@ +1TE TEE :%:9 SE%ETE@ I9:M TEE
%1ST ;:YES.
im"ort ,a-a.a""let.&'
im"ort ,a-a.awt.e-ent.&'
im"ort ,a-a.awt.&'
"ublic class Sha"e10 exten!s A""let im"lements 1tem%istener
(
%abel s2c'
hoice st2ct'
String sha"e2color'
"ublic -oi! init/0
(
s 3 new %abel/CSha"eC0'
a!!/s0'
st 3 new hoice/0'
st.a!!/CSquareC0'
st.a!!/C:-alC0'
st.a!!/C9ectangleC0'
a!!/st0'
c 3 new %abel/ColorC0'
a!!/c0'
ct 3 new hoice/0'
ct.a!!/C9E@C0'
ct.a!!/C;%VEC0'
ct.a!!/CX9EEWC0'
<
TOP
>
a!!/ct0'
st.a!!1tem%istener/this0'
ct.a!!1tem%istener/this0'
*
"ublic -oi! itemStatehange!/1temE-ent ie0
(
re"aint/0'
*
"ublic -oi! "aint/Xra"hics g0
(
sha"e 3 st.getSelecte!1tem/0'
color 3 ct.getSelecte!1tem/0'
if/color.equals/C9E@C00
(
g.setolor/olor.9E@0'
if/sha"e.equals/CSquareC00
(
g.!raw9ect/102102102100'
g.fill9ect/102102102100'
*
else if/sha"e.equals/C:-alC00
(
g.!raw:-al/102102102100'
g.fill:-al/102102102100'
*
else
(
g.!raw9ect/102102A02100'
g.fill9ect/102102A02100'
*
*
else if/color.equals/C;%VEC00
(
g.setolor/olor.blue0'
if/sha"e.equals/CSquareC00
(
g.!raw9ect/102102102100'
g.fill9ect/102102102100'
*
else if/sha"e.equals/C:-alC00
(
g.!raw:-al/102102102100'
g.fill:-al/102102102100'
*
else
(
g.!raw9ect/102102A02100'
g.fill9ect/102102A02100'
*
*
else
(
g.setolor/olor.green0'
if/sha"e.equals/CSquareC00
(
g.!raw9ect/102102102100'
g.fill9ect/102102102100'
*
else if/sha"e.equals/C:-alC00
(
g.!raw:-al/102102102100'
g.fill:-al/102102102100'
*
else
(
g.!raw9ect/102102A02100'
g.fill9ect/102102A02100'
*
*
*
*
(& im"ort ,a-a.awt.&'
im"ort ,a-ax.swing.&'
im"ort ,a-a.sql.&'
class reate
(
"ublic static -oi! main/String argsJ K0
(
onnection con'
Statement stmt'
tr$
(
lass.forWame/Csun.,!bc.o!bc.<!bc:!bc@ri-erC0'
*
catch/,a-a.lang.lassWotIoun!Exce"tion e0
(
S$stem.err."rint/ClassWotIoun!Exce"tion# C0'
S$stem.err."rintln/e.getMessage/00'
*
Mre"are!Statement "st'
tr$
(
con3@ri-erManager.getonnection/C,!bc#o!bc#m$!ataC0'
stmt 3 con.createStatemen /9esultSet.T?MERS9:%%RSEWS1T1>E2 9esultSet
. :WV9RVM@ATA;%E0'
String qr$ 3 Ccreate table ustomer/ustno char/1002C D Cname
char/1G02a!!ress -archar/A002"hone -archar/1002C D Ccre!itno
-archar/1A00C'
stmt.executeV"!ate/qr$0'
9esultSet u"rs 3 stmt.execute\uer$/Cselect & from ustomerC0'
u"rs.mo-eTo1nsert9ow/0'
u"rs.u"!ateString/CustnoC2C1A3C0'
u"rs.u"!ateString/CnameC2 CabcC0'
u"rs.u"!ateString/Ca!!ressC2 C"an,aguttaC0'
u"rs.u"!ateString/C"honeC2 C1A3FFG678C0'
u"rs.u"!ateString/Ccre!itnoC2 Cabc1A3C0'
u"rs.insert9ow/0'
<
TOP
>
u"rs.beforeIirst/0'
S$stem.out."rintln/CTable @e"t after insertion#C0'
while /u"rs.next/00
(
String no 3 u"rs.getString/CustnoC0'
String name 3 u"rs.getString/CnameC0'
String a!! 3 u"rs.getString/Ca!!ressC0'
String "n 3 u"rs.getString/C"honeC0'
String cr 3 u"rs.getString/Ccre!itnoC0'
S$stem.out."rint/no0'
S$stem.out."rint/name0'
S$stem.out."rint/a!!0'
S$stem.out."rint/"n0'
S$stem.out."rintln/cr0'
*
u"rs.close/0'
stmt.close/0'
con.close/0'
*
catch/S\%Exce"tion ex0
(
S$stem.err."rintln/CS\%Exce"tion# C D ex.getMessage/00'
*
*
*
"ection C: Applied 13eor4
)& An a""let is a s"ecific t$"e of <a-a a""lication that is !esigne! to be "ortable an!
!ownloa!able o-er the 1nternet. Ior this reason an a""let ma$ onl$ contain a subset of
the total functionalit$ of an a""lication. Ior exam"le2 an a""let cannot ha-e access to
local files on a machine' otherwise it is theoreticall$ "ossible for a hostile a""let to
u"loa! /to some !estination0 the contents of a local machine. This t$"e of hostile
a""let also inclu!es a""lets that ma$ tr$ to use all MV a-ailable2 or contact remote
sites2 without $our "ermission. These too are not allowe!. A""lications on the other
han! are assume! to be truste!2 as the$ resi!e on the local har! !isk. These
a""lications ha-e full access to the local har!-!ri-e an! to all network a!!resses. An
a""let woul! be chosen when an a""lication must be !istribute! to a number of clients
who trust2 or !onQt trust the content. The$ allow a local -ersion to resi!e on the ser-er2
so onl$ one co"$ exists2 allowing com"lete -ersion control.
An a""lication on the other han! !oes not contain the inherent restrictions an! so must
be com"letel$ truste! b$ the client. An in!e"en!ent -ersion is !istribute! to each
client so there is not the same !egree of -ersion control as that associate! with a""lets.
There is no o-erhea! in !ownloa!ing the a""lication before running it. ?ou also !o
not ha-e to be connecte! to the 1nternet to run an a""lication. The or!er of execution
of an a""let is main/0 an! then whate-er functionalit$ that is in-ol-e! in main/0
1n an a""let the or!er is init/0 -Z start/0 -Z "aint/0 an! sto"/0 on the !estruction. These
calls ma$ ha""en as$nchronousl$.
An a""lication ma$ be an a""let an! an a""lication. This is create! b$ exten!ing the
A""let class /,a-a.awt.A""let0 an! then a!!ing a main/0 metho! to the A""let class b$
calling the init/0-Zstart/0-Z"aint/0 metho!s in the or!er that the a""let-iewer woul!..
< TOP >
+&
a. 1n <a-a we ha-e "ublic2 "ri-ate an! "rotecte! access s"ecifiers2 but we also ha-e
another access s"ecifier C"ackageC. This is the !efault access s"ecifier an! means
that all states an! metho!s are accessible to all classes within the same "ackage.
There is no "ackage access s"ecifier ke$wor!2 it is sim"l$ the !efault if "ublic2
"ri-ate or "rotecte! are not use!. Access s"ecifiers in <a-a are# "ublic -
accessible e-er$where2 an interface metho! /same as DD0 "ri-ate - accessible
onl$ in the class where it was !efine! /same as DD0 "rotecte! - accessible in the
class where it is !efine!2 the !eri-e! classes an! in the same "ackage /almost the
same as DD with the exce"tion of the "ackage0 C"ackageC - !efault /there is no
< TOP >
"ackage s"ecifier ke$wor!0 an! means that it is accessible b$ an$ class in the
same "ackage. ?ou can equate this to the DD frien!l$ con!ition b$ sa$ing that
all of the classes in the same "ackage /!irector$0 are frien!l$.
b. A <@; "rogram in-ol-es the following ste"s#
1. 1m"ort the necessar$ classes.
A. %oa! the <@; !ri-er.
3. 1!entif$ the !ata source.
F. Allocate a onnection ob,ect.
G. Allocate a Statement ob,ect.
6. Execute a quer$ using the Statement ob,ect.
7. 9etrie-e !ata from the returne! 9esultSet ob,ect.
8. lose the 9esultSet.
L. lose the Statement ob,ect.
10. lose the onnection ob,ect.
< TOP OF THE DOCUMENT >