Question: I don't understand what this part of the code does its part of the Class Dictionary that uses some constants from Class Word . I

I don't understand what this part of the code does

its part of the Class Dictionary that uses some constants from Class Word. I have this explanation

[Explination part]

addString(arg) takes in the string arg. If arg is not yet contained in any element of theList, then addString(arg) creates a new instance of Word with a value count = 1 and adds it to theList. If there is already an instance of Word with a value of theWord equal to arg, then it increases its value of count by 1. Use only the methods listed in the class diagram of Word (dont implement a method that changes count). addString(String) should return the return value of toString() of the added or updated instance of Word.

I have a hard time hanging with this explanation i was given.

[This is the code part ]

public String addString(String arg) {

for (Word w : theList) {

if (w.getWord().equals(arg)) {

int count = w.getCount() + 1;

theList.remove(w); // what does this part does (remove?)

theList.add(new Word(arg, count));

return theList.get(theList.size() - 1).toString(); // why the -1?

}

}

theList.add(new Word(arg, 1)); // what does this part do?

return theList.get(theList.size() - 1).toString();

}// End of addString

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!