Midterm 2 Solutions 13

Forstudentsthattriedrecursioninsteadofiterationacommon

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Nm) ho e leaSaexeto "ulct ae urn.yae; } alae.d cretmNm) lNmsad(urn.yae; Ieao<mea tr=cretmCide.trtr() trtrAob>ie urn.yhlrnieao ; wie(trhset() hl ie.aNx ){ hle ie.et(); epr(trnx ) } } / apeSlto / * Sml ouin3 * / oe hsslto ol anol . u f5 ic trn * Nt:Ti ouinwuder ny25oto ,snei us ntm rprinlt ­qae. * i iepootoa oNsurd / pbi odiO ulcvi sK(){ AobIeao tr lAobs() meatrtrie1=almea ; wie(tr.aNx ){ hl ie1hset() Aob 1=ie1nx ; meaa tr.et() AobIeao tr lAobs() meatrtrie2=almea ; wie(tr.aNx ){ hl ie2hset() Aob 2=ie2nx ; meaa tr.et() i a =a){ f(1! 2 i a.yaeeul a.yae){ f(1mNm.qas(2mNm) trwnwIlglttEcpin(dpiaenm "+ ho e leaSaexeto "ulct ae: a.yae; 1mNm) } } } } } Common Mistakes: One of the most common errors was attempting to hash Amoeba objects, which doesn’t work since we have not overridden Amoeba’s hashCode. Instead, you should hash their names directly, because String does have an overridden hashCode. Some students still had troubles instantiating an iterator or differentiating between the AmoebaFamily and AmoebaClasses. The method you have to write is in AmoebaFamily, and so you can call the method allAmoebas() which returns an iterator over all Amoebas in the tree, which greatly simplifies the problem. You can also just construct a new AmoebaIterator() since AmoebaIterator is a public class inside AmoebaFamily. You cannot create an iterator over all Amoebas in the tree from a single Amoeba; however, you can create an iterator only over its immediate children by taking advantage of ArrayList’s iterator, which is what you needed to do if you attempted a recursive solution. For students that tried recursion instead of iteration, a common mistake was to not pass your HashSet of seen names through calls. If you don’t pass it in then you won’t be able to check against the names you’ve seen in other calls. An alternative solution to this problem is to make the HashSet an instance variable of AmoebaFamily, so all your recursive calls can access it (provided the recursive calls are not sta...
View Full Document

Ask a homework question - tutors are online