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

Question 1. Write a replace method for the ArrayBag class. This method: replaces

ID: 3727094 • Letter: Q

Question

Question 1.

Write a replace method for the ArrayBag class. This method:

replaces any element currently in the bag with an element sent in as a parameter

returns the element that is replaced (removed)

Question 2. Write a removeEvery method for the ArrayBag class. The method:

removes all occurrences of a specific entry (send as a parameter) from a bag

Question 3. Write a union method for the ResizableArrayBag class.

The union of two bags is the combined contents of both bags. A union might contain duplicates.

The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends.

The method header is:

public BagInterface union(ResizableArrayBag anotherBag)

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, since bags are unordered.)

Question 4.

Write an intersection method for the ResizableArrayBag class.

The intersection of two bags is the overlapping content of the bags.

An intersecion might contain duplicates.

The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when 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 that contains (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<Integer> numbersBag = new ArrayBag<Integer>();

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]

"

+

Arrays.toString(numbersBag.toArray()));

// Q3 replace method

// NOTE: The description does not specify that you must

replace the last

// element in the array, so your output might be

different- that's okay

System.out.println(" ***Q3***");

numbersBag.replace(6);

System.out.println("The bag contains[1, 2, 1, 4, 6]

"

+

Arrays.toString(numbersBag.toArray()));

numbersBag.replace(8);

System.out.println("The bag contains[1, 2, 1, 4, 8]

"

+

Arrays.toString(numbersBag.toArray()));

// Q4 removeEvery method

// NOTE: bags are unordered, so the order of your array

might be

// different- that's okay as long as the contents match

what is listed below

System.out.println(" ***Q4***");

numbersBag.add(1);

numbersBag.removeEvery(1);

System.out.println("The bag contains[8, 2, 4] (any order)

"

+

Arrays.toString(numbersBag.toArray()));

numbersBag.removeEvery(3);

System.out.println("The bag contains[8, 2, 4] (any order)

"

+

Arrays.toString(numbersBag.toArray()));

// Q5: union

System.out.println(" ***Q5***");

ResizableArrayBag<Integer> firstResizableBag = new

ResizableArrayBag<Integer>();

firstResizableBag.add(8);

firstResizableBag.add(2);

firstResizableBag.add(4);

firstResizableBag.add(5);

firstResizableBag.add(6);

firstResizableBag.add(2);

ResizableArrayBag<Integer> secondResizableBag = new

ResizableArrayBag<Integer>();

secondResizableBag.add(3);

secondResizableBag.add(1);

secondResizableBag.add(2);

BagInterface<Integer> unionBag = firstResizableBag

.union(secondResizableBag);

System.out.println("Bag1 contains [8, 2, 4, 5, 6, 2]

"

+

Arrays.toString(firstResizableBag.toArray()));

System.out.println("Bag2 contains [3, 1, 2] "

+

Arrays.toString(secondResizableBag.toArray()));

System.out.println("Union Bag contains [8, 2, 4, 5, 6, 2,

3, 1, 2] (any order) "

+

Arrays.toString(unionBag.toArray()));

// Q6: intersection

System.out.println(" ***Q6***");

firstResizableBag.add(2);

secondResizableBag.add(2);

secondResizableBag.add(4);

BagInterface<Integer> intersectionBag =

firstResizableBag.intersection(secondResizableBag);

System.out.println("Bag1 contains [8, 2, 4, 5, 6, 2, 2]

"

+

Arrays.toString(firstResizableBag.toArray()));

System.out.println("Bag2 contains [3, 1, 2, 2, 4]

"

+

Arrays.toString(secondResizableBag.toArray()));

System.out.println("Intersection Bag contains [2, 4, 2]

(any order) "

+

Arrays.toString(intersectionBag.toArray()));

// setting up AList to test

System.out.println(" TESTING LIST");

AList<String> produceList = new AList<String>();

produceList.add("banana");

// Q5: union

System.out.println(" ***Q5***");

ResizableArrayBag<Integer> firstResizableBag = new

ResizableArrayBag<Integer>();

firstResizableBag.add(8);

firstResizableBag.add(2);

firstResizableBag.add(4);

firstResizableBag.add(5);

firstResizableBag.add(6);

firstResizableBag.add(2);

ResizableArrayBag<Integer> secondResizableBag = new

ResizableArrayBag<Integer>();

secondResizableBag.add(3);

secondResizableBag.add(1);

secondResizableBag.add(2);

BagInterface<Integer> unionBag = firstResizableBag

.union(secondResizableBag);

System.out.println("Bag1 contains [8, 2, 4, 5, 6, 2]

"

+

Arrays.toString(firstResizableBag.toArray()));

System.out.println("Bag2 contains [3, 1, 2] "

+

Arrays.toString(secondResizableBag.toArray()));

System.out.println("Union Bag contains [8, 2, 4, 5, 6, 2,

3, 1, 2] (any order) "

+

Arrays.toString(unionBag.toArray()));

// Q6: intersection

System.out.println(" ***Q6***");

firstResizableBag.add(2);

secondResizableBag.add(2);

secondResizableBag.add(4);

BagInterface<Integer> intersectionBag =

firstResizableBag.intersection(secondResizableBag);

System.out.println("Bag1 contains [8, 2, 4, 5, 6, 2, 2]

"

+

Arrays.toString(firstResizableBag.toArray()));

System.out.println("Bag2 contains [3, 1, 2, 2, 4]

"

+

Arrays.toString(secondResizableBag.toArray()));

System.out.println("Intersection Bag contains [2, 4, 2]

(any order) "

+

Arrays.toString(intersectionBag.toArray()));

// setting up AList to test

System.out.println(" TESTING LIST");

AList<String> produceList = new AList<String>();

produceList.add("banana");

produceList.add("date");

produceList.add("grape");

produceList.add("eggplant");

produceList.add("jicama");

produceList.add("grape");

Explanation / Answer

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Random;

/*

* This class is a generic class which can contain any data.

*/

public class ArrayBag<T> {

private T t; // Facilitates generic datatype

private List<T> dataList = new ArrayList<T>(); // the list that contains data for ArrayBag

/*

* This function is used to add new value to ArrayBag data list.

*/

public void add(T t){

this.t=t;

dataList.add(t);

}

/*

* This function is used to return the data in the ArrayBag list.

*/

public T[] toArray(){

return (T[]) dataList.toArray();

}

/*

* Question 1. This function is written to replace any value in the datalist with the new data

*/

public void replace(T t){

int size = dataList.size();

Random rand = new Random();

int rand_int1 = rand.nextInt(size); //Find the random location in the list to be replaced by the new data

dataList.set(rand_int1, t); // replace the old data in identified location above with the new data

}

/*

* Question 1. This function is written to remove all occurrences of a specific entry

*/

public void removeEvery(T t){

if(dataList.contains(t)){

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

if(dataList.get(i).equals(t)){

dataList.remove(i);

}

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote