Question: I need assistance writing the following two java methods the equals method and the sort method public final class ResizableArrayBag implements BagInterface { .... /**

I need assistance writing the following two java methods the equals method and the sort method

public final class ResizableArrayBag implements BagInterface

{

....

/**

* The union of two collections consists of their contents combined into a new collection.

* @param otherBag: The other bag that is to have its contents combined with this bag.

* @return A new bag the contains the combined contents of this bag and the other bag.

* Note: Suppose bag 1 has the contents a,b,b,z and bag 2 has the contents d,g,b,c,e

* After this method has been called the results should be a,b,b,b,c,d,e,g,z not a,b,b,z,d,g,b,c,e

*/

public BagInterface union(BagInterface otherBag)

{

checkInitialization();

ResizableArrayBag newBag = new ResizableArrayBag(toArray());

T otherBagContents[] = otherBag.toArray();

if (otherBag != null)

{

for (T content : otherBagContents)

{

newBag.add(content);

}

}

//TODO write a private method named sort that will sort the newBag array to produce the correct results

return newBag;

}

/**

* Determines if to objects are equal.

* @param obj: The object to be compared.

* @return True when the contents of two bags are the same.

* currently this method only says true if I do bag1.equals(bag1)

* if i call the method bag1.equals(bag2) and bag 2 has the same contents the method says false

* I need help fixing this

* Note that two equal bags contain the same number of entries, and each entry occurs in each bag the same number of times. The order of the entries in each array is irrelevant.

*/

@Override

public boolean equals(Object obj)

{

if((obj == null) || (obj.getClass() != this.getClass()))

{

return false;

}

ResizableArrayBag otherBag = (ResizableArrayBag) obj;

if (this.getCurrentSize() == otherBag.getCurrentSize())

{

for (int i = 0; i < numberOfEntries; i++)

{

if (getFrequencyOf(bag[i]) != otherBag.getFrequencyOf(bag[i]))

{

return false;

}

return true;

}

}

return false;

}

...

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!