Java Write statements from the client perspective to accomplish the following ta
ID: 3883025 • Letter: J
Question
Java
Write statements from the client perspective to accomplish the following task.
Remove duplicate Strings from a BagInterface<String> called myWords
You can use a second, initially empty BagInterface<String> object, called tempBag, if it helps.
For full credit, do not use the toArray() method. Do not use an ArrayList object.
The purpose of this question is for you to practice using the methods of the BagInterface class, not to practice manipulating arrays and Array Lists!
As an example, if myWords initially contained:
hi, hello, hi, bye, hi, bye, hello, howdy
Then after the method completes, myWords will contain:
hi, hello, bye, howdy
Explanation / Answer
Please find my code.
Please let me know in case of any issue.
// getting current size of bag
int size = myWords.getCurrentSize();
// iterating until myWords gets empty
while(size > 0) {
// removing first element from bag
String item = myWords.removeEntry(0);
// if it is not in tempBag then add to it, otherwise ignore it
if(!tempBag.contains(item)) {
tempBag.add(item);
}
size = myWords.getCurrentSize();
}
// now adding all tempBag items in myWords
size = tempBag.getCurrentSize();
// iterating until tempBag gets empty
while(size > 0) {
// removing first element from tempBag
String item = tempBag.removeEntry(0);
// adding into myWords
myWords.add(item);
size = tempBag.getCurrentSize();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.