Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

a. How are members selected to join the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago