Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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>list=newArrayList<>();//listofItems

/**Constructsinitiallyemptyfavoriteslist.*/

publicItemFavoritesList(){}//constructsinitiallyemptyfavoriteslist

//nonpublicutilities

/**Providesshorthandnotationtoretrieveuser'selementstoredatItemt.*/

protectedEvalue(Itemt){returnt.getValue();}

/**ProvidesshorthandnotationtoretrievecountofitemstoredatItemt.*/

protectedintcount(Itemt){returnt.getCount();}

/**ReturnsItemhavingelementequaltoe(ornullifnotfound).*/

protectedItemfindItem(Ee){

Itemwalk=list.first();

while(walk!=null&&!e.equals(value(walk)))

walk=list.after(walk);

returnwalk;

}

/**MovesItemtearlierinthelistbasedonaccesscount.*/

protectedvoidmoveUp(Itemt){

intcnt=count(t);//revisedcountofaccesseditem

Itemwalk=t;

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){

Itemt=find(e);

if(t==null)

t=list.addLast(newItem(e));

t.increment();

moveUp(t);

}

/*****Implementthismethod*****/

/**Returnsaniterablecollectionofthekmostfrequentlyaccessedelements.*/

publicIterablegetFavorites(intk)throwsIllegalArgumentException{

if(k<0||k>size())

thrownewIllegalArgumentException("Invalidk");

ArrayListresult=newArrayList<>();

Iterator>iter=list.iterator();

for(intj=0;j

result.add(iter.next().getValue());

returnresult;

}

//theremainderofthisfileisfortesting/debuggingonly

publicStringtoString(){

returnlist.toString();

}

protectedstaticvoidtest(ItemFavoritesListfav){

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions