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

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

ID: 3903259 • Letter: I

Question

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

public final class ResizableArrayBag<T> implements BagInterface<T>

{

....

/**

* 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<T> union(BagInterface<T> otherBag)

{

checkInitialization();

ResizableArrayBag<T> newBag = new ResizableArrayBag<T>(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;

}

...

}

Explanation / Answer

Given below is the completed code.

Note , you need to change the ResizableBag<T> to ResizableArrayBag<T extends Comparable<T>>
This will enable us to compare the bag elements using compareTo() in order to sort them.
So your class header will look like
public final class ResizableArrayBag<T extends Comparable<T>> implements BagInterface<T>
{
//......


Then copy the below 3 methods into your class. Please do rate the answer if it helped. Let me know in case of any issues.

public BagInterface<T> union(BagInterface<T> otherBag)
{

checkInitialization();

ResizableArrayBag<T> newBag = new ResizableArrayBag<T>(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
newBag.sort();
return newBag;

}
  
//---------------------
private void sort()
{
//selection sort
for(int i = 0; i < numberOfEntries; i++)
{
int minIdx = i;
for(int j = i+1; j < numberOfEntries; j++)
{
if(bag[j].compareTo(bag[minIdx]) < 0)
minIdx = j;
}

if(i != minIdx)
{
//swap
T temp = bag[i];
bag[i] = bag[minIdx];
bag[minIdx] = temp;
}
}
}


//--------------------
@Override

public boolean equals(Object obj)
{
if((obj == null) || !(obj instanceof BagInterface))
{
return false;
}

BagInterface otherBag = (BagInterface) 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;

}

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