Question
I need to define the ItemFsvoritesList class which uses Java's ArrayList as underlying storage. I wrote my code which its output is different from the
I need to define the ItemFsvoritesList class which uses Java's ArrayList as underlying storage. I wrote my code which its output is different from the output it should be. Please see below. Which part of my code needs to be modified?
Here is my code:
importjava.util.Iterator;
importjava.util.ArrayList;
/**Maintainsalistofelementsorderedaccordingtoaccessfrequency.*/
publicclassItemFavoritesList
//----------------nestedItemclass----------------
protectedstaticclassItem
privateEvalue;
privateintcount=0;
/**Constructsnewitemwithinitialcountofzero.*/
publicItem(Eval){value=val;}
publicintgetCount(){returncount;}
publicEgetValue(){returnvalue;}
publicvoidincrement(){count++;}
//debugutility
publicStringtoString(){return"("+value+":"+count+")";}
}//-----------endofnestedItemclass-----------
ArrayList
/**Constructsinitiallyemptyfavoriteslist.*/
publicItemFavoritesList(){}//constructsinitiallyemptyfavoriteslist
//nonpublicutilities
/**Providesshorthandnotationtoretrieveuser'selementstoredatItemt.*/
protectedEvalue(Item
/**ProvidesshorthandnotationtoretrievecountofitemstoredatItemt.*/
protectedintcount(Item
/**ReturnsItemhavingelementequaltoe(ornullifnotfound).*/
protectedItem
Item
while(walk!=null&&!e.equals(value(walk)))
walk=list.after(walk);
returnwalk;
}
/**MovesItemtearlierinthelistbasedonaccesscount.*/
protectedvoidmoveUp(Item
intcnt=count(t);//revisedcountofaccesseditem
Item
while(walk!=list.first()&&count(list.before(walk)) walk=list.before(walk);//foundsmallercountaheadofitem if(walk!=t) list.addBefore(walk,list.remove(t));//remove/reinsertitem } /*****Implementthismethod*****/ //publicmethods /**Returnsthenumberofitemsinthefavoriteslist.*/ publicintsize(){returnlist.size();} /**Returnstrueifthefavoriteslistisempty.*/ publicbooleanisEmpty(){returnlist.isEmpty();} /** *Accesseselemente,therebyincreasingitsaccesscount. *Ifeisnew,itscountwillbe1afterthisoperation. */ publicvoidaccess(Ee){ Item if(t==null) t=list.addLast(newItem t.increment(); moveUp(t); } /*****Implementthismethod*****/ /**Returnsaniterablecollectionofthekmostfrequentlyaccessedelements.*/ publicIterable if(k<0||k>size()) thrownewIllegalArgumentException("Invalidk"); ArrayList Iterator for(intj=0;j result.add(iter.next().getValue()); returnresult; } //theremainderofthisfileisfortesting/debuggingonly publicStringtoString(){ returnlist.toString(); } protectedstaticvoidtest(ItemFavoritesList char[]sample="hello.thisisatestoffavoriteslist".toCharArray(); for(charc:sample){ fav.access(c); intk=Math.min(5,fav.size()); System.out.println("Entirelist:"+fav); System.out.println("Top"+k+":"+fav.getFavorites(k)); System.out.println(); } } publicstaticvoidmain(String[]args){ test(newItemFavoritesList } } And here is the out put of my code: Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Entire list: [] Top 0: [] Here is the output which is should be like: Entire list: [(h:1)] Top 1: [h] Entire list: [(h:1), (e:1)] Top 2: [h, e] Entire list: [(h:1), (e:1), (l:1)] Top 3: [h, e, l] Entire list: [(l:2), (h:1), (e:1)] Top 3: [l, h, e] Entire list: [(l:2), (h:1), (e:1), (o:1)] Top 4: [l, h, e, o] Entire list: [(l:2), (h:1), (e:1), (o:1), (.:1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:1), (e:1), (o:1), (.:1), ( :1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:1), (e:1), (o:1), (.:1), ( :1), (t:1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:2), (e:1), (o:1), (.:1), ( :1), (t:1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:2), (e:1), (o:1), (.:1), ( :1), (t:1), (i:1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:2), (e:1), (o:1), (.:1), ( :1), (t:1), (i:1), (s:1)] Top 5: [l, h, e, o, .] Entire list: [(l:2), (h:2), ( :2), (e:1), (o:1), (.:1), (t:1), (i:1), (s:1)] Top 5: [l, h,, e, o] Entire list: [(l:2), (h:2), ( :2), (i:2), (e:1), (o:1), (.:1), (t:1), (s:1)] Top 5: [l, h,, i, e] Entire list: [(l:2), (h:2), ( :2), (i:2), (s:2), (e:1), (o:1), (.:1), (t:1)] Top 5: [l, h,, i, s] Entire list: [( :3), (l:2), (h:2), (i:2), (s:2), (e:1), (o:1), (.:1), (t:1)] Top 5: [ , l, h, i, s] Entire list: [( :3), (l:2), (h:2), (i:2), (s:2), (e:1), (o:1), (.:1), (t:1), (a:1)] Top 5: [ , l, h, i, s] Entire list: [( :4), (l:2), (h:2), (i:2), (s:2), (e:1), (o:1), (.:1), (t:1), (a:1)] Top 5: [ , l, h, i, s] Entire list: [( :4), (l:2), (h:2), (i:2), (s:2), (t:2), (e:1), (o:1), (.:1), (a:1)] Top 5: [ , l, h, i, s] Entire list: [( :4), (l:2), (h:2), (i:2), (s:2), (t:2), (e:2), (o:1), (.:1), (a:1)] Top 5: [ , l, h, i, s] Entire list: [( :4), (s:3), (l:2), (h:2), (i:2), (t:2), (e:2), (o:1), (.:1), (a:1)] Top 5: [ , s, l, h, i] Entire list: [( :4), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:1), (.:1), (a:1)] Top 5: [ , s, t, l, h] Entire list: [( :5), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:1), (.:1), (a:1)] Top 5: [ , s, t, l, h] Entire list: [( :5), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (.:1), (a:1)] Top 5: [ , s, t, l, h] Entire list: [( :5), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (.:1), (a:1), (f:1)] Top 5: [ , s, t, l, h] Entire list: [( :6), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (.:1), (a:1), (f:1)] Top 5: [ , s, t, l, h] Entire list: [( :6), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (f:2), (.:1), (a:1)] Top 5: [ , s, t, l, h] Entire list: [( :6), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (f:2), (a:2), (.:1)] Top 5: [ , s, t, l, h] Entire list: [( :6), (s:3), (t:3), (l:2), (h:2), (i:2), (e:2), (o:2), (f:2), (a:2), (.:1), (v:1)] Top 5: [ , s, t, l, h] Entire list: [( :6), (s:3), (t:3), (o:3), (l:2), (h:2), (i:2), (e:2), (f:2), (a:2), (.:1), (v:1)] Top 5: [ , s, t, o, l] Entire list: [( :6), (s:3), (t:3), (o:3), (l:2), (h:2), (i:2), (e:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , s, t, o, l] Entire list: [( :6), (s:3), (t:3), (o:3), (i:3), (l:2), (h:2), (e:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , s, t, o, i] Entire list: [( :6), (t:4), (s:3), (o:3), (i:3), (l:2), (h:2), (e:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, o, i] Entire list: [( :6), (t:4), (s:3), (o:3), (i:3), (e:3), (l:2), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, o, i] Entire list: [( :6), (t:4), (s:4), (o:3), (i:3), (e:3), (l:2), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, o, i] Entire list: [( :7), (t:4), (s:4), (o:3), (i:3), (e:3), (l:2), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, o, i] Entire list: [( :7), (t:4), (s:4), (o:3), (i:3), (e:3), (l:3), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, o, i] Entire list: [( :7), (t:4), (s:4), (i:4), (o:3), (e:3), (l:3), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , t, s, i, o] Entire list: [( :7), (s:5), (t:4), (i:4), (o:3), (e:3), (l:3), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , s, t, i, o] Entire list: [( :7), (s:5), (t:5), (i:4), (o:3), (e:3), (l:3), (h:2), (f:2), (a:2), (.:1), (v:1), (r:1)] Top 5: [ , s, t, i, o]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started