Menu
support@authoritypapers.com
+1(805) 568 7317

using the driver program below answer the followingquestions question 1 write a unio 4952048

Using the driver program below answer the followingquestions: Question 1: Write a union method for the ResizableArrayBag class. The
union of two bags is the combined contents of bothbags. Unions are explained in more detail in Chapter 1, #5. A union might contain duplicates. The method should
not alter either bag. Thecurrent bag and the bag sent in as a parameter should be the samewhen the method ends. The method header is: public BagInterface union(ResizableArrayBaganotherBag) Example: bag1 contains (1, 2, 3) bag2 contains (2, 2, 4, 5) bag1.union(bag2) will return a new bag that contains (1, 2, 3,2, 2, 4, 5). The contents of bag1 and bag2 would be the same. (The order of the union bag might not match the above, sincebags are unordered.) Question 2: Write an intersection method for the ResizableArrayBagclass. The
intersection of two bags is the overlapping contentof the bags. Intersections are explained in more detail in Chapter1, #6. An intersecion might contain duplicates. The method should
not alter either bag. Thecurrent bag and the bag sent in as a parameter should be the samewhen the method ends. The method header is: public BagInterface intersection(ResizableArrayBag anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains (2, 2, 2, 4, 5) then bag1.intersection(bag2) would return a new bag thatcontains (2, 2) The contents of bag1 and bag2 would be the same. (The order of the intersection bag might not match the above,since bags are unordered.) Driver program: import java.util.Arrays; public class Homework04Driver { public static void main(String[] args) { // setting up an ArrayBag to test System.out.println(“TESTING BAG”); ArrayBag numbersBag = newArrayBag(); numbersBag.add(1); numbersBag.add(2); numbersBag.add(1); numbersBag.add(4); numbersBag.add(3); System.out.println(“The bag contains[1, 2, 1, 4, 3] ntt” + Arrays.toString(numbersBag.toArray())); // Q3 replace method // NOTE: The description does not specify that you must replacethe last // element in the array, so your output might be different-that’s okay System.out.println(“n***Q3***”); numbersBag.replace(6); System.out.println(“The bag contains[1, 2, 1, 4, 6] ntt” + Arrays.toString(numbersBag.toArray())); numbersBag.replace(8); System.out.println(“The bag contains[1, 2, 1, 4, 8] ntt” + Arrays.toString(numbersBag.toArray())); // Q4 removeEvery method // NOTE: bags are unordered, so the order of your array mightbe // different- that’s okay as long as the contents match what islisted below System.out.println(“n***Q4***”); numbersBag.add(1); numbersBag.removeEvery(1); System.out.println(“The bag contains[8, 2, 4] (any order)ntt” + Arrays.toString(numbersBag.toArray())); numbersBag.removeEvery(3); System.out.println(“The bag contains[8, 2, 4] (any order)ntt” + Arrays.toString(numbersBag.toArray())); // Q5: union System.out.println(“n***Q5***”); ResizableArrayBag firstResizableBag = newResizableArrayBag(); firstResizableBag.add(8); firstResizableBag.add(2); firstResizableBag.add(4); firstResizableBag.add(5); firstResizableBag.add(6); firstResizableBag.add(2); ResizableArrayBag secondResizableBag = newResizableArrayBag(); secondResizableBag.add(3); secondResizableBag.add(1); secondResizableBag.add(2); BagInterface unionBag = firstResizableBag .union(secondResizableBag); System.out.println(“Bag1 contains [8, 2, 4, 5, 6, 2] ntt” + Arrays.toString(firstResizableBag.toArray())); System.out.println(“Bag2 contains [3, 1, 2] ntt” + Arrays.toString(secondResizableBag.toArray())); System.out.println(“Union Bag contains [8, 2, 4, 5, 6, 2, 3, 1,2] (any order) ntt “ + Arrays.toString(unionBag.toArray())); // Q6: intersection System.out.println(“n***Q6***”); firstResizableBag.add(2); secondResizableBag.add(2); secondResizableBag.add(4); BagInterface intersectionBag =firstResizableBag.intersection(secondResizableBag); System.out.println(“Bag1 contains [8, 2, 4, 5, 6, 2, 2]ntt” + Arrays.toString(firstResizableBag.toArray())); System.out.println(“Bag2 contains [3, 1, 2, 2, 4] ntt” + Arrays.toString(secondResizableBag.toArray())); System.out.println(“Intersection Bag contains [2, 4, 2] (anyorder) ntt “ + Arrays.toString(intersectionBag.toArray())); // setting up AList to test System.out.println(“nTESTING LIST”); AList produceList = new AList(); produceList.add(“banana”); produceList.add(“date”); produceList.add(“grape”); produceList.add(“eggplant”); produceList.add(“jicama”); produceList.add(“grape”); // Q7: getPosition // Note: this driver assumes you return -1 if an element is notin the list; // it would also be valid to throw an exception System.out.println(“n***Q7***”); System.out.println(“Position is 1: ” +produceList.getPosition(“banana”)); System.out.println(“Position is 3: ” +produceList.getPosition(“grape”)); System.out.println(“Position is -1: ” +produceList.getPosition(“mango”)); //Q8: moveToBeginning System.out.println(“n***Q8***”); System.out.println(“List contains [banana, date, grape,eggplant, jicama, grape] ntt “ + Arrays.toString(produceList.toArray())); produceList.moveToBeginning(); System.out.println(“List contains [grape, banana, date, grape,eggplant, jicama] ntt “ + Arrays.toString(produceList.toArray())); produceList.clear(); produceList.moveToBeginning(); System.out.println(“List contains [] ntt “ + Arrays.toString(produceList.toArray())); // Q9: equals System.out.println(“n***Q9***”); produceList.add(“banana”); produceList.add(“potato”); produceList.add(“jicama”); produceList.add(“grape”); AList foodList = new AList(); foodList.add(“banana”); foodList.add(“potato”); foodList.add(“jicama”); System.out.println(“Result is false: ” +produceList.equals(foodList)); foodList.add(“grape”); System.out.println(“Result is true: ” +produceList.equals(foodList)); foodList.add(“squash”); System.out.println(“Result is false: ” +produceList.equals(foodList)); foodList.replace(1, “jicama”); foodList.replace(3, “banana”); System.out.println(“Result is false: ” +produceList.equals(foodList)); System.out.println(“The list contains[banana, potato, jicama,grape] ntt “ + Arrays.toString(produceList.toArray())); System.out.println(“The second list contains[jicama, potato,banana, grape, squash] nttt” + Arrays.toString(foodList.toArray())); /* un-comment this section if doing the extra credit */ /* System.out.println(“nTESTING EXTRA CREDIT”); AList numberListA = newAList(); numberListA.add(1); numberListA.add(5); numberListA.add(6); numberListA.add(8); numberListA.add(7); numberListA.add(9); AList numberListB = newAList(); numberListB.add(2); numberListB.add(4); numberListB.add(3); numberListB.add(9); numberListB.add(7); System.out.println(“List B is smaller (fewer elements), resultshould be positive: ” + numberListA.compareTo(numberListB)); numberListA.remove(1); numberListA.remove(1); System.out.println(“List A is smaller (fewer elements), resultshould be negative: ” + numberListA.compareTo(numberListB)); numberListA.clear(); numberListA.add(1); numberListA.add(3); numberListA.add(5); numberListA.add(7); numberListA.add(10); numberListB.clear(); numberListB.add(2); numberListB.add(4); numberListB.add(3); numberListB.add(9); numberListB.add(7); System.out.println(“List A is smaller (same number of elements,1 Attached

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now