Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Assume statesBag is a type of BagInterface<String>. What will print after the

ID: 3880246 • Letter: 1

Question

1. Assume statesBag is a type of BagInterface<String>. What will print after the following statements are executed:

System.out.println(statesBag.isEmpty());

statesBag.add("alaska");

statesBag.add("california");

statesBag.add("delaware");

statesBag.add("california");

statesBag.add("georgia");

statesBag.add("delaware");

statesBag.add("delaware");

statesBag.add("iowa");

System.out.println(statesBag.isEmpty());

System.out.println(statesBag.getCurrentSize());

System.out.println(statesBag.remove("california"));

System.out.println(statesBag.getCurrentSize());

System.out.println(statesBag.remove("california"));

System.out.println(statesBag.remove("california"));

System.out.println(statesBag.remove("hawaii"));

System.out.println(statesBag.getCurrentSize());

System.out.println(statesBag.getFrequencyOf("delaware"));

System.out.println(statesBag.contains("hawaii"));

System.out.println(statesBag.getFrequencyOf("hawaii"));

statesBag.clear();

System.out.println(statesBag.getCurrentSize());

2.

Assume cityList is a type of ListInterface<String>. What will print after the following statements are executed:

System.out.println(cityList.isEmpty());

cityList.add("burbank");

cityList.add("carlsbad");

cityList.add("davis");

cityList.add("granada");

cityList.add("sacramento");

cityList.add(2, "hayward");

cityList.add("menlo park");

cityList.add(4, "napa");

System.out.println(cityList.isEmpty());

System.out.println(cityList.getLength());

System.out.println(cityList.getEntry(2));

System.out.println(cityList.remove(2));

System.out.println(cityList.getEntry(2));

System.out.println(cityList.remove(1));

System.out.println(cityList.remove(2));

System.out.println(cityList.remove(3));

System.out.println(cityList.getLength());

System.out.println(cityList.replace(2, "oakland"));

System.out.println(cityList.getEntry(2));

System.out.println(cityList.getEntry(3));

cityList.clear();

System.out.println(cityList.getLength());

3.Write statements (does not need to be a complete method) 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

----------------------------------------------------------------------------------------------------------------------
1)

System.out.println(statesBag.isEmpty()); -> True

Bag Contains : "alaska" , "california" , "delaware" , "california" , "georgia" , "delaware" , "delaware" , "iowa"

System.out.println(statesBag.isEmpty()); --> False

System.out.println(statesBag.getCurrentSize());-> 8

System.out.println(statesBag.remove("california")); -> True
Bag Contains : "alaska" , "delaware" , "california" , "georgia" , "delaware" , "delaware" , "iowa"

System.out.println(statesBag.getCurrentSize());-> 7

System.out.println(statesBag.remove("california"));-> True
Bag Contains : "alaska" , "delaware" , , "georgia" , "delaware" , "delaware" , "iowa"

System.out.println(statesBag.remove("california"));-> False

System.out.println(statesBag.remove("hawaii"));> False
Bag Contains : "alaska" , "delaware" , , "georgia" , "delaware" , "delaware" , "iowa"

System.out.println(statesBag.getCurrentSize());-> 6

System.out.println(statesBag.getFrequencyOf("delaware"));-> 3

System.out.println(statesBag.contains("hawaii"));-> false

System.out.println(statesBag.getFrequencyOf("hawaii"));->0

statesBag.clear();

System.out.println(statesBag.getCurrentSize());->0


----------------------------------------------------------------------------------------------------------------------------------

2)

System.out.println(cityList.isEmpty()); -> True

List Contains : "burbank" , "carlsbad" ,"hayward", "davis" , "napa", "granada" , "sacramento" , "menlo park"

System.out.println(cityList.isEmpty()); -> False

System.out.println(cityList.getLength()); ->8

System.out.println(cityList.getEntry(2));->hayward

System.out.println(cityList.remove(2));
List Contains : "burbank" , "carlsbad" ,  "davis" , "napa", "granada" , "sacramento" , "menlo park"

System.out.println(cityList.getEntry(2));;->davis

System.out.println(cityList.remove(1));
List Contains : "burbank" ,  "davis" , "napa", "granada" , "sacramento" , "menlo park"

System.out.println(cityList.remove(2));
List Contains : "burbank" ,  "davis", "granada" , "sacramento" , "menlo park"

System.out.println(cityList.remove(3));
List Contains : "burbank" ,  "davis", "granada" , , "menlo park"

System.out.println(cityList.getLength()); -> 4

System.out.println(cityList.replace(2, "oakland"));
List Contains : "burbank" ,  "davis", "oakland" , , "menlo park"

System.out.println(cityList.getEntry(2)); ->oakland

System.out.println(cityList.getEntry(3)); ->menlo park

cityList.clear();

System.out.println(cityList.getLength()); -> 4


----------------------------------------------------------------------------------------------------------------------
3) public static void removeDuplicates(BagInterface<String> myWords) {


  BagInterface<String> tempBag = new BagInterface<String> ();

for(int i=0;i<myWords.size();++i){

           String str = arr.get(i) ;

boolean isPresent = false;

for(int i=0;i<tempBag.size();++i){

if(tempBag.get(i).equals(str))
{
isPresent = true;
break;
}
  }   

if(! isPresent)
tempBag.add(str);

       }


}