You can’t learn to ride a bike just by reading about. You can’t become become a good programmer by simply running other people’s programs.
Please watch the recording of the Week 1 Lecture where this code was explained and write the code on your own. The code on this page is given only for ease of reference.
If you have unresolved questions, please ask Gayan via the relevant forums in Canvas->Discussions.
/ n y h le el ed t h ica ion s d h a E i s ject po v x s g Opt nP e / o e y b o k o java c d o mu om n s x i ng w t t e c oe n p l . C m un at y r te tio . / s i h a lica on a s. h l s o Song obj ts i s usicLi ry { A mm t e t m r a ables n ex l in eir p se in the bl c / For v ry r e er i c uc or, c ate a p vate m r r a in tha sa e m . pr e n xN m n / a m cap ty of th Song a r y n is Ther c n be o h r i por a d a hat a lass m g and ne d t / creat m mber v riables or se ri a n ] son i / ng a priv te rr n NumSon rr n y nu ber of songs // emb r r bl s mus be p i at a non s t c un e s the se r q e //- — – —— – —- — — – / f g th on t u to f th M si Libr y as ote h w i is m t o h t h s the e n me a he cla h i ‘ n _ d it l o no et typ o v v h str ct s e ethod run when a ec f h s i re te . / The uc or s u d f e g up ” n o hav _ p r_ b o / th class / Th ar e er in h onstru o sh uld a at mi imum r ci i o m i i nee d / s i up h s ss. . . “Yo ca eat m sic b r wi h ut n in how m so s t n stor li sic r r (int axN m gs) / W e hav m rt /c i i fo m tio co ng e te s f h ns or, / e m g t th . // Wh r fe r n to the b c m ber a les, yo mu t w s t i . is dot” t i . ax u S s x u g / E e t u e o ha p met ic val e for t e oth m er v r l th o st m s nsur th t n me v iab e oe uni t a ised . gL st = new S g[this a mS gs / r a e n ar apa le o old n So gs s o e s is. r tN on s = ; / h w the he us r so ey ca us e s st sh w () r turn / omm n i m i eth d’ e n stat nt } // T s o Me m o , i lay a nu a l the ser to o er t e m rary T e t, t e n cance . p oi en { / he lo g s r ng ntai s h m nu o i s St ng men =”Men n . Ad a s ng n2. p son s\n3. Pla song at n”; // Ta h use ‘ me ec n. St ng o c =JOp o ane. owI pu D lo ( ; / sp y e m nu until h er o e t ex bu s le t g anc ” // M us o y i hile (cho ce =nu { int um Intege p rs c ic ); i Num= ) { // Add n io tr = Opt o Pan o In D g(” so g t e” tr ng ocat o =J ptio ane w ial te l ca on d ong(t l o o } else f ho ce um ) / D a ngs p o di pl s( l f (ch eN = 3) { // P a ong at tio / t n n x n a s i t a for g n i t i x nt g a I t J pt ane n u l En er n i de )) i ( y ngA I e (i e ) = f { J p i n n owM e a ( u l “No su h so ” } } Ta n elec on . c o ce Op i n w n a og men et rn; / S e om e in in m h d t n st m nt / he et od el add ong o h so L st b ati w n o ec / ba d n th t oc ion a se n via t ar ter . I n / d th g, h e roo i the r y. p b ic o d a S n ( t g t l , r ng l ca n { / C cit e a r y efore c e n the ec . ( hi re tNumS g hi a u S n ) { h s i this. u r Nu S = ne on tl ati n); his urre N m ong + 1; u r n nu r o s ngs i rem nt . } r n e ommen i ma n e d r urn ta e n } / e me od bel d p y l on in th o gList pub void disp aySo ( n 0; e oo e t s thr u indic s r l ta i g obje ts. / e w h l n ti cr es n t e lo co ti es d w e i en . / Y u must i p n t n lik t instea f s n bre , on i u e c. ile ( s u tNu S s) { ng the a c s rs/get e h ds f ea ong o i ve ir i s nd o t ons tem o printl i + “: ” + is. on L [i .ge tl () s son Lis [ ] tLocati n )); i += ; } et / S o nt ma method re rn sta ement } // Th m o elow play s g t th in if d by t e par eter / u n is a ai ab . u l ole n p on Index in e ) o ea s cce ful = f l / Ou m // Ch c s h i dex s i ie the r ng of a d ngs i i x 0 & i < u ren mSongs) // D p ong e g p ayed Syst m.o n ln " la + i + " " so t[ d . et i l ( ," + th . ong is n e ].g Loc ion ) ; / follo n w s ove n tro T o I 's rn m nta La nch h d p yer f h O f e ' oca n tri g o = stem get p rt s. oL ase( ; tr ng omm nd if ( .c i s " n o ") c mman explo er "; se f os. on a n (" inu ) c mm "xd - e "; e m nd="o " tr { Ru t m . t ntim () e ( omm \ t is.song is [ e ]. t at o \ ); } a c Excep e){ S . r.printl (" e to a nc p aye ; cce s u ; // S so g wa oun } t r s cc ss ; } / et r s t d x of h firs tha ha th t g 't le in t i e. pub i in tSon dex t g itl ) { in es lt - ; // ni ali re u t / o t h ll a de u i her s a h. nt i 0 i t i . u e tNumSon && ! h s.so L st[i etTit e ).equ gno e s (t e i += 1 // If w r her , e re u s de o t p w e s ithe w av ch d / t e d of he r en y dde l s or .. / w f und a at h. c if s the a ter f hi .c r n gs) e t ; / i is h nd x o th un son i t e o g t a ay } e n r su ; h i th m i try p int he ic . // nl t a l c i h h v ma n m thod. T e othe / er" class sho no a e a ma n m d // T s u d a b th nly m t on i e ir E l p ro ec u t l the // n r i t l u th r i e. B c us st t c i ke the i bj ct-orie te . p c st c vo d a n S ri r s { // e m n me o us only re te n o ec he pl i n cl (t t i n) // o t at t c tr , etc of t e application nd e u th fu a p i ti n. c rary m = us cLi r 100 ; r t rn; // n t spir t f oi n s agh o e w lw ys r tu n // s a h e of th m h d A d s w b th y r t rn s a ent of t et o . W eq ire to ollo h s styl u les o er is i t u t d / r ak and co ti lso lead t pag tti od an mu b avoi d S st m. xit. virt l y el m a re-u i o our an u b id . } }
/ O l t e fi s r at o t e appl t houl be in t e s me cl p e pro im rt ja a . win .J io an ; / F r ev r l c f o e, y u st add c me t e pla ni ha h blo k d s, //i lain Eng ish o m ic e ou in n ns / Thi s t e pp ti cl s T is ho d a list f ec . publ c cla s M bra // dd co en s n x to i po tant v ri a d p a th urpo o k / e e pa am t n the onstr t re ri embe va i ble // t m na e ivat i t ma u So gs; / The m ximu aci e r a , so gL t // e a t e m t nt at t c i ht hold we e o / e e a f the . p v te So g[ gL st; / So rray a int cu e t gs; // Cu e tl added m All m e va ia e t r v e nd - ta i l s o rwi e uest d. - - -- - - --- ---- - -- / The ollowin is e c s r c r o e u c ar cl s. N o t a e h d // t a a sam a s t ss t at t s i an _ a s has r urn e, n t e en oid. // T e con u or i th m that's n obj t o t e clas s c a d / constr t i se or s ttin things" eeded t e a pro e o ject f / is . / e p am t s t e c ct r o s y wh n /c u al nf r at on s de for / ett ng t i cla // E g u n't cr e a u li ra y t o k ow g any ng i ca e" pub c Mu Lib a y m u Son { / hen w e i po ant ruc al n r a n mi via th parame r o t e co tuct / w ust "han on o em" en e r i g o je t em v riab u s al ays ay h s "th . h s m N m ong = ma N mSon s; / v n ho gh w d n't ve ara r u s h er emb a iab es, // e c n ructor u t e e a o mber ar l g s n i l . this son i on .m xNu on ]; / C e t a ray c b f h i g maxNum n ong bj ct th cur en umS g 0 / S o menu to t e th n e th y em o Menu ; e ; / See c e t n a n m o r tur eme he h w nu eth d d sp s me nd a lows u p ate h usic lib . // o xi h y ca l ublic v d showM u() / T fol win t i co n t e e pt on ri u u\ 1 d o \ Dis lay g y \ ke t e r s nu sel tio ri ch i e ti nP sh n t ia g menu) / Di la th e t e us cho s s o it t e c in "c el . ust e nl wh le loops. w i ! ll) choiceN = r. a eInt( ho e f (choice =1 so g opt n S ing title J i n e.sh w put ialo Enter n ti l ); S i l i n O nP .sho InputD og("En r song o ti "); a dS it e,l cati n); i (c i N ==2 { / ispl y so o ti n s aySong ); } e se i oic um = l y s op n / Ge so g i de a d p s t o relev nt method playin the so g n nde =I e er.p rse n ( O ionP .showI p tDia og(" t so g n x" ; f pla So t nd x nd x = alse) O t o Pa e.sh essag Di log n l , c ng! ); // ke ext s ti . . h i =J t o Pane.sho I putDi l ( u); } r u / e c m nt ma et o ' re ur ate e } / T m h b ow s a s t t e ng i y cre ng a ne so g bj t / se o e itle and l at p s d i he p ame s t o ly / ad s e son if t er 's m n a ra u l v i dd o g S rin it e St i o tio ) / heck capa y of th r a b r ati g obj t if t s.cur n on s < t s.m xN m o gs t i .songL st[ c r ent m ongs] w S g(ti e, loc o t .c nt u S s = // C r e t mbe f o s inc e ed etur ; // S e c t n i m tho ' et s t me t / Th th ow is la s a l s gs e s n lic l ngs ) { i t i = // Th l p it ra e o gh the e cur ent y con in ng son c / Not ho t e oop co di on des ib whe h op n nu an h n t ds / o wr te loo co di io s e his d o u i g ak c t n e, t wh i < thi .c rren m ong // Usi c es o m t o o ch S t retr e the t tle a l ca i Sys . ut. n( th s g ist ] tTi e + "," + thi . g t i .ge o ( 1 r urn; / ee c mme in in ' tu t e eth d b s a on a e dex spec ie h am , / if s ch a so g v l le p b ic bo a layS gAt ( t ind x { bo l n u ss a se; / tco e e k if t e n pec f d is a e dde so f ( nde >= & ndex this.c r tNu { is lay the s b in l . e ut.pri t ( P ying ” ndex : + this. ngLis in ex] g T t e ) + ” is s L t[i d x et at ( ) / The wi g a not c red i In o Pr g. t o a e l // u t e efault la o t e S or th l tio ‘ S n s Sy . Pro e y(“o name”).t owerC ) S i c a ; os onta n ( wi d ws ) o d=” r el i ( c t i s l x”) o and= g op n els co ma pen ; y n i e ge Ru e . xec c and+” “”+ h L t ind x ge Loc i n()+” “” c t h( tion ystem er n Unabl l u h l r”) } su s ful = tr e ince n s f d re u n u e ful / R u n he in e t e t song t s e s rin it ‘ i s t tl l c t ge gIn (S rin t e t r u = 1 I ti sed s l / G hroug a d d songs nt l t e’ m tc i = ; while ( < h s c rr n gs t i ng i ].g l ( alsI r Ca e itl )) ; e a e e w a o t i f he loo , hich m an e r e h e rea e / he n t cu r tl a d song i t . / e o m c Che k it' l t ... i (i < t s ur e tNumSon { r sul =i / Th s t e i e f e fo d g n h s n Lis rr r tur e lt } // T is s e a n en o to t appl ation O y he pp i at on class s ould a e a i e h r "small es uld // t h v i etho . his ho l lso e e o en i of stat c in your nt e c i se p j t n i i st uct ons e l yo o e w s e a e a i s li oppos te of o e n d ubli ati i m i ( t ng[] a g ) Th ai th d m t c a a bj t of t ap icat o ass ha t's i s h he ons uctor . h class a "fir p" e ll // p l ca o Musi Lib l new M i bra y( ) e u I he i o av di g ' p etti c d ' we ill a a add a e r t tement at t e nd e et o . n thi ill e e onl e u t tem // hat m h d e r u you f w t i e n s th w e ns r c e . / b e n nue a o s he c e d st e de . // y e e .. ua l i in tes the sab lity f y code d m st e avo ed
Sorry, your browser does not support inline SVG.
// Onl t e l s ted to t e pl c i n h u b in Ec ipse ro ect // e g cla s e to e e ent si l s g ( b t t c rary // ote h w e cla s ame r i i gu v th u h w w l m ny ma ong h li a y / o e ee h s ner co nen c s g ne a es no out ts. hi lo th Mus b a o de ide how the u r f ce h n le . / a the u br ry s co m t st. h r x a ti s r n t add d h / re uc tte ubl cl ss o vat i t l / Song' tle r te S ri l ti n Location w ic an be pa r a RL pr a in a g; / S n .5 he o o in con u t r s n le a d l c n as a m imu p b S ng( ing i le, t g oca o { t .t e= e i .l cation= ca ; i .ra i 1; ur W us not h v a th c n uc s sp c lly one th t d ' ake imp nt/cru i / m u eded fo aki obj c of his cl ss If n i v i bje is e t d / o a ck s ruc rs r ue a o t ct r de ig t at s the r g am e fa t / p b i S () { / //} // e c so m o or i e me r a i bl . publ c S e Tit ( // S ing t l // his li e ad d to u e h mp ta t is. // Y u must a wa s y his "t is do w en ref r i g t me er v i l ec u //if s me ne a s a l ca v r l / ame r b h a na e t you d ill b k. etur t .t t e; / so for l c t n public S e ca on e urn t s i n } set ut t r me hod fo rat ' e a iable. // T mut i a m c ns ru tor ich u ed o et a ne u / t a e vari l . I s d n r a e a p r meter i m o tant for t m thod u e y m m er ari l t o ifi s do ge b k . b i o d setRa n t ){ t o rec a i ; / Th ria le i us d o sto e t or cted tin ec se / e mus ot m dify h lue ' st d in p ram t r .. // We wan e ig al o t e par e o t ere w e / a ot e p g am er m t wa t e in t u ur f r t < / ti g oo l w a e t t or ect i g= lse i r ti / R t g o ig a se to 5 rrec d a ng=5; ls / he i se r ing ec co c Rati r i g s r rect ti g re A c s r m h o t r ng m r r b e. p blic i g ting { u h .r t ng }
y h fi e rela h ap i at o s o ld e the same l p j Th Son s is us d r pr s a ng e on o jec ) in he musi lib N o th s n he e s s n lar e en o g e i l add a ny s s to t e br r / N t 2: S how t i in mpo t la s e r t pu T s al ws e icLi r ry t // c se inter a is a d d / Ple se refer to M sicLi a class' m en s fir Teac e 's e pl na on a e o e ere / to d e clu r. p ic a S ng { pri e Str ng it e; / s ti p iva t ng oca o ; // h h c a th o U iv te t r tin / ong's rati g 0. // T f ll w g str c o takes a o g tit n a o atio in m. u lic o Str t t S rin l ti n) his itl titl ; th s o lo tion th s t ng=- ret n; } // e m t a e ny o er o str tor , e e ia s a on t t orta ic al / inim m info ne r m ng an e t t a . a n al d o ct cr a e / due t la of con t to o d a b d c ns ru o s n, h ' p o r m r's ul . / u l c ong / g t/a ces r eth d f t tl mbe v r a e .. i tring g t le ) { tr it e; T n was de ill strat t e i or nce of " h " o l ys a "t ." h t" h e r n o mb ar ab es b a se o o dd o l a iab e par te y t e s me m la er, r co e w rea r n his i l } / Acces r o a io tring g tLo ti () { r t hi .locat o ; // /m a o t r ' ing memb r v r he ator s like ini o t c wh is s t s w val e / o m mber ab e t houl ge e ally t k a a . // It s i p r mu ator e s to ens r that an e b v ab es i m d e // n't t ro en pu l c v i ti g(int ra ing in c r tedR t ng / is va b s e t r he c re ra g b au / w t n o t e va that s ore a a e e . t th or inal v ue f h amet r t be h h n / n h r ro r m igh nt o us it he f t e. i ( a ing 0) / Ra n s t o r se o 0 c r edRat n 0; e f ( a ng>5) / a in s to h h re t co te R ti e e / Ot rw se, u at dir tly. rre ted ng= at n ; thi . ating=cor edRa n ; turn; } // c es o et od f he ati me be va ia l u nt etRa () ret rn t is a i ; }
Sorry, your browser does not support inline SVG.