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