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
{
....
/**
* 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
{
checkInitialization();
ResizableArrayBag
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started