m

advertisement
Q1 Review Quiz 1 Q1
Searching in a 2D array ÊQJEarþl
Complete the given method, search, which
searches for a wanted value in a 10x10 2D
26Lø141822X.5913
ls 2ø ø 5 Lø 15 2ø ø .5
3 9 15 2L 2 8. t4 2ø L
16235L2L91815224
4L22ø311L92Lø18L
LTttøLg3L22LsL423
515øLø2ø5lsø7ø2ø
184!51,122392ø617
6185L7416315214
t972ø821922tø23tL
Enter a value to search: !
array.
The search should be done by rows; ie. start
from first row (ie row 0) , from column 0 to
column 9; then row 1, row 2, .. etc..
If the value is not found, ouþut
otherwise,
@8
...---->
onlyoutput@
m
M
M
First locatÍon:
where it is found, as shown in the sample
rundown.
Lø
7
(row Ø, coJ. 7)
Main. ava
public static void seanch(int
{
.,(Á,"
b*l,rs,
@
i,¿(
[][] arr, int
wanted)
{"-J
"ora
,l
col;
aa
.ft,, C 1^[
Wo, l<
&- Ctt @o;
v
n
Nole:
+r)
r*+)
ar
'î1 (*vellJtxJ'--
&&!found
+æ+& ,*r'*i
t
.n{. )
) ¿4zr)
Tricþ * Good
Short * 6ood
Less vorioblø * Good
We focus on:
.
Givíng correct code
(confidence insteod or risksduring coding)
. Eosyto read / debug / modify / reuse
. Leost trops when chonge / reuse in the future.
Neutrol words like'fiog". "check etc.. o?e bod nomes:
connot tell $rhot théir volu¿s m¿on.
E9. "check==tru¿" tn¿ons OKor Problen??? Don'l knowl!
"found:=lrue" is precise inmeaning. r'r'
ì+crû%)
Syelan.øl .p"t tl*(':^,"t
¿,t+c
93s{*,.-^t.f*-ú+¿" çrrtt
{';);
!,,áø"' ( o"' ã4'
co("bá)",
)
public static void main(String[]
{
angs)
int anrl][];
. . //Set up the 10x10 2D array and its contents [You do not need to do this part]
. . //Display the 2D array [You do not need to do this part]
/ lSeaech
Scanner in = new Scannen(System.in);
System.out.pnint("Enter a value
int x = in.nextlnt0;
Ì
i
in. close
O
j
to search: ");
&r&r,
Q2 Review Quiz 1 Q2
Enter year and month (eg. "2øt3
Array of Days t20¡arksl
Thene ane 29 days
t2")i
2ø72 2
in the month:
Add required code to complete the given
progftim which lists all days in a month,
as shown in the example:
Note/Hint:
Your solution should involve at least
one method in the Day class.
h'rfu'\äh
29 Feb 2øt2
Main
public static void main(Stning[] args)
{
System,out.print("Enter yean and month (eg. \"2ø13
Scanner scannenobj = ne¡, Scanner(System.Ín);
int
int
ava
12\"): ")
y=sç¿¡¡s¡0bj.nextlntO;
m=scannenObj.nextlntO;
Day[] dArnay;
2 Marks)
-
¿ Awa.tr
System.out. printf("\nThene are %d days
fon (Ínt Í=0;i<dAnray.length;i++)
System.
scannen0bj
.
out. println(dAnnayIi]
close(
)
in the
month
:
\n",dArray.length);
j
);
)
ava
@
Marks
( ;^t
îht,'ù
S{otÈ p^!b?" Þ"1Ef cnelæa1â* 6 É\ ),
î¡,'t tÞtÞ = .-.r.dury s (r,
I
totD I
Þ%[
rt@
^))
*r.rCj
Ya,ô
f*
Cr¿ î=o )r
aY, f;3
( totD ; itt )
úcþ
: n4¡r DA(Y , rn, îtl)
F(e¡;b[¿
@
Sø*
r¡¡¡
z.zto
4i
fett rtn' ú( )
@
3
;^f
sdo"tre P r¿vafu-*3 ü,
l-t
r
{.t
whik Aj¡aú,44¿gæüD
Å-..+
to\'--z
,
re"h*.-,r
td;
-
¡1"{
j.
"D
ôu¡hen rfm,
*n.rat,
ú,¡,..¡¡yrú
3"a
,,gnp
Q3 Review Quiz 1 Q3
[0.
] mark; (partial mark up to 0.05 may be given for pafüaI correctness / improper style O)]
Rewrite your method for Ql
such that if the wanted value
appears 2 times or more, it
clearly shows the first and last
locations where the wanted
Sample rundown #1 (only one locat¡on)
Enten a value to seanch: L3
Found location: (now Ø, co1 9)
value is found.
Sample rundown #2 (2 locations)
Reminder: lf
valueis not
found, outntlnor r9yg¡D,
þ
Enten a vaLue to seanch: 7
Found locations: (row 2, co1
9) and (now 9, coI 1)
Sample rundown #3 (more than?locations)
Enten a value to seanch: 22
Found locations: (row Ø, col
5)
(now
9,
co1 6)
Main. ava
public static void seanch(int [][] ann, int
wanted)
{
c"*^
-trnÂ
--o )
(1, ( 2,
Ë&
i r4^
4er ( ìh"Î r
cl
,QL]
=o
',
{-(¡,¡ c=o) "
I'
-'t(, o" t'lc e] :: UJat"f¡{)
-n
ìf ( --d -=o ) l rt :r; ct'-c-i\
12: rr. r*L=C)
Cr-'^^t
)
+t ;
)
,+( Co'\rt 7! --=O )
Çr<"-- --J
J r
-_I
)
,t-t
,L
I
1-*
(_
n O-{
4'^¿
')
r,
¿lq*
L( *
)
1
(
)
93"t^.<''J.no
".-ff ("
¡.^.o(Å,
(1,{\,
'"tIA)
þt,.sf !,
r&ti',
cl ,, cL ))
a
J
Download