Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with writing the test method for the this method and also check if I wrote the methods correctly. The intersection method

Can someone help me with writing the test method for the this method and also check if I wrote the methods correctly.

The intersection method

The intersection of two bags contains the minimum number of occurrences of each element from each bag. For example:

intersection({a, a, a, b}, {a, a, b, b}) == {a, a, b}

intersection({ 2,3,4,3,5,6,5,3,2}, {2,3,3,5,5,5,5}) =={2,3,3,5,5}

Keep in mind that if you test your intersection method by using the equals method from GroceryBag, your equals method will need to be functioning correctly before you can test intersection. To avoid a dependency, you can test the properties of your intersection such as its size, which items it contains, and the number of occurrences of those items.

You can test this method by a) defining 3 bags where one of them is the intersection of the other 2 bags, as in the examples above, then b) use assertTrue to check that the results will equal to the third object.

Write the intersection method for GroceryBag using the following approach:

Create two variables of type String[] and use them to store the contents of each bag. [Hint: Use contents() instead of contents field.]

Next create a GroceryBag object to store the intersection you are about to find.

This method will require the use of two loops.

Create the outer loop and have it iterate through the elements in the first array.

Then create the inner loop and have it iterate through the elements in the second array.

If a match is found then that element belongs in the intersection.

Null out both array entries that already matched so it doesn't get counted repeatedly. Be sure you are putting nulls in your copies from step 1, not in the contents[] of GroceryBag.

Because the array may contain nulls, you may need to check whether the index contains a null before calling methods on that index. [Else, you will get NullPointerException]

// Public methods ........................................................

/**

* If an element is in both bags, then it will be in the intersection. If there

* are multiple occurrences of that element, then the number of occurrences of

* that element in the intersection will equal the minimum number of occurrences

* in either set.

*

* Examples: intersection of ({"apple","apple","cereal","chips"}, {"chips",

* "apple","apple","chips","cake"}) = {"apple","apple","chips"}

*

* @param bag Bag to be intersected with.

* @return The intersection of the two bags.

*/

public GroceryBag intersection(GroceryBag bag) {

// Implement this method

String[] bag1 = bag.contents();

String[] bag2 = bag.contents();

GroceryBag intersection = new GroceryBag();

for (int i = 0; i < bag1.length; i++)

{

for (int j = 0; j < bag2.length; j++)

{

if(bag2[j] !=null && bag1[i].equals(bag2[j]))

{

intersection.add(bag1[i]);

bag2[j]=null;

bag1[i]=null;

break;

}

}

}

if(!intersection.isEmpty()) {

return intersection;

}

return null;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions