meljun cortes manual intro information system java applets comp02
TRANSCRIPT
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
1/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES COMP02: Introduction to Inor!ation Sy"te!"
Under t#e I$Carne%ie Modu&e SS'(: Introduction to Inor!ation Sy"te!"
Unit (. T#e )or&d )ide )eb
(.( U"in% t#e )eb
(.(.( Surin% t#e )eb
(.(.2 *our )eb Pa%e"
(.(.+ C&ient", Ser-er", and URL"
(.(. Searc#in% t#e )eb
(.(./ Co!!erce on t#e )eb
2 (.(. So!e Et#ica& Con"ideration"
(.2 )#at1" in t#e )eb
(.2.( )#at1" in a )eb Pa%e
(.2.2 3 4ir"t Loo5 at 6TML
(.2.+ 3 More 'etai&ed Loo5 at 6TML
(.2. Creatin% Lin5" to Ot#er 'ocu!ent"
(.2./ I!a%e" and Mu&ti!edia
(.+ )ritin% )eb Pa%e"
(.+.( Or%ani7in% t#e Proce""
(.+.2 So!e 4ina& T#ou%#t"
College of Computer Studies 1
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
2/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
Unit 2. Introduction to Ja-a
2.( Readin% and Modiyin% a Ja-a Pro%ra!
2.(.( Introduction
2.(.2 Readin% a Ja-a 3pp&et
2.(.+ 6o8 to Run a Ja-a 3pp&et
2.(. 3 Second Loo5 at Ja-a 3pp&et"
2.2 9rap#ic" and ariab&e"
+ 2.2.( T#e 3pp&et, 9rap#ic", and Co&or C&a""e"
2.2.2 Type", ariab&e", and Identiier"
2.+ Ob;ect$Oriented Pro%ra!!in%
2.+.( C&a""e", Ob;ect", and In#eritance
2.+.2 Met#od"
2.+.+ 3 Re-ie8 o OOP
2. Under"tandin% C&a""e"
2..( )id%et":
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
3/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
Unit +. T#e Ja-a Lan%ua%e
+.( Ja-a 4unda!enta&"
+.(.( 3rit#!etic Operator" and E=pre""ion"
+.(.2
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
4/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
UNIT 1. THE WORLD WIDE WEB
Internet
I" a co!puter net8or5 t#at connect" !i&&ion" o co!puter" acro"" a
nu!ber o countrie"
)a" ori%ina&&y concei-ed o by t#e 3d-anced Re"earc# Pro;ect 3%ency
>3RP3? o t#e U.S. %o-ern!ent in t#e (@0".
Net8or5 o net8or5"
World Wide Web >or "the WebA or "#ort?
Reer" to t#at portion o t#e co!puter" on t#e Internet t#at can
co!!unicate 8it# eac# ot#er u"in% a co!puter$net8or5 protoco& ca&&ed
6TTP. 3&& bro8"er" u"e 6TTP to reBue"t and recei-e )eb pa%e" ro!
ot#er co!puter".
Two major areas o !ontrol
(. Na!in% o a "ite on t#e )eb
2. T#e ru&e" o an Internet er#i!e $ro#ider>I$?.
3n I$ i" any one o a nu!ber o co!panie" t#at enab&e" you not on&y to
connect to t#e Internet and "ur t#e )eb but a&"o to pub&i"# )eb pa%e".
College of Computer Studies 4
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
5/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES ear!h en%ine i" a pro%ra! t#at a&&o8" one to "earc# or 5ey8ord" in i&e" at
one or !ore Internet "ite". Popu&ar "earc# en%ine" inc&ude Lyco", E=cite, and
3&tai"ta.
&lient a''li!ation
ReBue"t" inor!ation ro! a "er-er app&ication or a"5" t#e "er-er to
peror! "o!e ta"5 on it" be#a&.
Typica&&y run on &e"" po8eru& co!puter"
er#er a''li!ations
Supp&ie" t#e inor!ation
Typica&&y run on po8eru& co!puter", "ince t#ey need to be ab&e to "er-ice
concurrent reBue"t" ro! a nu!ber o c&ient".
URL>Uniorm Reso(r!e Lo!ator?
Speciie" t#e &ocation o a )eb "ite >or o a pa%e 8it#in t#e "ite?
U"ua&&y be%in" 8it# an e&e!ent t#at identiie" t#e co!!unication protoco&
to be u"ed to acce"" t#e i&e t#e URL identiie".
3&"o 5no8n a" )eb addre""
&omm(ni!ation 'roto!oli" a %enera&&y a%reed upon "et o "tandard" and ru&e"
t#at !ac#ine" o&&o8 8#en t#ey co!!unicate 8it# eac# ot#er.
College of Computer Studies 5
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
6/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
$roto!ols (sed in the Internet
(. #ttp Hyperte=t Tran"er $rotoco&
2. tp )i&e Tran"er $rotoco&
+. %op#er
. ne8"
/. !ai&to
Browsin% denote" t#e acti-ity o o&&o8in% #yper&in5" t#at one encounter" on
"ucceedin% pa%e".
ear!hin%in-o&-e" t#e u"e o a co!puter pro%ra! ca&&ed a search engine.
ear!h en%inei" a %eneric one t#at reer" to a c&a"" o pro%ra!", t#ou%# t#e
!e!ber" o t#i" c&a"" !ay e!p&oy -ery dierent "earc# !ec#ani"!".
Two t*'es o sear!h sites on the Web
Site" t#at eature a "earc# en%ine.
Site" t#at eature a )eb directory.
&ommon web+based Internet !ommer!e ,also -nown as e+!ommer!e
College of Computer Studies 6
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
7/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Internet Ser-ice Pro-ider" >ISP"?
3d-erti"in%
Co!!ercia& Tran"action"
HTML"tand" or Hypertext Markup Language
Lan%(a%ei" a ter! 8e a&& under"tand. 3 &an%ua%e i" a "y"te! o "i%n"
u"ed or co!!unicationD8ritten and ora&. .
/ar-(', in paper pub&i"#in% conte=t", i" t#e proce"" o preparin%
!anu"cript" or type"ettin% by !ar5in% t#e! 8it# direction" about ont
type and "i7e, indentation, "pacin%, and t#e &i5e. In e&ectronic pub&i"#in%,
t#e "a!e princip&e app&ie", on&y in"tead o !ar5" !ade by penci& or pen,
or!attin% direction" are con-eyed by notation ca&&ed Ata%"
Te0t reer" to t#e 8ord" on t#e co!puter "creen t#at 8e are !ar5in%
up. T#e ter! Ate=t or!atA denote" t#e p&ain c#aracter" o t#e 3SCII
>3!erican Standard Code or Inor!ation Interc#an%e? c#aracter "et. 3" it
i", te=t or!at i" t#e "i!p&e"t or!at in 8#ic# to "tore 8ritten !ateria&",
and te=t i&e" contain on&y t#e 0c#aracter" o t#e 3SCII c#aracter "et
H*'er "#ou&d be co!bined 8it# t#e ter! Ate=tA in t#i"
di"cu""ion. 6yperte=t a" a concept de-e&oped ro! anne-ar
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
8/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 8a" ne-er bui&t, t#e notion o creatin% &in5ed, non&inear "tructure" o
inor!ation endured. 3 #yperte=t docu!ent i" Buite dierent ro! a
traditiona& printed docu!ent. In a printed docu!ent, t#e order in 8#ic#
inor!ation i" pre"ented i" &inearDt#at i", "entence ( precede" "entence
2, 8#ic# in turn precede" "entence +, and "o it %oe" on up t#e &e-e&" "o
t#at c#apter ( precede" c#apter 2, 8#ic# precede" c#apter +, and "o on.
6o8e-er, a #yperte=t docu!ent contain" &in5", and by c&ic5in% a &in5, a
u"er i" ta5en to a dierent pa%e or dierent part o t#e "a!e pa%e. I t#e
u"er t#en c&ic5" ot#er &in5", #e or "#e i" ta5en to anot#er pa%e or part o
t#e "a!e pa%e. T#u", t#e u"er can o&&o8 a non&inear pat# t#rou%# t#e
docu!ent or docu!ent", u"in% t#e &in5" pro-ided by t#e aut#or and
encounterin% t#e inor!ation t#ere in an order t#at depend" on t#e
c#oice" t#at bot# t#ey and t#e 8riter #a-e !ade.
HT/L i" a or!attin% &an%ua%e u"e in order to create a 8eb pa%e
Ta%s "y!bo&" t#at te&&" t#e bro8"er #o8 to di"p&ay te=t. Ta%" "#ou&d be
enc&o"ed in a pair o an%&e brac5et".
The /ain &om'onents o an HT/L
3n 6TML docu!ent "#ou&d be%in and end 8it# t#i" ta% pair:
F6TMLG
FH6TMLG
College of Computer Studies 8
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
9/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES I!!ediate&y ater t#e F6TMLG ta% "#ou&d be t#e o&&o8in% ta% pair to denote t#e
docu!ent #eader:
F6E3'G
FH6E3'G
In bet8een t#e F6E3'G FH6E3'G pair "#ou&d be t#e FTITLEG FHTITLEG ta%".
T#e tit&e o t#e docu!ent i" "peciied 8it#in t#e!. T#i" tit&e 8i&& be di"p&ayed in
t#e tit&e bar o t#e )eb pa%eDor e=a!p&e,
F6TMLGF6E3'GFTITLEGMy 4ir"t )eb Pa%eFHTITLEGFH6E3'GFH6TMLG
Ne=t co!e t#e be%innin% and endin% AF
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
10/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES FIG FHIG Italics
FTTG FHTTG Mono"pace>i=ed 8idt#?
FUG FHUG Under&ine
FSU
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
11/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES T#e"e are u"ed in a !anner "i!i&ar to t#e 8ay p#y"ica& ta%" are u"ed, but t#e
bro8"er decide" #o8 to di"p&ay t#e te=t. 4or e=a!p&e, t#e o&&o8in% 6TML te=t:
T#e o&&o8in% i" FSTRON9G "tron%&y e!p#a"i7ed te=t
FHSTRON9G.
!i%#t &oo5 &i5e t#i" in Net"cape
T#e o&&o8in% i" stron%l* em'hasied te0t.
and &i5e t#i" in Internet E=p&orer
T#e o&&o8in% i" "tron%&y e!p#a"i7ed te=t
F#rG $ 6ori7onta& Line
FbrG $ Line brea5
FpreGFHpreG $ Preor!atted Te=t
FontGFHontG $ u"ed to c#an%e t#e 4ont ace, "i7e and co&or.
Two t*'es o List
(. Unordered &i"t Fu&GFHu&G
2. Ordered &i"t Fo&GFHo&G
&reatin% a Table in HT/L Do!(ment
Ftab&eGFHtab&eG $ create" a tab&e
Ft#GFHt#G $ Tab&e 6eadin%
FtrGFHtrG $ Tab&e Ro8
College of Computer Studies 11
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
12/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES FtdGFHtdG $ Tab&e 'ata
Two !ommon ima%e iles
T#e .;p% e=ten"ion i" u"ed or 2$E3>Joint P#oto%rap#ic E=pert" 9roup? i&e".
JPE9 i&e" are co!pre""edD8#ic# !ean" t#at t#e data t#ey contain i" "tored in
a !anner t#at reduce" t#e a!ount o inor!ation needed to reproduce a picture.
I t#e data 8a" not co!pre""ed, JPE9 i&e" 8ou&d be !uc# &ar%er and 8ou&d ta5e
!uc# &on%er to tran"er o-er t#e )ebD8#ic# i" 8#at !u"t #appen or t#e
i!a%e" to be di"p&ayed by a bro8"er. 3&"o, !o"t bro8"er" "tore i!a%e i&e" on
t#e u"er1" #ard dri-e, "o t#at i t#e u"er 8ant" to -ie8 an i!a%e a%ain, t#e i!a%e
can be di"p&ayed !ore Buic5&y by bein% read ro! t#e dri-e rat#er t#an
tran"erred a%ain o-er t#e )eb. T#i" i" an e=a!p&e o a proce"" 5no8n a"
caching, and cac#in% pro-ide" yet anot#er rea"on or co!pre""in% i!a%e i&e"D
t#e &ar%er an i!a%e i&e i", t#e !ore roo! it 8i&& ta5e up on a #ard dri-e.
T#e .%i e=ten"ion i" u"ed or 3I) >9rap#ic" Interc#an%e 4or!at? i&e". Li5e
JPE9 i&e", t#e data in 9I4 i&e" i" co!pre""ed. T#e !a;or dierence bet8een
t#e t8o or!at" &ie" in t#e 8ay co!pre""ion i" ac#ie-ed. T#e data o 9I4 i&e" i"
co!pre""ed in "uc# a 8ay t#at none o t#e ori%ina& i!a%e inor!ation i" &o"t.
6o8e-er, 9I4 i&e" are &i!ited in t#e ran%e o co&or" t#ey "upport. JPE9 i&e", on
t#e ot#er #and, "upport !ore co&or", but "o!e o t#e ori%ina& i!a%e inor!ation
i" &o"t durin% co!pre""ionDa act t#at can #a-e an i!pact on i!a%e
Bua&ity. T#ereore, !o"t )eb !a"ter" reco!!end u"in% JPE9 co!pre""ion or
p#oto%rap#" and 9I4 co!pre""ion or ot#er 5ind" o %rap#ic", "uc# a" icon" and
dra8in%". It1" a&"o i!portant to note #ere t#at you do not need to 5no8 a &ot
College of Computer Studies 12
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
13/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES about co!pre""in% or deco!pre""in% to u"e o eit#er o t#e"e or!at": !o"t
%rap#ic" pac5a%e" and bro8"er" #and&e t#e"e proce""e" auto!atica&&y. 4or
e=a!p&e, i you are 8or5in% on a p#oto in P#otoS#op, a&& you #a-e to do i" "a-e
t#e p#oto a" JPE9 i&e >an option a-ai&ab&e t#rou%# t#e Sa-e 3" dia&o%ue?, and
P#otoS#op co!pre""e" it in JPE9 or!at auto!atica&&y.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
14/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES No8 Ja-a i" in it" ir"t re&ea"e a" a de-e&op!ent en-iron!ent, and a&ready it i"
be%innin% to in&uence t#e direction o co!putin% and t#e Internet. T#e Ja-a
pro%ra!!in% &an%ua%e i" bein% re&ea"ed ree on t#e Internet, and Sun i"
&icen"in% t#e u&& i!p&e!entation o Ja-a and it" co!ponent" to a -ariety o
Internet "ot8are -endor" in t#e #ope" o creatin% a ne8 )eb pro%ra!!in%
"tandard.
5bo(t 2a#a
Ja-a i" a -ery ne8 pro%ra!!in% &an%ua%e and !a5e" po""ib&e a -ery ne8
pro%ra!!in% rea&ity. 3" a co!!ercia& product, it i" on&y a e8 year" o&d, but e8
ne8 product" #a-e captured t#e attention o t#e inor!ation "cience, co!puter
"cience, "ot8are de-e&op!ent, and pro%ra!!in% 8or&d" t#e 8ay t#i" ne8
&an%ua%e #a". One o Ja-aK" !o"t attracti-e eature" i" t#at it a&&o8" you to 8rite
a Ja-a pro%ra! on an Inte& co!puter and t#en run it on a Macinto"# 8it#out
doin% anyt#in% "pecia&, and t#at tru&y isa ne8 pro%ra!!in% rea&ity.
In it" "ynta=, Ja-a i" -ery !uc# &i5e anot#er popu&ar pro%ra!!in% &an%ua%e: C
. Un&i5e C, #o8e-er, Ja-a eature" a nu!ber o Abui&din% b&oc5"A or creatin%
3ra'hi!al User Intera!es >or 3UIs, pronounced gooey?, !a5in% 9UI" -ery
ea"y to i!p&e!ent in Ja-a. 6o8e-er, in order to bui&d !eanin%u& 9UI", a
pro%ra!!er need" to #a-e a %ra"p o "o!e i!portant pro%ra!!in% i""ue" and
"o!e air&y co!p&e= data "tructure", and i you are #opin% to acBuire t#at 5ind o
"5i&&, you 8i&& need to ta5e ot#er cour"e" in addition to t#i" one.
College of Computer Studies 14
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
15/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
Wh* 2a#a
)e are %oin% to u"e Ja-a to e=p&ore ba"ic pro%ra!!in% concept"D"uc# a"
"eBuentia& and iterati-e e=ecution, "i!p&e and co!p&e= -ariab&e", and pro%ra!
p&annin%. )e 8i&& a&"o &oo5 at "o!e a"pect" o t#e ob;ect$oriented pro%ra!!in%
paradi%!, and 8#i&e t#ere are t#ree !ain ob;ect$oriented pro%ra!!in%
&an%ua%e"DC, Ja-a, and S!a&&ta&5D8e 8i&& 8or5 on&y 8it# Ja-a. 4ir"t o a&&,
S!a&&ta&5 i" not u"ed by a" !any pro%ra!!er" a" C or Ja-a are, and 8e #a-e
c#o"en Ja-a o-er C and ot#er pro%ra!!in% &an%ua%e" or a nu!ber o %ood
rea"on":
Ja-a i" 'ortableDt#at i", a" !entioned abo-e, a Ja-a pro%ra! 8ritten or
one type o !ac#ine >"ay a PC? can ea"i&y 8or5 on anot#er !ac#ine >"ay
a Macinto"#?
Ja-a app&et" can run in"ide a bro8"er. >In t#i" cour"e, 8e 8i&& 8or5
pri!ari&y 8it# app&et".?
Ja-a i" u"ed 8ide&y or 8ritin% Internet app&ication".
College of Computer Studies 15
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
16/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
2a#a 5''li!ations #s. 5''lets
Ja-a a&&o8" u" to 8rite t8o dierent type" o pro%ra!": app&ication" and app&et".
2a#a a''li!ationsare co!puter pro%ra!" t#at run &i5e a&!o"t any ot#er
co!puter pro%ra!. T#ey can be -ery co!p&e=, &i5e t#e notepad
app&ication t#at !any o you u"ed to bui&d your 6TML pa%e", or t#ey can
be -ery "i!p&e. Ja-a app&ication" are con"idered A"tanda&one"AD8#ic#
!ean" t#at t#ey can run on a co!puter 8it#out t#e aid o anot#er
app&ication, "uc# a" a )eb bro8"er. )ord proce""or" and "pread"#eet"
a" 8e&& a" )eb bro8"er" are a&& app&ication".
2a#a a''letsare pro%ra!" t#at are e=ecuted ro! 8it#in a )eb bro8"er.
Put into a )eb pa%e, a Ja-a app&et can a&&o8 u"er" to interact 8it# t#e
pa%e in intere"tin% 8ay". 4or e=a!p&e, i your a!i&y o8ned a carpet "tore,
you !i%#t 8ant to create a "i!p&e ca&cu&ator t#at 8ou&d a&&o8 u"er" to
enter t#e "i7e o a roo! and type o carpet t#ey 8ant, and your carpet
ca&cu&ator 8ou&d te&& t#e! t#e nu!ber o "Buare yard" o carpet t#ey need
and t#e appro=i!ate co"t o t#e carpet and paddin%. )#i&e your )eb pa%e
cou&d urni"# t#i" inor!ation in, "ay, a tab&e or c#art, potentia& cu"to!er"
!i%#t preer in"tead to u"e a ca&cu&ator app&et, "i!p&y becau"e it1" ea"ier
to u"e.
College of Computer Studies 16
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
17/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 2a#a 5''li!ations #s. 5''lets
Ja-a a&&o8" u" to 8rite t8o dierent type" o pro%ra!": app&ication" and app&et".
2a#a a''li!ationsare co!puter pro%ra!" t#at run &i5e a&!o"t any ot#er
co!puter pro%ra!. T#ey can be -ery co!p&e=, &i5e t#e notepad
app&ication t#at !any o you u"ed to bui&d your 6TML pa%e", or t#ey can
be -ery "i!p&e. Ja-a app&ication" are con"idered A"tanda&one"AD8#ic#
!ean" t#at t#ey can run on a co!puter 8it#out t#e aid o anot#er
app&ication, "uc# a" a )eb bro8"er. )ord proce""or" and "pread"#eet"
a" 8e&& a" )eb bro8"er" are a&& app&ication".
2a#a a''letsare pro%ra!" t#at are e=ecuted ro! 8it#in a )eb bro8"er.
Put into a )eb pa%e, a Ja-a app&et can a&&o8 u"er" to interact 8it# t#e
pa%e in intere"tin% 8ay". 4or e=a!p&e, i your a!i&y o8ned a carpet "tore,
you !i%#t 8ant to create a "i!p&e ca&cu&ator t#at 8ou&d a&&o8 u"er" to
enter t#e "i7e o a roo! and type o carpet t#ey 8ant, and your carpet
ca&cu&ator 8ou&d te&& t#e! t#e nu!ber o "Buare yard" o carpet t#ey need
and t#e appro=i!ate co"t o t#e carpet and paddin%. )#i&e your )eb pa%e
cou&d urni"# t#i" inor!ation in, "ay, a tab&e or c#art, potentia& cu"to!er"
!i%#t preer in"tead to u"e a ca&cu&ator app&et, "i!p&y becau"e it1" ea"ier
to u"e.
College of Computer Studies 17
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
18/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 9rap#ic" % ?
%.dra8Strin%>A)e&co!e to
J33A,+0, +0?
%.dra8Rect>20, /, (0, /0?
H
N3ME:
SECTION:
3SSI9NMENT:
H
pub&ic c&a"" 6e&&o
pub&ic "tatic -oid !ain>Strin% ar%"Q?
Sy"te!.out.print&n>A)e&co!e to J33A?
T#ere are a coup&e o !a;or dierence" bet8een t#e app&ication and app&et
"#o8n abo-e:
)#i&e t#e app&et e=ecute" %.dra8Strin% and %.dra8Rect, t#e app&ication e=ecute"
Sy"te!.out.print&n. T#i" i" becau"e t#e app&et run" in a %rap#ica& en-iron!entD
t#at i", in a bro8"er. 4or t#at rea"on, it can dra8 %rap#ica& e&e!ent" in t#e
College of Computer Studies 18
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
19/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES bro8"er >"uc# a" &ine", circ&e", rectan%&e", "trin%", etc.?. In t#i" e=a!p&e, it dra8"
t#e "trin% Welcome to Javaand a rectan%&e around t#e "trin%. 6o8e-er, t#e Ja-a
app&ication doe" not run in a %rap#ica& en-iron!ent. It #a" to be run in a 'OS
8indo8 >a&"o ca&&ed a Acon"o&e 8indo8A? on a PC. T#e Sy"te!.out.print&n
in"truct" t#e co!puter to print t#e te=t in"ide t#e parent#e"e" to t#e "y"te!1"
output de-ice. T#u", t#e !e""a%e A)e&co!e to Ja-aA i" printed to t#e "y"te!1"
output de-ice$$in t#i" ca"e, t#e 'OS 8indo8 >con"o&e 8indo8?.
T#ere are a coup&e o im'ort"tate!ent" in t#e app&et, 8#i&e t#ere are none in
t#e app&ication. 3n im'ort"tate!ent !a5e" pre-iou"&y 8ritten code a-ai&ab&e to
your pro%ra!. In t#i" ca"e, t#e im'ort"tate!ent" !a5e a-ai&ab&e t#e code t#at
i" needed to dra8 a "trin% in a bro8"er. In contra"t, t#e "i!p&e Ja-a app&ication
dra8" no %rap#ica& e&e!ent", and #ence it doe" not need t#e i!port "tate!ent".
T#e Ja-a app&ication inc&ude" a !et#od ca&&ed main, . T#e main, !et#od i"
!andatory or a&& Ja-a app&ication". )#en you run t#e Ja-a app&ication, t#e
!ac#ine &oo5" or it" main, !et#od, and i it cannot ind it, t#e !ac#ine 8i&&
Aco!p&ain.A 3pp&et" doe" not need to inc&ude a main, !et#odDt#e unctiona&ity
nor!a&&y pro-ided by t#e main,!et#od i" pro-ided by t#e bro8"er. Re!e!ber
t#at a bro8"er i" an app&ication and t#at app&et" run in"ide bro8"er".
2a#a 5''let in Detail
T#e tab&e be&o8 brea5" our e=a!p&e app&et into part" and e=p&ain" it" i!portant
concept".
The a!t(al !ode What it reall* means7
H T#e "&a"#$a"teri"5 co!bination > H ? "i%na&" t#e
College of Computer Studies 19
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
20/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 3 Si!p&e Ja-a 3pp&et
N3ME:
SECTION:
3SSI9NMENT:
be%innin% o t#e !omment. 4ro! a co!puter1"
per"pecti-e, a !ommenti" a "eBuence o c#aracter"
t#at it 8i&& i%nore. 4ro! t#e per"pecti-e o t#e
co!puter1" #u!an u"er", a !omment u"ua&&y
con"i"t" o "tate!ent" and inor!ation intended or
"o!e purpo"e, "uc# a" !a5in% t#e pro%ra!1" code
!ore under"tandab&e.
H T#e a"teri"5$"&a"# co!bination > H ? "i%na&" t#e end
o t#e !omment.
i!port ;a-a.a8t.
i!port ;a-a.app&et.3pp&et
T#e"e im'ort "tate!ent" !a5e code ro! t#e
'a!-a%esjava.awt and java.applet a-ai&ab&e
to t#e app&et.
pub&ic c&a"" 6e&&o e=tend"
3pp&et
E-eryt#in% in Ja-a i" 8ritten a" a class. In t#i"
e=a!p&e, t#e c&a"" i" na!ed Hello. T#i" &ine initiate"
t#e deinition o t#e 6e&&o c&a"".
T#i" open brace !ar5" t#e be%innin% o t#e bodyo
t#e c&a"" 6e&&o.
pub&ic -oid
paint>9rap#ic" %?
T#i" &ine initiate" t#e deinition o a !et#od ca&&ed
'aint, .
T#i" open brace !ar5" t#e be%innin% o t#e body o
t#e 'aint, !et#od.
%.dra8Strin%>A)e&co!e
to J33A,+0, +0?
T#i" ca&&" t#e drawtrin%, !et#od, 8#ic# i" part o
t#e 3ra'hi!s c&a"". T#e 3ra'hi!s c&a"" >and,
College of Computer Studies 20
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
21/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES t#ereore, t#e drawtrin%, !et#od? i" deined in
one o t#e i!ported &ibrarie".
%.dra8Rect>20, /, (0,
/0?
T#i" ca&&" t#e drawRe!t, !et#od o t#e 3ra'hi!s
c&a"".
T#i" c&o"e brace !ar5" t#e end o t#e body and
deinition o t#e !et#od 'aint, .
T#i" c&o"e brace !ar5" t#e end o t#e body and
deinition o t#e c&a"" 6e&&o.
T#e "i!p&e Ja-a app&et ana&y7ed abo-e i&&u"trate" 8e&& t#e "tructure o a Ja-a
source file.3 typica& Ja-a "ource i&e >a i&e 8it# t#e e=ten"ion .java? contain" a
deinition o one c&a"", and t#e e=a!p&e app&et abo-e contain" t#e deinition o
c&a"" na!ed Hello.Ja-a reBuire" t#at t#e na!e o t#e c&a"" a" deined in a
"ource i&e be t#e ba"e part o t#e "ource i&e1" i&ena!e >t#at i", part o t#e na!e
beore t#e e=ten"ion?. In t#e abo-e app&et, t#e c&a"" na!e i" Hello t#ereore, t#e
"ource i&e1" i&ena!e !u"t be Hello.java. 3 c&a"" deinition !ay contain "e-era&
method deinitionsand !ay >but not nece""ari&y? be preceded by one or !ore
im'ort"tate!ent" t#at %i-e t#e app&et acce"" to t#e code in e=terna& &ibrarie".
T#e c&a"" deinition in t#e abo-e e=a!p&e contain" a deinition o a !et#od
na!ed 'aint, and i" preceded by t8o im'ort "tate!ent". In t#e o&&o8in%
"ection", 8e 8i&& ana&y7e t#e"e c#ie co!ponent" o a Ja-a pro%ra! > im'ort
"tate!ent", c&a""e", and !et#od"? in !ore detai&.
College of Computer Studies 21
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
22/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Im'ort tatements
Ja-a im'ort"tate!ent" Ai!portA >and, #ence, %i-e our app&et" acce"" to? Ja-a
!ode librariest#at contain code a&ready 8ritten to do ta"5" 8e !i%#t 8ant our
app&et" to do. O cour"e, t#e capacity to i!port code &ibrarie" i" not uniBue to
Ja-a: !any "eriou" pro%ra!!in% &an%ua%e" #a-e t#at capacity. Code &ibrarie"
are "i!p&y co&&ection" o pre-iou"&y 8ritten c&a""e", !et#od", and unction" t#at
are ready to be e=ecuted and can be u"ed in ot#er pro%ra!". T#e c&a""e",
!et#od", and unction" t#at &ibrarie" contain peror! unction" t#at are needed
by a -ariety o pro%ra!". Co&&ectin% t#e"e e&e!ent" into &ibrarie" pro-ide"
de-e&oper" 8it# a "et o Abui&din% b&oc5"A t#at are ready to u"e.
Ja-a code &ibrarie" con"i"t o 'a!-a%es. Eac# pac5a%e con"i"t" o a nu!ber o
pre-iou"&y 8ritten c&a""e". 4or e=a!p&e, java.at i" a pac5a%e
java.applet.!ppleti" a c&a"" in t#e pac5a%ejava.applet. 3" 8e "ee, a na!e o a
pac5a%e or a c&a"" in Ja-a i" a "et o 8ord" de&i!ited by dot". To u"e t#e
!et#od" o a pre$bui&t c&a"" in our pro%ra! 8e need to importt#i" c&a"" u"in% t#e
im'ort "tate!ent. )e cou&d c#oo"e to i!port a particu&ar c&a"" or a 8#o&e
pac5a%e 8it# a&& it" c&a""e". 3&"o, a&& im'ort"tate!ent" are u"ua&&y "peciied at
t#e be%innin% o a pro%ra!.
No8, &et1" ana&y7e t#e t8o im'ort"tate!ent" in t#e app&et abo-e.
im'ort ja#a.a''let.5''let8
T#i" "tate!ent i!port" t#e c&a"" 5''let t#at i" contained in t#e pac5a%e
java.applet. T#e 5''let c&a"" contain" t#e code t#at a&&o8" u" to run our
pro%ra! a" an app&et in a )eb bro8"er.
College of Computer Studies 22
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
23/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES im'ort ja#a.awt.98
T#i" "tate!ent i!port" all c&a""e" in t#e pac5a%e java.at >by t#e 8ay, at
"tand" or 3b"tract )indo8in% Too&5it?. T#i" pac5a%e contain" pro%ra!" t#at can
bui&d t#e 8indo8 and do t#e type o dra8in%" 8e need to do. T#e a"teri"5 > ?
t#at o&&o8" t#e "tate!ent1" &a"t period > . ? i" a A8i&dcard c#aracterA and te&&"
Ja-a to i!port allc&a""e" in t#e pac5a%e.
It turn" out t#at t#e &an%ua%e Ja-a A5no8"A -ery &itt&e. On it" o8n, it can do a &itt&e
arit#!etic, a&&ocate !e!ory or data, "tore data in t#e a&&ocated !e!ory, !a5e
"i!p&e true$or$a&"e te"t", and repeat t#in%". T#at i" not !uc#Dbut t#at i" a&& a
pro%ra!!in% &an%ua%e rea&&y need" to be ab&e to do. )#ene-er 8e 8rite a
pro%ra!, 8e need to decide 8#ic# part" o t#e pro%ra! 8e "#ou&d 8rite ro!
"cratc# our"e&-e" and 8#ic# part" can !a5e u"e o code 8ritten by ot#er" and
!ade a-ai&ab&e in &ibrarie". In %enera&, 8e 8ant to !a5e a" !uc# u"e o t#e code
a-ai&ab&e in &ibrarie" a" po""ib&e. T#i" i" "o!e8#at ana&o%ou" to decidin%
8#et#er to bui&d a bui&din% out o bric5" !ade by "o!eone e&"e or out o bric5"
8e !a5e our"e&-e" ro! "cratc#. 3&"o, be a8are t#at t#e &ibrary "tructure or
Ja-a i" -ery &ar%e and i" "ti&& e-o&-in%, and in t#i" cour"e, 8e 8i&& u"e on&y a e8
piece" o a e8 &ibrarie". T#ereore, i you 8ant to be a Ja-a pro%ra!!er, you 8i&&
need to &earn a &ot !ore about t#e -ariou" &ibrarie" t#an 8e 8i&& co-er in t#i"
cour"e.
&lasses: Obje!ts: and /ethods
E-eryt#in% in an ob;ect$oriented &an%ua%e &i5e Ja-a i" 8ritten a" a c&a"". &lass
reer" to t#e "tructure and be#a-ior o a cate%ory o ob;ect". T#e structure
College of Computer Studies 23
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
24/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES specifies the datat#at i" contained in t#e cate%ory o ob;ect", 8#i&e t#e behavior
specifies the actions t#at can be peror!ed by t#e cate%ory o ob;ect". T#e
be#a-ior co&&ecti-e&y reer" to t#e "et o methodst#at t#e c&a"" can e=ecute.
3n obje!ti" an in"tance o a c&a"". 3n ob;ect i" a "e&$contained entity t#at #a"
bot# t#e data and t#e !ean" to act on t#e data. T#e e=a!p&e app&et and
app&ication abo-e eac# #a-e one !et#od on&y >'aint, in t#e app&et and main,
in t#e app&ication?, and t#ere are no "tructure" or data in eit#er o t#e"eDat &ea"t
no data t#at 8e can "ee at t#e pre"ent.
No8 it i" ti!e to &oo5 at "o!e o t#e ru&e" or 8ritin% Ja-a c&a""e". T#ere i" an is"
an"instance"ofrelationshipbet8een an ob;ect and it" c&a"". 3" a "#ort or!, an
i"$an$in"tance$o re&ation"#ip i" a&"o ca&&ed an is"arelationship8#en t#ere i" no
po""ibi&ity o !i"under"tandin%. 3" 8e !entioned ear&ier, a c&a"" denote" a
cate%ory o ob;ect"D;u"t a" in En%&i"# t#e ter! Elephant reer" to a cate%ory o
t#in%" in t#e 8or&d: e&ep#ant". )#en 8e ta&5 about Elephant, 8e ta&5 in
%enera&itie" and not about any one e&ep#ant in particu&ar. Si!i&ar&y, in
pro%ra!!in%, a c&a"" reer" to a category of objects and not to a particu&ar
ob;ect. 4or e=a!p&e, a c&a"" Elephantde"cribe" a cate%ory o elephantobjects.
3n ob;ect i"$an$in"tance$o a c&a"". 3" a con-ention, 8#en 8e reer to a c&a"",
8e 8i&& upperca"e t#e ir"t &etter o t#e c&a""1" na!e, and 8#en 8e reer to an
ob;ect, 8e 8i&& &o8erca"e t#e entire ob;ect na!e.
In t#e rea& 8or&d, e&ep#ant" haveattribute" "uc# a" #ei%#t, 8ei%#t, etc. T#ey a&"o
dot#in%" "uc# a" 8a&5, drin5, etc. Si!i&ar&y, in pro%ra!!in% you can deine a "et
College of Computer Studies 24
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
25/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES o attribute" t#at a c&a"" can #a-e and a "et o acti-itie" >ca&&ed !et#od"? t#at it
can peror!.
&lass Deinition
C&a""e" are t#e !a;or or%ani7ationa& b&oc5 or Ja-a pro%ra!". 3 Ja-a pro%ra!
con"i"t" o one or !ore classes. )it#in t#i" cour"e, eac# c&a"" i" deined in a
"eparate "ource i&e.
T#e deinition o a c&a"" "peciie" 8#at t#e c&a"" hasand 8#at it can do. )it#in a
c&a"", 8e can i!port and u"e ot#er c&a""e". I you &oo5 at t#e ir"t &ine o t#e
deinition o t#e c&a"" 6e&&o, you 8i&& "ee t#e o&&o8in%:
pub&ic c&a"" 6e&&o e=tend" 3pp&et
LetK" &oo5 at eac# o t#i" &ine1" e&e!ent"Dbe%innin% 8it#
'(bli!
)e can "peciy a Ja-a c&a"" a"public,private, orprotectedu"in% t#e 5ey8ord"
'(bli!, 'ri#ate, and 'rote!ted #o8e-er, or no8 8e 8i&& 8or5 on&y 8it# c&a""e"
t#at de"i%nated public.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
26/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Hello
Hello i" t#e na!e o t#e c&a"". 3" 8e !entioned ear&ier, c&a"" na!e" by
con-ention "#ou&d be%in 8it# a capita& &etterDand, a" you "ee, t#i" e=a!p&e
o&&o8" t#at con-ention. Note: or rea"on" 8e 8on1t %o into no8, you !u"t not
c#an%e t#i" na!e. )e 8i&& &oo5 at t#e"e rea"on" &ater and "#o8 you 8#at you
!u"t do i you 8ant to c#an%e t#i" na!eDbut or no8, p&ea"e rea&i7e t#at i you
c#an%e t#i" na!e, your pro%ra! 8i&& probab&y not run.
e0tends 5''let
Read t#e p#ra"e extends !ppletto !ean t#at t#e c&a"" bein% deined Ade"cend"
ro!,A Ain#erit" ro!,Aor Ai" a c#i&d oA t#e c&a"" 5''let. T#at i", our c&a"" 6e&&o i"
a !hildor a s(b!lasso a c&a"" na!ed 5''let. In turn, c&a"" 5''leti" ca&&ed
t#e s('er!lassor t#e c&a"" 6e&&o. T#i" !ean" t#at 6e&&o can do anyt#in% t#at
t#e parent c&a"" 5''letcan do. T#e parent in t#i" ca"e A5no8"A #o8 to e=ecute
it"e& 8it#in a )eb pa%e. Since 8e #a-e dec&ared our c&a"" 6e&&o a" a c#i&d o
5''let, 8e donKt #a-e to 8rite a&& o t#e needed Ja-a in"truction" reBuired to run
t#e pro%ra! in a )eb pa%e. C&a"" 6e&&o inheritedt#i" abi&ity ro! it" parent c&a""
5''let. In Ja-a, t#e !ec#ani"! by 8#ic# a c#i&d c&a"" acBuire" unctiona&ity
deined in a parent c&a"" i" ca&&ed inheritan!e. T#ere are ot#er !et#od" in c&a""
5''lett#at c&a"" 6e&&o can in#erit and u"e. So!e o t#e"e !et#od" are init, ,
start, , sto', , and ('date, . Notice t#at our c&a"" 6e&&o #a" it" o8n 'aint,
!et#od. T#i" i" ineDJa-a 8i&& u"e our deinition o paint and i%nore t#e deinition
o 'aint, in t#e c&a"" 5''let. I 8e &ea-e out a deinition o a !et#od, Ja-a
auto!atica&&y re&ie" on t#e parent1" deinition.
College of Computer Studies 26
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
27/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES T#e o&&o8in% i" t#e body o t#e 6e&&o c&a"":
pub&ic -oid paint> 9rap#ic" % ?
%.dra8Strin%>A)e&co!e to J33A,+0, +0?
%.dra8Rect>20, /, (0, /0?
T#i" c&a"" body i" bounded by or de&i!ited by a pair o brace". T#e &a"t c#aracter
o t#e c&a"" !u"t a&8ay" be a c&o"in% brace > ?. 3&& o t#e Ja-a code ound 8it#in
t#e"e brace de&i!iter" i" part o t#e c&a"".
Met#od" and Met#od 'einition"
T#ere are t8o !ain co!ponent" o a Ja-a c&a"":
(. data >a&"o ca&&ed t#e structure?
2. !et#od" >a&"o ca&&ed t#e behavior?
3&& o t#e "tructure >data? and be#a-ior >!et#od"? o a c&a"" #a-e to be deined in
t#e body o t#e c&a"". T#at "aid, a" you !ay #a-e noticed, t#ere i" no data "tored
in t#e 6e&&o e=a!p&e c&a"". 6o8e-er, t#ere 8i&& be data in "o!e o our uture
pro%ra!", but t#at 8i&& #a-e to 8ait or a 8#i&e.
In Ja-a, methodsare %roup" o in"truction" t#at #a-e na!e". Met#od" enab&e
our pro%ra!" to do t#in%". Note t#at ot#er &an%ua%e" u"e ot#er ter!" to reer to
8#at in Ja-a 8e ca&& !et#od". In 4ortran, t#e ter! i" subroutine in Pa"ca&, it i"
procedure in C, it i" function and in C, it i" member function.
College of Computer Studies 27
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
28/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES In order to #a-e t#e co!puter e=ecute t#e in"truction" in t#e body o one o our
!et#od", t#e !et#od !u"t be in#o-ed or !alled. )e #a-e a -ery intere"tin%
"ituation 8#en 8e 8rite app&et": "o!e o our !et#od" are ca&&ed or u"
automatically8#en 8e run t#e app&etDand ot#er" are not. 4or e=a!p&e, t#e
'aint, !et#od i" ca&&ed auto!atica&&y 8#en 8e "tart t#e app&et and i" ca&&ed
anyti!e it i" needed.
In"ide t#e body o c&a"" 6e&&o, t#e ir"t t#in% 8e "ee i" a deinition o a !et#od
na!ed 'aint, . T#e !et#od deinition con"i"t" o t#e !et#od1" name, o&&o8ed
by a "et o parent#e"e". So!eti!e" t#e"e parent#e"e" 8i&& contain 'arameters.
3 para!eter i" a piece o data t#at i" pro-ided to t#e !et#od 8#en t#at !et#od
i" ca&&ed. 3ter t#e parent#e"e" i" t#e bod*, enc&o"ed in brace".
Let1" di""ect t#i" !et#od:
'(bli! #oid 'aint, 3ra'hi!s %
T#e 5ey8ord '(bli!te&&" Ja-a t#at t#i" !et#od can be u"ed by ot#er c&a""e"
out"ide o t#e c&a"" 6e&&o. Re!e!ber t#at "o!e8#ere, "o!e#o8, our 'aint,
!et#od i" ca&&ed or u" auto!atica&&yD"o our !et#od 'aint, mustbe deined a"
pub&ic&y acce""ib&eDot#er8i"e our app&et 8i&& not be ab&e to run. 4or no8, a&& o
our !et#od" 8i&& be deined a" pub&ic.
T#e 5ey8ord #oid!ean" t#at a !et#od doe" not return a -a&ue. Met#od" can do
t#ree t#in%":
T#ey can dra8.
T#ey can co!pute and return -a&ue", "uc# a" an a-era%e o a &i"t o nu!ber".
T#ey can do bot# o t#e abo-e.
College of Computer Studies 28
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
29/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 4or no8 8e 8i&& u"e on&y #oid!et#od"Dt#e"e are !et#od" t#at do t#in%" but do
not return -a&ue". 4o&&o8in% t#e 5ey8ord #oidi" t#e !et#od na!e: 'aint, .
,3ra'hi!s %
4o&&o8in% t#e !et#od na!e in our e=a!p&e i" a list of parametersenc&o"ed in a
pair o parent#e"e". T#i" i" t#e &i"t o re"ource" t#e !et#od need" to do it" ;ob.
So!e !et#od", #o8e-er, need no para!eter" at a&&, and, t#ereore, eature a "et
o e!pty parent#e"e"Da !et#od deinition !u"t #a-e t#e parent#e"e", e-en i
t#e parent#e"e" are e!pty. 6o8e-er, in our e=a!p&e, t#e parent#e"e" contain
one para!eter: g.
T#e #9rap#ic"A in"ide t#e parent#e"e" i" t#e na!e o a c&a"" deined in one o
t#e i!ported &ibrarie" >t#e act t#at it i" capita&i7e" c&ue" you in to t#e act t#at it i"
a c&a"", "ince t#e con-ention i" to capita&i7e c&a"" na!e"?.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
30/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES pen to do it. T#e &oanin% o t#e pen i" -ery "i!i&ar to puttin% A9rap#ic" %A in t#e
para!eter &i"t.
T#e body o t#e !et#od i" de&i!ited by a pair o brace", ;u"t a" t#e body o t#e
c&a"" i". E-eryt#in% 8it#in t#i" "et o brace" i" part o t#e 'aint, !et#od. *ou
can deine !et#od" 8it#in c&a""e", but you cannot deine c&a""e" 8it#in
!et#od", and you cannot deine !et#od" 8it#in !et#od".
In"ide o t#e body o 'aint, t#ere are t8o"tate!ent". T#e ir"t "tate!ent dra8"
a "trin% A)e&co!e to J33A at t#e &ocation =:+0, y:+0:
%.dra8Strin%>A)e&co!e to J33A, +0, +0?
T#e ne=t "tate!ent %i-en be&o8 dra8" a rectan%&e (0 pi=e&" in &en%t# and /0
pi=e&" #i%# at t#e &ocation =:20, y:/:
%.dra8Rect>20, /, (0, /0?
)o(r ste's in &reatin% a 2a#a 5''let
(. Write 2a#a !odet#at 8i&& con"titute t#e app&et. To 8rite code, you
8i&& u"e an editor. *ou 8i&& "a-e t#e Ja-a code in a so(r!e ile> .;a-a i&e?.
Note t#at a&& ;a-a "ource i&e" "#ou&d #a-e a .javae=ten"ion. 4urt#er!ore,
t#e i&e in 8#ic# you put t#e "ource code or a c&a"" "#ou&d #a-e t#e "a!e
na!e a" t#e na!e o t#e c&a"" extendedith .java. In t#e abo-e e=a!p&e,
t#e c&a"" Hello"#ou&d be p&aced in a i&e na!ed Hello.java.
2. &om'ile the 2a#a so(r!e !odein t#e .;a-a i&e into b*te!odeu"in% a Ja-a
co!pi&er "uc# a" ;a-ac t#at i" a part o Ja-a 'e-e&op!ent it >J'?. T#e
re"u&t o co!pi&ation 8i&& be a bytecode i&e. Note t#at a&& bytecode i&e" 8i&&
College of Computer Studies 30
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
31/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES #a-e t#e e=ten"ion .class. In our e=a!p&e, t#e bytecode i&e %enerated ro!
t#e "ource i&e 6e&&o.;a-a 8i&& be na!ed Hello.class.
+. &reate a HT/L ilet#at embedst#i" bytecode i&e in a pair o 3PPLET ta%"
t#at "peciy
t#e &ocation o t#e Ja-a app&et
t#e "i7e >t#at i", t#e 8idt# and #ei%#t? o t#e area occupied by t#e
app&et in t#e bro8"er or app&et -ie8er.
. R(n the a''letby openin% t#e 6TML i&e in a )eb bro8"er or an app&et
-ie8erDa "pecia& A!ini!a&A bro8"er or runnin% app&et" t#at i" a&"o
inc&uded in Sun1" J'.
Note t#at t#e "tep" nece""ary to run a Ja-a app&ication are "&i%#t&y dierent.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
32/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES u"in% t#e J' 2a#a inter'reterca&&edja#a. )eb bro8"er" and app&et -ie8er"
can1t run Ja-a app&ication".
$a* 'arti!(lar attention to the ollowin% when !reatin% an 5''let;
Spe&&in%.
T#e ca"e o &etter". Ja-a treat" Ai!portA and AI!portA a" dierent 8ord"Di
you type AI!portA, your pro%ra! i" 8ron%
Matc#in% "et" o parent#e"e" > ? and brace" . T#e"e are ca&&ed
delimitersand !u"t co!e in openin%$c&o"in% pair".
T#e punctuation !ar5". In t#i" pro%ra! t#ey inc&ude t#e period > . ?,
"e!ico&on > ?, Buotation !ar5 > A ?, and co!!a > , ?.
Specia& "y!bo&". T#e"e inc&ude t#e a"teri"5 > ? and "&a"# > H ?.
o(r!e !ode i" t#e pro%ra! 8ritten in Ja-a or ot#er #i%#$&e-e& pro%ra!!in%
&an%ua%e by t#e pro%ra!!er. Source code can be read by #u!an". Source code
i" "tored in so(r!e iles. T#e Ja-a pro%ra! you "a8 in t#e pre-iou" pa%e i" t#e
"ource code o t#e 6e&&o app&et.
&om'ilation i" t#e proce"" o con-ertin% #i%#$&e-e& "ource code into machine
languageor anot#er or! t#at can be e=ecuted by t#e !ac#ine. )#i&e #i%#$&e-e&
"ource code can be ea"i&y under"tood by #u!an", t#e &o8$&e-e& !ac#ine
&an%ua%e i" a or! t#at i" e=tre!e&y "trenuou" or #u!an" to o&&o8, 8#i&e
e=tre!e&y ea"y or !ac#ine" to interpret.
&om'ileri" a co!puter pro%ra! t#at peror!" co!pi&ation. In our cour"e, you
8i&& !o"t &i5e&y be u"in% a Ja-a co!pi&er na!edjavac t#at i" a part o t#e Ja-a
College of Computer Studies 32
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
33/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 'e-e&op!ent it >J'?. T#ere i" !ore #e&p on in"ta&&in% and u"in% J' in t#e
appendi= "ection on "ettin% up your Ja-a en-iron!ent.
4ro! your Co!!and Pro!pt 8indo8 >a&"o ca&&ed a 'OS con"o&e 8indo8?, you
can ca&& t#e Ja-a co!pi&er ;a-ac to co!pi&e t#e "ource code i&e 6e&&o.;a-a 8it#
t#e co!!and:
ja#a! Hello.ja#a
Co!pi&in% a Ja-a "ource i&e produce" a b*te!ode ile.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
34/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES JM actin% a" an ab"tract !ac#ine, Ja-a ac#ie-e" portabilityacro"" co!puter
p&ator!". T#at i", you can run your bytecode on a Sun co!puter or a Macinto"#
co!puter, or any ot#er co!puter t#at #a" a JM i!p&e!entation. Current&y, JM
i" i!p&e!ented or !o"t e=i"tin% co!puter p&ator!".
&reatin% the HT/L )ile
3 Ja-a app&et i" in-o5ed >run or e=ecuted? ro! 8it#in a )eb pa%e. 3 ca&& o an
app&et #a" to be embeddedin"ide "o!e 6TML docu!ent. 6ere i" t#e "i!p&e"t
or! o an 6TML docu!ent t#at 8i&& run our app&et Hello. Con-ention dictate"
t#at you na!e t#i" i&e 6e&&o.#t!&, but t#at i" ;u"t con-ention and not a Ja-a ru&e
>un&i5e t#e ca"e 8it# 6e&&o.;a-a?.
F6TMLG
F
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
35/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES #ei%#t /
T#e ir"t &i"t ite! &itera&&y "ay" t#at t#e app&et to be e=ecuted i" ca&&ed
A6e&&o.c&a""A. 3" you re!e!ber, ;a-ac created 6e&&o.c&a"" ro! 6e&&o.;a-a 8#en
you co!pi&ed t#e 6e&&o.;a-apro%ra!.
T#e "econd and t#ird &i"t ite!" te&& t#e bro8"er or app&et -ie8er #o8 bi% t#e
app&et 8indo8 !u"t be. )#en a )eb bro8"er or t#e app&et -ie8er e=ecute" or
run" your app&et, it bui&d" a 8indo8 200 pi=e&" 8ide and / pi=e&" #i%# and
e=ecute" t#e bytecode in"truction" in t#e i&e 6e&&o.c&a"". T#e unit o !ea"ure i"
'i0el, 8#ic# i" "#ort or picture e&e!ent. T#e %rap#ic" di"p&ay or your co!puter
i" co!po"ed o t#ou"and" >or !i&&ion"? o t#e"e indi-idua& e&e!ent" t#u" t#e
pi=e& i" t#e "!a&&e"t piece o -i"ua& inor!ation t#at your !onitor can di"p&ay. It i"
about t#e "i7e o a period > . ?, &i5e t#e one at t#e end o t#i" "entence.
R(nnin% the 5''let
Runnin% t#e app&et i" done by u"in% a )eb bro8"er or t#e app&et -ie8er
pro-ided by Sun a" a part o t#e J'. 3n app&et -ie8er i" a "pecia& pro%ra! t#at
cou&d run app&et". It cou&d be con"idered a "tripped$do8n )eb bro8"er t#at can1t
di"p&ay 6TML, but can &ocate an app&et in"ide a )eb pa%e and run it. t#at i", t#ey inc&ude t#e JM? can under"tand
and e=ecute your bytecode. 3nd !o"t popu&ar bro8"er" "uc# a" t#e Internet
E=p&orer and Net"cape Na-i%ator are Ja-a enab&ed. T#i" i" one o t#e rea"on"
or t#e re!ar5ab&e popu&arity o Ja-a. 6o8e-er, dierent bro8"er" #a-e "&i%#t
College of Computer Studies 35
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
36/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES -ariation" in t#e 8ay t#ey e=ecute app&et". )e "u%%e"t t#at you u"e an app&et
-ie8er pro-ided 8it# your J' a" t#e deiniti-e too& to c#ec5 your 8or5.
&om'onents o a 2a#a 5''let
T#e typica& Ja-a app&et i" !ade up o nu!erou" co!ponent" t#at inc&ude
-e*words, identiiers, statements, !lasses, methods, and !ontrol
str(!t(res.
4or e=a!p&e, t#e code
College of Computer Studies 36
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
37/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Apublic class Hello extends AppletA te&&" Ja-a t#at Hello i" a
"ubc&a"" o 5''let.?
#oid$ !ean" t#at t#i" !et#od 8i&& not return a -a&ue.
Identiiers
3n identiier i" a na!e. In Ja-a, an identiier can be a c&a"" na!e, a !et#od
na!e, or a para!eter na!e. 4or e=a!p&e, t#e c&a"" na!e 6e&&o i" an identiier.
Si!i&ar&y, t#e na!e o t#e para!eter gi" a&"o an identiierDand "o i" t#e !et#od
na!e paint.
T#e "ynta= o identiier" i" -ery i!portant. Identiier" can u"e a&p#abetic
c#aracter" o eit#er ca"e >a7 and 3?, nu!ber" >0@?, under"core" > V ?, or
do&&ar "i%n" > W ?. No ot#er c#aracter" are a&&o8ed. No co!!a", #yp#en", etc.
3&"o, identiier" cannot "tart 8it# a nu!ber. 4ina&&y, 5ey8ord" cannot be u"ed a"
identiier" >or t#i" rea"on 5ey8ord" are "o!eti!e" ca&&ed reserved ords?.
The ollowin% are #alididentiiers
The ollowin% are not #alididentiiers
"tudent pieVMa"terpieMa"ter pie pie2pie
do&&arW!an Wdo&&ar!anpieMet#od
pie pie$pie pie,pie
pieHca5e !et#od -oid
College of Computer Studies 37
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
38/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
tatements
tatementsare t#e ba"ic unit" o e=ecution in a pro%ra!. T#ey te&& Ja-a to do
"o!et#in%. T#i" pro%ra! #a" t#e o&&o8in% "tate!ent":
i!port ;a-a.app&et.
i!port ;a-a.a8t.
%.dra8Strin%>A)e&co!e to J33A, +0, +0?
%.dra8Rect>20, /, (0, /0?
T#e"e "tate!ent" re"u&t in "o!e action bein% peror!ed. So!e o t#e
action" are in-i"ib&e to t#e u"er o t#e app&et. T#e u"er 8i&& ne-er actua&&y "ee t#e
im'ort"tate!ent" bein% e=ecuted becau"e t#ey are directi-e" to t#e co!pi&er.
T#e %.dra8Strin% "tate!ent 8i&& re"u&t in a -i"ib&e action becau"e t#e "trin%
Welcome to J!&!8i&& be di"p&ayed in t#e app&et 8indo8. State!ent" #a-e one
t#in% in co!!onDt#ey end 8it# a "e!ico&on > ?. )#ere t#ere i" no "e!ico&on,
t#ere i" no "tate!ent.
T#e body o t#e !et#od 'aint, in our e=a!p&e #a" t8o "tate!ent":
%.dra8Strin%>A)e&co!e to J33A, +0, +0?
%.dra8Rect>20, /, (0, /0?
)indin% and )i0in% Errors
)#en you !odiy t#e Ja-a pro%ra!, you !ay encounter "o!e error" 8#en you
co!pi&e it. )e 8i&& "oon &earn 8#at t#o"e error" !ean and #o8 t#ey can be i=ed.
So!eti!e" it1" a !atter o a !i""ed co!!a or "e!ico&on.
College of Computer Studies 38
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
39/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES In a&& di"a%ree!ent" 8it# t#e co!pi&er, t#e ru&e i" "i!p&e: t#e co!pi&er a&8ay"
8in". T#e aut#or typed in t#e 6e&&o Ja-a app&et pro%ra! and &et out t#e
"e!ico&on at t#e end o t#e "econd &ine "#o8n be&o8Dt#e AVA i" 8#ere t#e
!i""in% "e!ico&on "#ou&d be:
i!port ;a-a.a8t.V
6ere1" 8#at 8a" typed on t#e co!!and &ine >it 8a" "uppo"ed to co!pi&e a Ja-a
pro%ra!?:
G;a-ac 6e&&o.;a-a
3nd #ere are t#e error !e""a%e" i""ued by t#e co!pi&er:
6e&&o.;a-a:: 11 e=pected. 4or!at: filename'line
number'message. T#i" error
!e""a%e indicate" t#at it #a"
detected a prob&e! on &ine nu!ber
: a !i""in% "e!ico&on.
i!port ;a-a.a8t. 6ere t#e co!pi&er di"p&ay" t#e &ine o
code 8#ere t#e error !i%#t be.
X T#e co!pi&er t#en p&ace" a
caret > X ? under t#e &ine o code
8#ere t#e error !i%#t be.
6e&&o.;a-a:Y: Superc&a"" 3pp&et o c&a""
6e&&o not ound.
3 "econd error !e""a%eDt#i" one
&a%%in% &ine nu!ber Y a" a po""ib&e
&ocation o an error.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
40/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES o code 8#ere it t#in5" t#e error i".
X 3%ain, co!pi&er p&ace" a caret > X ?
under t#e &ine o code 8#ere t#e
error !i%#t be.
2 error" T#i" i" t#e tota& nu!ber o error"
ound.
No8, i a "e!ico&on i" put in &ine , 8#ere it be&on%", bot# o t#e"e error" 8i&&
di"appear.
3" indicated abo-e, t#e ba"ic !e""a%e or!at %i-e" t#e i&e na!e and t#en t#e
nu!ber o t#e &ine on 8#ic# t#e error !i%#t be &ocated. Ju"t re!e!ber t#at t#e
error !e""a%e repre"ent" t#e co!pi&er1" be"t %ue"" at 8#at i" 8ron%. T#e be"t
8ay to %et introduced to Ja-a error !e""a%e" i" by e=perience. *our initia&
e=po"ure" 8i&& probab&y be ru"tratin%, but t#at 8i&& pa"". Once you #a-e
corrected a&& o t#e error", you can run your pro%ra! in app&et -ie8er or )eb
bro8"er.
Error" are a part o a pro%ra!!er1" &ieD8e a&& !a5e t#e!. It turn" out t#at
8ritin% a pro%ra! ta5e" a !uc# #i%#er de%ree o perection t#an !o"t o u" are
u"ed to ac#ie-in%, and t#e "i!p&e"t error can 5eep our pro%ra!" ro! runnin%.
3nd once our pro%ra!" run, 8e !u"t continue e-a&uatin% and te"tin% t#e! to
!a5e "ure t#ey are runnin% correct&y. So!e pro%ra!" are "o co!p&e= t#at t#ey
dey co!p&ete te"tin% or correctne"". 4ortunate&y, you 8i&& not 8rite "uc#
pro%ra!" in t#i" c&a"", but "o!e o t#e one" you ill8rite 8i&& be diicu&t to te"t
e=#au"ti-e&y.
College of Computer Studies 40
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
41/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES )#en you 8rote )eb pa%e" in t#e pre-iou" unit, you probab&y !ade t8o type" o
error":
"ynta= error"
e=ecution error"
*nta0 errorsoccurred 8#en you u"ed t#e 8ron% ta% or &et out a c&o"in% ta% or
a c&o"in% an%&e brac5et. 3 "ynta= error 8ou&d cau"e an%&e brac5et" to be
di"p&ayed in your )eb pa%e 8#en -ie8ed in a bro8"er. T#e act t#at t#e )eb
pa%e i" di"p&ayed at a&& i" due to t#e act t#at t#e bro8"er i%nore" 6TML "ynta=
error" and di"p&ay" t#e pa%e re%ard&e"".
*ou can t#in5 o "ynta= error" a" error" in %ra!!ar. Ju"t a" %ra!!atica& error"
in En%&i"# &ead to i&&$or!ed "entence", "ynta= error" in pro%ra!!in% &ead to i&&$
or!ed pro%ra!". 6u!an" are "!art enou%# to iner t#e !eanin% o a "entence,
e-en i t#e "entence i" i&& or!ed.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
42/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES pri"oner1" &ie "pared, in #i" #a"te to 8rite Ai&& not, "pareA #e !i"p&aced t#e
co!!a and 8rote Ai&&, not "pare.A T#u", t#e 5in%1" "tate!ent i&&u"trate" 8#at 8e
#a-e ca&&ed a A&o%ica& error.A T#e "tate!ent i" %ra!!atica&&y correct, and t#e
;ai&er cou&d, t#ereore, under"tand and obey it a" an an"8er to #i" Bue"tion but it
did not acco!p&i"# 8#at t#e 5in% #ad intended, becau"e t#e pri"oner 8a" 5i&&ed.
)#en you 8rite pro%ra!", you 8i&& !o"t &i5e&y a&"o run into "uc# &o%ica& error"
>t#ou%# not a" ata&?. *ou "#ou&d te"t your pro%ra!" to "ee i t#ey do 8#at you
intended t#e! to do. 3%ain, e=ecution error" are not error" in %ra!!ar: i t#e
5in%1" %ra!!arian" #ad e=a!ined #i" "tate!ent, t#ey 8ou&dn1t #a-e ound an
error. Si!i&ar&y, a co!pi&er >8#ic# i" a type o %ra!!arian? 8i&& not u"ua&&y
co!p&ain about &o%ica& error" &o%ica& error" u"ua&&y !anie"t t#e!"e&-e" at run
ti!e, not co!pi&e ti!e.
!ore e-a&uationHte"tin%, p&annin%, and codin%?
you 8i&& #a-e e&i!inated t#e "ynta= error". T#en you !u"t run t#e app&et to "ee i
it doe" 8#at you intended it to do.
College of Computer Studies 42
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
43/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 4our ina& t#ou%#t" about "ynta= error":
(. T#e Ja-a co!pi&er 8i&& pro-ide, at be"t, a %ue"" o 8#at t#e error i" and
8#ere it occurred. So!eti!e" it i" correct and "#o8" you 8#ere to i= t#e
error. So!eti!e" it 8i&& !ar5 a p&ace t#at contain" no error. I you ind no
error -i"ib&e, c#ec5 t#e code abo-e t#e !ar5ed &ine. *ou !ay #a-e !ade
a !i"ta5e one, t8o, or !ore &ine" abo-e t#e indicated p&ace.
2. I t#e Ja-a co!pi&er print" a &on% &i"t o error", you "#ou&d con"ider on&y
t#e ir"t one or t8o to be tru&y -a&id. It i" po""ib&e t#at t#e ir"t "ynta= error
&ed t#e co!pi&er do8n a -ery 8ron% pat# to ind ot#er error" t#at are not
rea&&y error" at a&&. 4i= t#e ir"t one or t8o error" and reco!pi&e your
pro%ra!. *ou !ay ind t#at t#e ot#er error" are no &on%er in t#e &i"t.
+. T#e oppo"ite o ite! t8o can occur. *ou co!pi&e and %et t8o error". *ou
i= t#e!, and t#e ne=t co!pi&e %enerate" t8e&-e error". )#at #appened
Ja-a ound t#e ir"t t8o error" and or -ariou" rea"on" "topped co!pi&in%
t#e pro%ra!. Ja-a &itera&&y Adid not "eeA t#e re"t o t#e error". Once you
i=ed t#e ir"t t8o error" and reco!pi&ed, Ja-a 8a" ab&e to A"eeA t#e ne=t
"et o error". T#i" &ead" to one !a;or piece o ad-ice t#at 8i&& be i!portant
to you 8#en you 8rite your ir"t ori%ina& pro%ra!: co!pi&e reBuent&y and
i= t#e error" a" you %o. It i" u"ua&&y not a %ood idea to 8rite an entire
pro%ra! ro! t#e openin% co!!ent to t#e &a"t AA and t#en "tart co!pi&in%
it or t#e ir"t ti!e. Seein% a &i"t o +/ error" can be -ery depre""in%.
. I you read an error !e""a%e and it !a5e" no "en"e, %et #e&p. I you read
an error !e""a%e and under"tand it but cannot ind t#e actua& error, &oo5
College of Computer Studies 43
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
44/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES a%ain. I you "pend !ore t#an 2+ !inute" in t#i" "tate, %et out"ide #e&p.
3"5 your teac#er, or, i it1" a&&o8ed, a"5 anot#er "tudent to &oo5 at your
code. Ju"t "tarin% at code rare&y #e&p" you ind error". 9et #e&pDan
out"ide pair o eye" can ind t#in%" at a %&ance t#at you, a" t#e ori%ina&
aut#or, !i%#t ne-er "ee.
&ommentin% =o(r &ode
'ocu!entin% code i" one i!portant ta"5 or 8#ic# "ot8are de-e&oper" are
re"pon"ib&e. One e&e!ent o docu!entin% code correct&y i" inc&udin% co!!ent"
8it#in t#e code. Co!!entin% code i" -ery i!portant becau"e it !a5e" t#e code
ea"y to !aintain t#u" !a5in% t#e "ot8are1" tota& co"t o o8ner"#ip &e""
e=pen"i-e t#an it i" or code t#at i" not 8e&& co!!ented. )#en code i" 8e&&
co!!ented, it i" &e"" co"t&y to !aintain becau"e any de-e&oper 8#o i"
!aintainin% it reBuire" &e"" ti!e to under"tand t#e codeK" purpo"e and #o8 t#e
code 8or5".
)#en you are 8ritin% co!!ent", you "#ou&d a""u!e t#at t#e reader o t#e
co!!ent" 8i&& be a de-e&oper o a-era%e co!petence 8#o 5no8" not#in% about
t#e c&a""K" purpo"e or #o8 t#e code unction". )e&& 8ritten co!!ent" "#ou&d
e=p&ain t#e purpo"e o t#e c&a"" "o!e8#ere near t#e be%innin% o t#e c&a"".
Eac# !et#od "#ou&d #a-e at &ea"t one co!!ent p&aced i!!ediate&y beore t#e
!et#od na!e e=p&ainin% t#e purpo"e o t#e !et#od and #o8 t#e !et#od
peror!" it" unction. 3ny co!p&icated approac# t#at you u"ed to acco!p&i"# t#e
!et#odK" unction "#ou&d be brou%#t to t#e attention o t#e reader in order to
College of Computer Studies 44
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
45/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES !ini!i7e t#e ti!e reBuired by t#e de-e&oper to under"tand #o8 t#e code
unction".
3&& o t#e c&a""e" t#at you "ub!it "#ou&d be co!!ented. Co!!ent" can be
created in t8o 8ay". One !et#od t#at can be u"ed to create a co!!ent i" to
be%in t#e &ine o code 8it# a ZHH "y!bo&. T8o e=a!p&e" o&&o8.
HH T#e purpo"e o t#i" c&a"" i" to co!pute t#e a-era%e o t8o nu!ber".
Or,
a-era%e >nu!(nu!2?H2 HH co!pute" t#e a-era%e o t8o nu!ber".
In t#e abo-e e=a!p&e", a&& te=t appearin% ater t#e ZHH 3N' on t#e "a!e &ine 8i&&
be con"idered to be a co!!ent. T#i" approac# i" typica&&y u"ed 8#en t#e
co!!ent i" "#ort in &en%t#. Co!!ent" are not co!pi&ed 8#en t#e c&a"" i"
co!pi&ed. Co!!ent" are not con"idered to be ;a-a "tate!ent". I t#e co!!ent
8rap" to a ne8 &ine and t#at &ine doe" not be%in 8it# a ZHH "y!bo&, t#e co!pi&er
8i&& not treat it a" a co!!ent and a co!pi&e error 8i&& be %enerated. So!eti!e"
t#e co!!ent i" p&aced i!!ediate&y beore t#e &ine>"? o code t#at it i" intended to
docu!ent, a" "#o8n in t#e ir"t e=a!p&e abo-e, and "o!eti!e" t#e co!!ent i"
p&aced on t#e "a!e &ine a" t#e &ine o code t#at it i" intended to docu!ent, a"
"#o8n in t#e "econd e=a!p&e abo-e. Eit#er approac# i" acceptab&e.
So!eti!e", a co!!ent t#at "pan" "e-era& &ine" i" needed. In t#i" "ituation, t#e
co!!ent can be created by be%innin% t#e co!!ent 8it# a ZH "y!bo& and
endin% it 8it# a ZH "y!bo&. 4or e=a!p&e,
H
T#i" c&a"" accept" ro! t#e u"er a na!e to di"p&ay and a co&or in 8#ic# to
College of Computer Studies 45
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
46/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES di"p&ay it. Upon acceptin% t#e u"er1" input, t#e -a&ue o t#e na!e i" "tored in a
-ariab&e ca&&ed "trNa!e and t#e -a&ue o t#e co&or i" "tored in a -ariab&e ca&&ed
"trCo&or. T#en t#e paint !et#od i" ca&&ed to di"p&ay t#e output.
H
In t#e abo-e e=a!p&e, a&& te=t bet8een t#e [H1 "y!bo& and t#e [H1 "y!bo& i" a
co!!ent.
3ra'hi!s and 6ariables
ja#a.a''let
-Pro-ide" t#e c&a""e" nece""ary to create an app&et and t#e c&a""e" an
app&et u"e" to co!!unicate 8it# it" app&et conte=t.
ja#a.awt
- Contain" a&& o t#e c&a""e" or creatin% u"er interace" and or paintin%
%rap#ic" and i!a%e".
ja#a.awt.e#ent
- Pro-ide" interace" and c&a""e" or dea&in% 8it# dierent type" o e-ent"
ired by 3)T co!ponent".
5''let !lass
- !u"t be t#e "uperc&a"" o any app&et t#at i" to be e!bedded in a )eb
pa%e or -ie8ed by t#e Ja-a 3pp&et ie8er.
-
pro-ide" a "tandard interace bet8een app&et" and t#eir en-iron!ent.
&lass 5''let
College of Computer Studies 46
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
47/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES ;a-a.&an%.Ob;ect
\
$$;a-a.a8t.Co!ponent
\
$$;a-a.a8t.Container
\
$$;a-a.a8t.Pane&
\
$$ja#a.a''let.5''let
init + pub&ic -oid init>?
Ca&&ed by t#e bro8"er or app&et -ie8er to inor! t#i" app&et t#at it #a" been
&oaded into t#e "y"te!. It i" a&8ay" ca&&ed beore t#e ir"t ti!e t#at t#e "tart
!et#od i" ca&&ed.
3 "ubc&a"" o 3pp&et "#ou&d o-erride t#i" !et#od i it #a" initia&i7ation to peror!.
4or e=a!p&e, an app&et 8it# t#read" 8ou&d u"e t#e init !et#od to create t#e
t#read" and t#e de"troy !et#od to 5i&& t#e!.
start $ pub&ic -oid "tart>?
Ca&&ed by t#e bro8"er or app&et -ie8er to inor! t#i" app&et t#at it "#ou&d "tart it"
e=ecution. It i" ca&&ed ater t#e init !et#od and eac# ti!e t#e app&et i" re-i"ited in
a )eb pa%e.
3 "ubc&a"" o 3pp&et "#ou&d o-erride t#i" !et#od i it #a" any operation t#at it
8ant" to peror! eac# ti!e t#e )eb pa%e containin% it i" -i"ited. 4or e=a!p&e,
College of Computer Studies 47
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Container.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Panel.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/lang/Object.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Container.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/awt/Panel.html -
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
48/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES an app&et 8it# ani!ation !i%#t 8ant to u"e t#e "tart !et#od to re"u!e
ani!ation, and t#e "top !et#od to "u"pend t#e ani!ation.
sto' + pub&ic -oid "top>?
Ca&&ed by t#e bro8"er or app&et -ie8er to inor! t#i" app&et t#at it "#ou&d "top it"
e=ecution. It i" ca&&ed 8#en t#e )eb pa%e t#at contain" t#i" app&et #a" been
rep&aced by anot#er pa%e, and a&"o ;u"t beore t#e app&et i" to be de"troyed.
3 "ubc&a"" o 3pp&et "#ou&d o-erride t#i" !et#od i it #a" any operation t#at it
8ant" to peror! eac# ti!e t#e )eb pa%e containin% it i" no &on%er -i"ib&e. 4or
e=a!p&e, an app&et 8it# ani!ation !i%#t 8ant to u"e t#e "tart !et#od to re"u!e
ani!ation, and t#e "top !et#od to "u"pend t#e ani!ation.
destro* + pub&ic -oid de"troy>?
Ca&&ed by t#e bro8"er or app&et -ie8er to inor! t#i" app&et t#at it i" bein%
rec&ai!ed and t#at it "#ou&d de"troy any re"ource" t#at it #a" a&&ocated. T#e "top
!et#od 8i&& a&8ay" be ca&&ed beore de"troy.
3 "ubc&a"" o 3pp&et "#ou&d o-erride t#i" !et#od i it #a" any operation t#at it
8ant" to peror! beore it i" de"troyed. 4or e=a!p&e, an app&et 8it# t#read"
8ou&d u"e t#e init !et#od to create t#e t#read" and t#e de"troy !et#od to 5i&&
t#e!.
'aint + pub&ic -oid paint>9rap#ic" %?
Paint" t#e container. T#i" or8ard" t#e paint to any &i%#t8ei%#t co!ponent" t#at
are c#i&dren o t#i" container. I t#i" !et#od i" rei!p&e!ented, "uper.paint>%?
"#ou&d be ca&&ed "o t#at &i%#t8ei%#t co!ponent" are proper&y rendered. I a c#i&d
College of Computer Studies 48
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
49/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES co!ponent i" entire&y c&ipped by t#e current c&ippin% "ettin% in %, paint>? 8i&& not
be or8arded to t#at c#i&d.
O-erride":
paint in c&a"" Co!ponent
Para!eter":
% $ t#e "peciied 9rap#ic" 8indo8
('date + pub&ic -oid update>9rap#ic" %?
Update" t#e container. T#i" or8ard" t#e update to any &i%#t8ei%#t co!ponent"
t#at are c#i&dren o t#i" container. I t#i" !et#od i" rei!p&e!ented,
"uper.update>%? "#ou&d be ca&&ed "o t#at &i%#t8ei%#t co!ponent" are proper&y
rendered. I a c#i&d co!ponent i" entire&y c&ipped by t#e current c&ippin% "ettin% in
%, update>? 8i&& not be or8arded to t#at c#i&d.
O-erride":
update in c&a"" Co!ponent
Para!eter":
% $ t#e "peciied 9rap#ic" 8indo8
re'aint + '(bli! #oid repaint,
Repaint" t#i" co!ponent.
T#i" !et#od cau"e" a ca&& to t#i" co!ponent1" update !et#od a" "oon a"
po""ib&e
Deinin% &lasses
College of Computer Studies 49
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
50/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES pub&ic c&a"" classname
'ec&aration o in"tance -ariab&e(
'ec&aration o in"tance -ariab&e2
... 'ec&aration o ot#er in"tance -ariab&e" ...
'einition o !et#od(
'einition o !et#od2
... 'einition o ot#er !et#od" ...
&omments and White 'a!e
Re!e!ber t#at Ja-a i%nore" 8#ite "paceD"uc# a" b&an5", tab", e!pty &ine",
etc.Dbet8een 8ord" in pro%ra!". Note t#at 8#ite "pace in"ide a "trin% >anyt#in%
in"ide a pair o Buotation !ar5" i" a "trin%? are not i%nored. E=a!p&e: AT#i" i" a
"trin%A.
3&"o note t#at anyt#in% in a &ine t#at co!e" ater a pair o "&a"#e" > HH ? i" a
!omment. Co!!ent" are or t#e #u!an reader, and t#e Ja-a co!pi&er 8i&&
i%nore t#e!. T#u", 8#en t#e co!pi&er "ee" a pair o "&a"#e", it i%nore" t#e re"t
o t#e &ine. *ou can a&"o !ar5 "o!et#in% a" a co!!ent by enc&o"in% it bet8een
a "&a"#$a"teri"5 > H ? and an a"teri"5$"&a"# > H ?. 3nyt#in% t#at a&&" bet8een t#e
co!!ent de&i!iter" H and H 8i&& be i%nored by t#e co!pi&er.
am'le &lass
College of Computer Studies 50
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
51/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES i!port ;a-a.a8t.
i!port ;a-a.app&et.3pp&et
pub&ic c&a"" Per"on
HH 3ttribute" o Per"on a" "#o8n be&o8
pub&ic Strin% na!e
pub&ic int a%e
pub&ic int #ei%#t Y0
pub&ic Strin% "e=
pub&ic -oid dra8Per"on >9rap#ic" %, int =, int y?
HH dra8" t#e per"on, anc#ored at >=,y?
HH dra8 #ead
%.dra8O-a&>=, y, 20, +0?
%.dra83rc>=(2, y, , @, 200, (+/?
%.dra83rc>=+, y, , @, 200,(+/?
%.dra8O-a&>=, y20, , /?
HHdra8 body
%.dra8Line>= Y, y +0, = Y, y #ei%#t?
%.dra8Line>=(2, y +0, =(2, y #ei%#t?
HHdra8 ar!"
%.dra8Line>= Y, y +0, =, y 0?
%.dra8Line>=, y 0, = Y, y /0?
College of Computer Studies 51
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
52/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES %.dra8Line>= (2, y +0, = 20, y 0?
%.dra8Line>= 20, y 0, = (2, y /0?
HHdra8 eet
%.i&&Rect>=, y #ei%#t, (0, +?
%.i&&Rect>=((, y #ei%#t, (0, +?
i!port ;a-a.a8t.
i!port ;a-a.app&et.3pp&et
pub&ic c&a"" Per"on3pp&et e=tend" 3pp&et
pub&ic Per"on ;oe HH ;oe i" an in"tance -ariab&e.
pub&ic -oid init>?
H init>? i" ca&&ed on&y once.
T#i" i" 8#ere t#e app&et i" initia&i7ed.H
;oe ne8 Per"on>? HH Create a ne8 ob;ect in"tance or Joe 6a!i&ton.
pub&ic -oid paint > 9rap#ic" % ?
;oe.dra8Per"on>%, 20, (/? HH 'ra8 ;oe on t#e app&et.
Inheritan!e
College of Computer Studies 52
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
53/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES In#eritance i" u"ed to "peciy an i"$a$5ind$o re&ation"#ip bet8een t8o c&a""e".
In#eritance pro-ide" a or!a& !ec#ani"! or code reu"e.
'einition: 3 "ubc&a"" i" a c&a"" t#at deri-e" ro! anot#er c&a"". 3 "ubc&a""
in#erit" state and behavior ro! a&& o it" ance"tor". T#e ter! superclass reer" to
a c&a""1" direct ance"tor a" 8e&& a" a&& o it" a"cendant c&a""e".
i!port ;a-a.a8t.
pub&ic c&a"" Pro e=tend" Per"on
HH attribute" o Pro a" "#o8n be&o8
pub&ic doub&e "a&ary
HH !et#od" o Pro
pub&ic -oid dra8c?
%.i&&Rect>=, y, Y0, /?
HH 'ra8 a b&ac5 co&ored inner rectan%&e
%."etCo&or>Co&or.b&ac5?
%.i&&Rect>= , y , 2, /?
College of Computer Studies 53
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
54/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES pub&ic -oid dra8Pro>9rap#ic" %, int =, int y?
dra8Per"on>%, =, y?
dra8=,y?
pub&ic -oid dra8Per"on >9rap#ic" %, int =, int y?
HH dra8 #ead
%.dra8O-a&>=, y, 20, +0?
%.dra8O-a&>= ((, y ((, /, +? HH ri%#t eye
%.dra8O-a&>= +, y ((, /, +? HH &et eye
%.dra8O-a&>= , y 20, , +? HH !out#
HHdra8 body
%.dra8Line>= Y, y +0, = Y, y #ei%#t?
%.dra8Line>= (2, y +0, = (2, y #ei%#t?
HH dra8 ar!"
College of Computer Studies 54
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
55/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES HH ri%#t ar! >appear" to t#e &et?
%.dra8Line>= Y, y +0, =, y 0?
%.dra8Line>=, y 0, = Y, y /0?
HH &et ar!
%.dra8Line>= (2, y +0, = 20, y 0?
%.dra8Line>= 20, y 0, = 2Y, y +0?
HHdra8 eet
%.i&&Rect>=, y #ei%#t, (0, +?
%.i&&Rect>= ((, y #ei%#t, (0, +?
pub&ic -oid dra89rap#ic" %, int =, int y?
dra8Pro>%, =, y?
dra8
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
56/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Namin% &on#entions or Instan!e 6ariables and &lasses
T#e "ynta= o Ja-a identiier" i" i!portant, a" are t#e na!in% con-ention"
t#at identiier" o&&o8. T#e con-ention" or Ja-a identiier" are
"trai%#tor8ard. Con-ention" are %enera&&y o&&o8ed Aru&e"A t#at
pro%ra!!er" u"e. Con-ention" are not ru&e" in t#e rea& "en"e, "ince t#eir
-io&ation 8i&& not pre-ent Ja-a pro%ra!" ro! co!pi&in%. e"pecia&&y t#e per"on
8#o %rade" your pro%ra!"? to read your code. So!e o t#e"e con-ention"
are:
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
57/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES Identiier" or &oca& -ariab&e" and para!eter" "#ou&d be "#ort and
!eanin%u&. T#ey u"ua&&y do not contain 8#o&e 8ord". One &etter identiier"
D"uc# a" i, +, and xD"#ou&d be a-oided e=cept or te!porary -ariab&e"
or &oop -ariab&e", 8#ic# 8e 8i&& "ee &ater in t#e cour"e.
T#e na!e" o c&a""e" "#ou&d be "#ort and de"cripti-e noun" or noun
p#ra"e". T#e ir"t &etter o a c&a"" na!e identiier "#ou&d be%in 8it# a
capita& &etter. T#e re!ainder o t#e c&a"" na!e o&&o8" t#e con-ention" o
-ariab&e identiier". C&a"" na!e" "#ou&d be tru&y de"cripti-e o 8#at t#e
c&a"" repre"ent".
Writin% a /ethod
T#e %enera& "ynta= or a !et#od i":
pub&ic returnType !et#odNa!e >TypeOPara!eter( para!(,
TypeOPara!2 para!2,...?
type( &oca&ariab&e(
type2 &oca&ariab&e2
...
"tate!ent(
"tate!ent2
...
&on#entions or Namin% /ethods
College of Computer Studies 57
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
58/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES 3n identiier cannot "tart 8it# a nu!ber and can con"i"t o nu!ber" >0@?, &etter"
>a7, 3?, do&&ar "i%n" > W ?, and under"core" > V ?. Note t#at !et#od na!e" are
a&"o identiier", "o t#ey "#ou&d o&&o8 t#e ru&e" or creatin% identiier". T#e
%enera& na!in% con-ention or !et#od" i" to u"e a -erb or -erb p#ra"e. I t#e
!et#od na!e con"i"t" o !ore t#an one 8ord, t#e ir"t 8ord i" in &o8erca"e
&etter" t#e "ub"eBuent 8ord" "tart 8it# an upperca"e &etter". 4or e=a!p&e, t#e
ma-eDean, !et#od abo-e o&&o8" t#i" con-ention or !u&ti$8ord".
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
59/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES &allin% a /ethod
Met#od ca&&" o&&o8 t8o pattern":
One pattern i" or a !et#od o a c&a"" to ca&& anot#er !et#od be&on%in% to
t#e "a!e c&a"". To ca&& !et#od" in t#e "a!e c&a"", 8e "i!p&y u"e t#e
na!e o t#e !et#od o&&o8ed by a pair o parent#e"e".
3not#er pattern i" or a !et#od o a c&a"" to ca&& a !et#od be&on%in% to a
dierent c&a"". To ca&& a !et#od be&on%in% to a dierent c&a"", 8e "peciy
t#e na!e o t#e ob;ect and a dot or period > . ? ater t#e ob;ect na!e, and
t#en t#e !et#od na!e.
Be*ond Dis'la*in% Ima%es and Te0t
T#e Ja-a &an%ua%e pro-ide" a "et o ob;ect" t#at a&&o8 u"er" to interact 8it#
pro%ra!". T#e"e ob;ect" are %rap#ica& in nature and u"ua&&y reBuire a !ou"e
c&ic5 or "o!e part o t#e interaction. T#ey are co!!on&y ca&&ed wid%etsin 9UI
;ar%on, and t#ey !a5e up a a!i&y o e&e!ent" 8e can u"e to bui&d %rap#ica& u"er
interace" >or 9UI"?.
&lass Hierar!h*
3 part o t#e c&a"" #ierarc#y or t#e c&a"" &om'onenti" %i-en be&o8.
College of Computer Studies 59
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
60/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES
E-eryt#in% in t#e dra8in% t#at i" be&o8 &om'onent i" a "ubc&a"" o
&om'onent.
Label &lass
T#e c&a"" Label#a" t#ree con"tructor".
B(tton &lass
T#e B(ttonc&a"" 8i&& be u"ed e=ten"i-e&y or u"er input. T#e B(ttonc&a""
#a" t8o con"tructor" and t8o !et#od" t#at %et and "et t#e &abe& t#at i"
di"p&ayed in"ide t#e button..
Te0t&om'onent &lass
College of Computer Studies 60
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
61/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES T#e Te0t&om'onentc&a"" i" an ab"tract c&a"", "o it #a" no con"tructor.
T#e one Te0t&om'onent!et#od to re!e!ber i" setEditable, ,
Te0t)ield and Te0t5rea
T#e Te0t)ield and Te0t5rea c&a""e" are bot# "ubc&a""e" o t#e
Te0t&om'onent c&a"". T#e Te0t)ield c&a"" #a" our con"tructor" t#at
enab&e one to bui&d t#e e=act Te0t)ieldob;ect t#at i" needed.
Te0t5rea ob;ect" are -ery "i!i&ar to Te0t)ields. 6o8e-er, 8#erea"
Te0t)ield ob;ect" a&&o8 u"er" to input a "in%&e &ine o te=t on&y, Te0t5rea
ob;ect" a&&o8 t#e! to input !u&tip&e &ine" o te=t.
5 im'le E0am'le; Labels: Te0t)ields: Te0t5reas: and B(ttons
H
SS'($GIntroduction to Ja-a$GUnder"tandin% C&a""e"$G)id%et"
)id%et e=a!p&e"
3ttention:
T#e code #ere u"e" a e8 concept" t#at #a-e not yet been introduced.
U"e t#i" app&et ;u"t a" a
%rap#ica& i&&u"tration o 8id%et".
College of Computer Studies 61
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
62/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES H
i!port ;a-a.app&et.
i!port ;a-a.a8t.
pub&ic c&a"" )id%et" e=tend" 3pp&et
pri-ate Labe& na!eLabe&, addre""Labe&, !e""a%eLabe&
pri-ate Te=t4ie&d na!e, addre""
pri-ate Te=t3rea !e""a%e
pri-ate ?
"etLayout>nu&&?
HH Create t#ree Labe&", na!ed ANa!eA, A3ddre""A and AMe""a%eA.
na!eLabe& ne8 Labe&>ANa!eA?
addre""Labe& ne8 Labe&>A3ddre""A?
!e""a%eLabe& ne8 Labe&>AMe""a%eA?
HH Create Te=t4ie&d" to enter t#e na!e and addre"".
na!e ne8 Te=t4ie&d>AJean$Luc PicardA?
College of Computer Studies 62
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
63/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES addre"" ne8 Te=t4ie&d>AEnterpri"eA?
HH Create a Te=t3rea to enter a !e""a%e.
!e""a%e ne8 Te=t3rea>ARe"i"tance i" 4uti&eA, /0, /0?
HH Create a 0, 0? na!eLabe&."etSi7e>0, 20?
addre""Labe&."etLocation>0, 20? addre""Labe&."etSi7e>0, 20?
!e""a%eLabe&."etLocation>0, 0? !e""a%eLabe&."etSi7e>0, 20?
na!e."etLocation>(00, 0? na!e."etSi7e>220, 20?
addre""."etLocation>(00, 20? addre""."etSi7e>220, 20?
!e""a%e."etLocation>(00, 0? !e""a%e."etSi7e>220, (00?
button."etLocation>((0, (/0? button."etSi7e>(00, 20?
HH 3dd t#e 8id%et" to t#e app&et
add>na!eLabe&? add>na!e?
add>addre""Labe&? add>addre""?
add>!e""a%eLabe&? add>!e""a%e?
College of Computer Studies 63
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
64/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES add>button?
What is E#ent+Dri#en $ro%rammin%>
E#ent+dri#en 'ro%rammin% i" ca&&ed t#at becau"e it in-o&-e" 8ritin%
pro%ra!" t#at react to e#entsD"uc# a" 5ey pre""e", button c&ic5", !ou"e
!o-e!ent", and "o on. T#e ba"ic rea&ity o t#i" type o pro%ra!!in% i" t#at
e-ent" are unpredictab&e.
Step" to your approac#:
T#e o&&o8in% "tep" out&ine one approac# to de"i%nin% pro%ra!".
(. T#e !o"t i!portant "tep in pro%ra!!in% i" to de"i%n your pro%ra!
beore you be%in to code it. Startin% to code a pro%ra! beore you
#a-e %i-en t#ou%#t to t#e de"i%n i" certain to re"u&t in ru"tration.
*ou "#ou&d "5etc# your approac# on paper beore you be%in to code.
4or !any "tudent", co!p&etin% "tep" 2 t#rou%# / be&o8, t#en
"5etc#in% t#e &o8 o t#e pro%ra! i" -ery #e&pu&.
2. 4or any pro%ra!!in% a""i%n!ent, t#e ir"t 5ey i" to under"tand t#e
prob&e!.
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
65/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES . 'eine 8#at input" are needed ro! t#e u"er in order or your
pro%ra! to produce t#e correct output.
/. 3ter you 5no8 8#at t#e output i" and 8#at input" t#e u"er !u"t
pro-ide, you "#ou&d be ab&e to deine 8#at action" your pro%ra!
"#ou&d peror! on t#e u"er1" input" in order to produce t#e
appropriate output".
. Many be%innin% de-e&oper" can co!p&ete "tep" ( t#rou%# abo-e
8it#out !uc# prob&e!. T#e ne=t "tep, de"i%nin% #o8 t#e pro%ra!
8i&& accept t#e input" ro! u"er, proce"" t#e!, ca&cu&ate t#e output"
and di"p&ay t#e output" to t#e u"er i" 8#ere !o"t "tudent" #a-e
prob&e!". So!eti!e" "tudent" #a-e prob&e!" identiyin% in 8#ic#
!et#od" t#ey "#ou&d p&ace to code to peror! t#e -ariou" unction".
*ou "#ou&d dra8 a "i!p&e dia%ra! or out&ine on paper beore you
be%in to code. T#i" dia%ra! or out&ine "#ou&d "#o8 in %enera& ter!",
t#e !et#od" t#at you are %oin% to u"e, t#e -ariab&e" reBuired in eac#
!et#od and t#e action" t#at eac# !et#od i" %oin% to peror!
T#ere are "o!e %uide&ine" t#at you !i%#t ind to be u"eu& in !a5in% your
dia%ra! or out&ine. T#e ir"t action t#at a pro%ra! peror!" i" u"ua&&y to
accept t#e input" ro! t#e u"er. T#e actionPeror!ed !et#od i" co!!on&y
u"ed to accept input ro! t#e u"er and "tore t#e u"er1" input" in -ariab&e".
So!eti!e", code can be p&aced in t#e actionPeror!ed !et#od t#at te"t"
t#e -a&ue" o t#e u"er1" input and deter!ine" 8#ic# !et#od to ca&& ba"ed on
t#e -a&ue o t#e u"er1" input. So!eti!e", t#e actionPeror!ed !et#od
College of Computer Studies 65
-
8/13/2019 MELJUN CORTES MANUAL Intro Information System JAVA Applets COMP02
66/198
COMP02 Instructional Manual
Prepared by: MELJUN P. CORTES "i!p&y accept" t#e u"er" input and ca&&" a dierent !et#od, pa""in%
para!eter" to t#e ca&&ed !et#od to "et t#e -a&ue o t#e ca&&ed !et#od1" &oca&
-ariab&e". T#i" !et#od t#en te"t" t#e -a&ue" o t#e u"er" input" i nece""ary
are direct" t#e &o8 o pro%ra!. T#e "trate%y t#at i" e!p&oyed 8i&& be
deter!ined ba"ed on t#e nature o t#e pro%ra!!in% a""i%n!ent. In any
e-ent, t#e actionPeror!ed !et#od i" u"ua&&y u"ed to accept t#e input" ro!
t#e u"er, decide 8#at action to ta5e ne=t and pa"" para!eter" to t#e
!et#od t#at it ca&&".
T#e !et#od" t#at proce"" t#e u"er1" input are u"ua&&y created and %i-en a
na!e by t#e de-e&oper, 8it# eac# !et#od #a-in% a narro8&y deined
unction. T#e"e !et#od" u"ua&&y u"e %&oba& -ariab&e" andHor accept -a&ue"
or t#eir &oca& -ariab&e" a" para!eter" ro! t#e !et#od" t#at ca&& t#e!. 3t
"o!e point, one o t#e"e !et#od" u"ua&&y ca&&" t#e paint !et#od, pa""in%
t#e appropriate para!eter", in order to #a-e t#e output di"p&ayed to t#e
u"er.
. T#e paint !et#od i" co!!on&y u"ed to di"p&ay t#e output to t#e u"er.
So!eti!e" ca&cu&ation" t#at are crucia& to di"p&ayin% t#e output correct&y,
"uc# a" t#e "i7e o t#e app&et, are p&aced in t#e paint !et#od to ca