Vous êtes sur la page 1sur 17

Question Paper

Object Oriented Programming and Java (MC221) : April 2!


"ection A : #asic Concepts ($ Mar%s)
This section consists of questions with serial number 1 - 30.
Answer all questions.
Each question carries one mark.
Maximum time for answering Section A is 30 Minutes.
1&
onsi!er the following "rogram#
im"ort m$%ibrar$.&'
"ublic class ShowSomelass
(
)) co!e for the class...
*
+hat is the name of the ,a-a file containing this "rogram.
/a0 M$ librar$.,a-a
/b0 Show Some lass.,a-a
/c0 Show Some lass
/!0 Show Some lass.class1
/e0 An$ file name with the ,a-a suffix will !o.
<Answer>
2&
+hich of the following is true.
/a0 1n ,a-a2 an instance fiel! !eclare! "ublic generates a com"ilation error
/b0 int is the name of a class a-ailable in the "ackage ,a-a.lang
/c0 1nstance -ariable names ma$ onl$ contain letters an! !igits
/!0 A class has alwa$s a constructor /"ossibl$ automaticall$ su""lie! b$ the ,a-a com"iler0
/e0 The more comments in a "rogram2 the faster the "rogram runs.
<Answer>
$&
onsi!er the following co!e sni""et#
String ri-er 3 new String /4olumbia50'
S$stem.out."rintln/ri-er.length/00'
+hat is "rinte!.
/a0 6
/b0 7
/c0 8
/!0 olumbia
/e0 9i-er.
<Answer>
'&
A constructor
1. Must ha-e the same name as the class it is !eclare! within.
11. 1s use! to create ob,ects.
111. Ma$ be !eclare! "ri-ate.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 :nl$ /1110 abo-e
/!0 ;oth /10 an! /110 abo-e
/e0 All /102 /110 an! /1110 abo-e.
<Answer>
(&
+hich of the following ma$ be "art of a class !efinition.
1. 1nstance -ariables.
11. 1nstance metho!s.
111. onstructors.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 :nl$ /1110 abo-e
/!0 ;oth /10 an! /110 abo-e
/e0 All /102 /110 an! /1110 abo-e.
<Answer>
)&
+hich is the *alse statement among the following !ifferences between a <a-a a""let an! a <a-a a""lication.
1. An a""lication can in general be truste! whereas an a""let can=t.
11. An a""let must be execute! in a browser en-ironment.
111. An a""let is able to access the files of the com"uter it runs on.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 :nl$ /1110 abo-e
/!0 ;oth /10 an! /110 abo-e
/e0 ;oth /10 an! /1110 abo-e.
<Answer>
+&
onsi!er
"ublic class M$lass(
"ublic M$lass/0()&co!e&)*
)) more co!e...
*
To instantiate M$lass2 $ou woul! write.
/a0 M$lass mc 3 new M$lass/0'
/b0 M$lass mc 3 M$lass/0'
/c0 M$lass mc 3 M$lass'
/!0 M$lass mc 3 new M$lass'
/e0 1t can=t be !one. The constructor of M$lass shoul! be !efine! as "ublic -oi! M$lass/0 ()&co!e&)*.
<Answer>
!&
+hat is b$te co!e in the context of <a-a.
/a0 The t$"e of co!e generate! b$ a <a-a com"iler
/b0 The t$"e of co!e generate! b$ a <a-a >irtual Machine
/c0 1t is another name for a <a-a source file
/!0 1t is the co!e written within the instance metho!s of a class
/e0 1t is another name for comments written within a "rogram.
<Answer>
,&
+hat is garbage collection in the context of <a-a.
/a0 The o"erating s$stem "erio!icall$ !eletes all of the ,a-a files a-ailable on the s$stem
/b0 An$ "ackage im"orte! in a "rogram an! not use! is automaticall$ !elete!
/c0 +hen all references to an ob,ect are gone2 the memor$ use! b$ the ob,ect is automaticall$ reclaime!
/!0 The <>M checks the out"ut of an$ <a-a "rogram an! !eletes an$thing that !oesn=t make sense
/e0 <anitors working for Sun Micro S$stems are require! to throw awa$ an$ Microsoft !ocumentation foun!
in the em"lo$ees= offices.
<Answer>
1
&
?ou rea! the following statement in a <a-a "rogram that com"iles an! executes.
submarine.!i-e/!e"th0'
+hat can $ou sa$ for sure.
/a0 @e"th must be an int
/b0 @i-e must be a metho!
/c0 @i-e must be the name of an instance fiel!
/!0 Submarine must be the name of a class
/e0 Submarine must be a metho!.
<Answer>
11&
onsi!er
int a 3 6'
int b 3 1A'
while/aBb0(
S$stem.out."rintln/C1n the loo"C0'
aD3A'
b-3A'
*
Eow man$ times is the "hrase C1n the loo"C "rinte!.
/a0 1
/b0 A
/c0 3
/!0 F
/e0 G.
<Answer>
12
&
1n <a-a2 elements of an arra$ are automaticall$ initialiHe! to some !efault -alue.
+hat is the !efault -alue for the elements of an arra$ of integers.
/a0 0
/b0 C0C
/c0 null
/!0 1
/e0 =0=.
<Answer>
1$
&
?ou want to initialiHe all of the elements of a !ouble arra$ a to the same -alue equal to 1.G.
+hat coul! $ou write.
Assume that the arra$ has been correctl$ initialiHe!.
/a0 Ior /int i31' iBa.length' iDD0 aJiK 3 1.G'
/b0 Ior /int i30' iB3a.length' iDD0 aJiK 3 1.G'
/c0 Ior /int i30' iBa.length' iDD0 aJiK 3 1.G'
/!0 Ior /int i30' iBa.lengthD1' iDD0 aJiK 3 1.G'
/e0 Ior /int i30' iBa.length-1' iDD0 aJiK 3 1.G'.
<Answer>
1'
&
+hich of the following while statements is equi-alent to
!o(
$3xD7'
xDD'
*while/xBL0'
/a0 $3xD7'
xDD'
while/xBL0(
$3xD7'
xDD'
*
/b0 while/xBL0(
$3xD7'
xDD'
*
$3xD7'
xDD'
/c0 while/xB3L0(
$3xD7'
xDD'
*
/!0 ;oth /a0 an! /b0 abo-e
/e0 /a02 /b0 an! /c0 abo-e.
<Answer>
1(
&
onsi!er the co!e#
intJK x 3 (G2627282L*'
intJK $ 3 x'
$JAK 3 10'
+hat is the -alue of xJAK.
/a0 6
/b0 7
/c0 10
/!0 8
/e0 0.
<Answer>
1)
&
onsi!er the following "iece of co!e#
int i'
for/i30' iB10' iDD0(
)& some co!e that !oesn=t mo!if$ i &)
*
S$stem.out."rintln/Ci3CDi0' )& line A &)
+hat is "rinte! b$ the statement on line A.
/a0 i 3 0
/b0 i 3 L
/c0 i 3 10
/!0 i 3 11
/e0 This "iece of co!e !oesn=t com"ile since the sco"e of i is limite! to the for loo".
<Answer>
1+
&
+hat is "rinte! b$ the following co!e fragment.
intJK a 3 (0212A232F2G26*'
S$stem.out."rintln/a.length0'
/a0 G
/b0 6
/c0 7
/!0 8
/e0 an=t tell. There is not enough information.
<Answer>
1!
&
Eow woul! $ou !eclare an! initialiHe an arra$2 "alette2 of olor ob,ects of siHe 6.
/a0 olor "alette 3 new olorJGK'
/b0 olor "aletteJ6K'
/c0 olor JGK "alette'
/!0 olor JK "alette 3 new olorJ6K'
/e0 olor JK "alette 3 new olorJGK'.
<Answer>
1,
&
The !ocumentation of a class lists a metho! castTo1ntArra$ that has a !ouble arra$ as a formal "arameter an!
returns an arra$ of integers.
+hat is the signature of the metho!.
/a0 Mublic -oi! castTo1ntArra$ /!oubleJ K a0
/b0 Mublic J K castTo1ntArra$ /!ouble a0
/c0 Mublic int castTo1ntArra$ /!ouble a0
/!0 Mublic int J K castTo1ntArra$ /!oubleJ K a0
/e0 Mublic int J K castTo1ntArra$ /0.
<Answer>
2
&
+hich of the following is true regar!ing the continue statement in a for loo".
/a0 ontinue transfers the control flow to the initialiHation statement of the for loo"
/b0 ontinue transfers the control flow to the con!itional statement of the for loo"
/c0 ontinue transfers the control flow to the u"!ate statement of the for loo"
/!0 ontinue transfers the control flow to the statement ,ust after the for loo"
/e0 ontinue transfers the control flow to the statement ,ust before the for loo".
<Answer>
21
&
+hich of the following is true about the switch statement in <a-a.
/a0 A !efault sen!s execution imme!iatel$ to the en! of the switch statement
/b0 A break sen!s execution imme!iatel$ to the en! of the switch statement
/c0 A case sen!s execution imme!iatel$ to the en! of the switch statement
/!0 A break sen!s execution imme!iatel$ to the en! of the next case
/e0 The statements in a switch continue to execute as long as the con!ition at the to" of the switch remains
true.
<Answer>
22
&
Among these ex"ressions2 which is not of t$"e String.
1. C0C.
11. CabCDCc!C.
111. =0=.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 :nl$ /1110 abo-e
/!0 ;oth /10 an! /1110 abo-e
/e0 ;oth /110 an! /1110 abo-e.
<Answer>
2$
&
The interface of a class refers to
/a0 The list of all of the fiel!s an! metho!s of the class
/b0 The list of all of the "ublic fiel!s an! "ublic metho!s of the class
/c0 The list of all the "ri-ate fiel!s an! "ublic metho!s of the class
/!0 The list of all the "ri-ate fiel!s an! "ri-ate metho!s of the class
/e0 The comments written in the co!e of the class.
<Answer>
2'
&
onsi!er the following co!e fragment#
9ectangle r1 3 new 9ectangle/0'
r1.setolor/olor.blue0'
9ectangle rA 3 r1'
rA.setolor/olor.re!0'
After the abo-e "iece of co!e is execute!2 what are the colors of r1 an! rA /in this or!er0.
/a0 olor.blue
olor.re!
/b0 olor.blue
olor.blue
/c0 olor.re!
olor.re!
/!0 olor.re!
olor.blue
/e0 an=t tell. There is not enough information.
<Answer>
2(
&
+hat is the t$"e an! -alue of the following ex"ression. /notice the integer !i-ision0
-F D 1)A D A&-3 D G.0
/a0 int
-G
/b0 !ouble
-F.G
/c0 int
-F
/!0 !ouble
-G.0
/e0 All of the abo-e.
<Answer>
2)
&
+hat is "rinte! b$ the following statement.
S$stem. out. Mrint /CEello2Nnworl!OC0'
/a0 Eello2 Nnworl!O
/b0 Eello2 worl!O
/c0 Eello2
worl!O
/!0 CEello2 Nnworl!OC
/e0 All of the abo-e.
<Answer>
2+
&
1n the class ar2 $ou want to create an instance metho! Pis S"ee!ingQ to check whether an ob,ect to t$"e ar is
s"ee!ing or not.
:ne of the instance fiel!s of the ar class is a !ouble -ariable s"ee! equal to the current s"ee! of the ar ob,ect.
The class also lists a constant static !ouble fiel! SMEE@R%1M1T2 equal to the legal s"ee! limit for the !ri-ing
con!itions of the ar ob,ect.
+hat metho! signature woul! be best for is S"ee!ing.
/a0 "ublic -oi! is S"ee!ing /!ouble s"ee!0
/b0 "ublic boolean is S"ee!ing/0
/c0 "ublic boolean is S"ee!ing/!ouble s"ee!2 !ouble SMEE@R%1M1T0
/!0 "ublic is S"ee!ing /0
/e0 "ublic boolean is S"ee!ing /s"ee!2 SMEE@R%1M1T0.
<Answer>
2!
&
onsi!er the two metho!s /within the same class0#
"ublic int foo/int a2 String s0
(
s 3 C?ellowC'
a3aDA'
return a'
*
"ublic -oi! bar/0
(
int a33'
String s 3 C;lueC'
a 3 foo/a2s0'
S$stem.out."rintln/Ca3CDaDC s3CDs0'
*
+hat is "rinte!.
/a0 a 3 3 s 3 ;lue
/b0 a 3 G s 3 ?ellow
/c0 a 3 3 s 3 ?ellow
/!0 a 3 G s 3 ;lue
/e0 The co!e !oesn=t com"ile. 1n <a-a2 it is forbi!!en to use the same name for a local -ariable in two
!ifferent metho!s.
<Answer>
2,
&
1n a <a-a "rogram2 $ou rea! the following statement that com"iles an! executes.
ookie cookie 3 new ookie/Cchocolate chi"sC0'
+hat can $ou conclu!e.
1. ookie is the name of a class.
11. ookie is a "rimiti-e t$"e -ariable.
111. The ookie class must ha-e a constructor that has one formal "arameter of t$"e String.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 ;oth /10 an! /110 abo-e
/!0 ;oth /10 an! /1110 abo-e
/e0 All /102 /110 an! /1110 abo-e.
<Answer>
$
&
onsi!er the following class !efinition#
"ublic class M$lass(
"ri-ate int -alue'
"ublic -oi! set>alue/int i0( )& co!e &) *
)) :ther metho!s...
*
The metho! set>alue assigns the -alue of i to the instance fiel! -alue. +hat coul! $ou write for the
im"lementation of set>alue.
1. >alue 3 i'.
11. This.-alue 3 i'.
111. >alue 33 i'.
/a0 :nl$ /10 abo-e
/b0 :nl$ /110 abo-e
/c0 :nl$ /1110 abo-e
/!0 ;oth /10 an! /110 abo-e
/e0 All /102 /110 an! /1110 abo-e.
<Answer>
-./ O0 "-C12O. A
"ection # : Problems (( Mar%s)
This section consists of questions with serial number 1 S
G .
Answer all questions.
Marks are in!icate! against each question.
@etaile! workings shoul! form "art of $our answer.
@o not s"en! more than 110 - 1A0 minutes on Section ;.
1&
+rite a ,a-a "rogram to generate the following "attern.
1
A A
3 3 3
F F F F
TT..
10 10 10TT.
Eere the user has to in"ut the -alue through the ke$boar! an! base! on that
4the "attern is to be shown as abo-e5 to the ke$e! number.
(
1marks0
<Answer>
2&
@esign an! write a class to re"resent a bank account that inclu!es the
following
Members#
i. @ata members
:wner name
Account number
;alance amount in the account
ii. Metho!s members
To assign initial -aluesU
To !e"osit an amount
To with!raw an amount after checking balance
To !is"la$ the owner name an! balance
Mo!if$ the abo-e class to incor"orate a constructor to "ro-i!e initial -alues.
(
1marks0
<Answer>
$&
+rite a ,a-a "rogram that executes three threa!s. Iirst Threa! !is"la$s
41IA1 VW1>E9S1T?5 e-er$ secon!2 the secon! threa! !is"la$s 41IA1
MA @istance %earning52 e-er$ two secon!s an! thir! threa! !is"la$s
4%ASS :I A0085 e-er$ three secon!s. reate the three threa!s using
Threa! class.
(
1marks0
<Answer>
'&
+rite an a""let to !raw a sha"e fille! with the color selecte! from the list
boxes as follows#
(
1marks0
<Answer>
(&
+rite a ,a-a a""lication to create a table an! insert fi-e recor!s of !ata.
/Table name# ustomer2 Iiel!s# ustno2 name2 a!!ress2 MhoneWo2 re!it
ar! Wo0.
(
1marks0
<Answer>
-./ O0 "-C12O. #
"ection C : Applied 13eor4 (2 Mar%s)
This section consists of questions with serial number 6 -
7 .
Answer all questions.
Marks are in!icate! against each question.
@o not s"en! more than AG -30 minutes on Section .
)&
om"are an! contrast a""lets to a""lications. +hen woul! one choose
to use A""lets o-er a""lications. +hat are the limitations of a""lets.
@escribe the lifec$cle of a""lets an! a""lications. an an a""lication be
an a""let.
( 1 mar%s)
<Answer>
+&
a. @iscuss the !ifferent access s"ecifiers a-ailable in the <a-a
"rogramming language.
( ( mar%s)
b. +hat are the ste"s in-ol-e! in <@; Mrogram.
( ( mar%s)
<Answer>
-./ O0 "-C12O. C
-./ O0 Q5-"12O. PAP-6
"uggested Ans7ers
Object Oriented Programming and Java (MC221) : April 2!
"ection A : #asic Concepts
Ans7er 6eason
1& ; The name of the ,a-a file to ShowSomelass.,a-a is the a""ro"riate file name
to be gi-en.
< TOP >
2& @ A class has alwa$s a constructor /"ossibl$ automaticall$ su""lie! b$ the ,a-a
com"iler0.
< TOP >
$& The length of the string is 8.
< TOP >
'& E onstructor must ha-e the smae name as the class it is !eclare! within. .1t is
use! to create ob,ects an! ma$ be !eclare! "ri-ate
< TOP >
(& E lass !efinition ma$ ha-e instance -ariables2 instance metho!s an!
constructors.
< TOP >
)& An a""let is not able to access the files of the com"uter it runs on
< TOP >
+& A To instantiate M$lass2 $ou woul! write M$lass mc 3 new M$lass/0'
< TOP >
!& A ;$te co!e is the co!e generate! b$ a ,a-a com"iler.
< TOP >
,& +hen all references to an ob,ect are gone2 the memor$ use! b$ the ob,ect is
automaticall$ reclaime! is calle! as Xarbage ollection.
< TOP >
1
&
; @i-e must be a metho!
< TOP >
11& ; 41n the %oo"5 is "rinte! as A times.
< TOP >
12
&
A The !efault -alue for the elements of an arra$ of integers is 0.
< TOP >
1$
&
is the right o"tion for
< TOP >
1'
&
A +hile statement is equi-alent to while/xBL0
(
?3xD7'
YDD'
*
< TOP >
1(
&
the -alue of xJAK is 10.
< TOP >
1)
&
i310 is "rinte! b$ the statement on line A.
1+
&
The length of the arra$ is 7.
< TOP >
1!
&
@ olorJK "alette3new olorJ6K'
< TOP >
1,
&
@ The signature of the metho! is "ublic intJK castTo1ntArra$/!oubleJK a0.
S"ecific to ,a-a2 The signature of a metho! shoul! contain more !etails. 1t
shoul! contain
1. >isibilit$ mo!ifier /"ublic2 "ri-ate2 "rotecte!0
A. 9eturn t$"e
3. Wame of the metho!
F. Arguments /t$"e2 or!er0
G. Throws clause.
< TOP >
2
&
ontinue transfers the control flow to the u"!ate statement of the for loo"
< TOP >
21
&
; A break sen!s execution imme!iatel$ to the en! of the switch statement.
< TOP >
22
&
:nl$ P0Q is not of t$"e string.
< TOP >
2$
&
; The list of all of the "ublic fiel!s an! "ublic metho!s of the class.
< TOP >
2'
&
olor.re! olor.re! is the correct or!er.
< TOP >
2(
&
@ The t$"e an! -alue of the abo-e ex"ression is !ouble -G.0
< TOP >
2)
&

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 >

Vous aimerez peut-être aussi