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

50. Revise the definition of method remove, so that it removes a random entry fr

ID: 3909796 • Letter: 5

Question

50. Revise the definition of method remove, so that it removes a random entry from a bag. Would this change affect any other method within the class LinkedBag? occurrences of a given entry from a bag. methods for the class LinkedBag: S1. Define method removeEvery for class LinkedBag that removes all 52. Suppose that a bag contains Comparable objects. Define the following c. The method getMin that returns the smallest object in a bag d. The method getMax that returns the largest object in a ba 53. Suppose that a bag contains Comparable objects. Define a method for the class LinkedBag that returns a new bag of items that are less than some given item. The header of the method could be as follows: public ArrayBag getAllLessThan (Comparable anobject). Make sure that your method does not affect the state of the original bag 54. Define an equals method for the class LinkedBag that returns true when the contents of two bags are the same. Note that two equal bags contain the same number of entries, and each entry occurs in each bag the same number of times.

Explanation / Answer

public BagInterface<T> getAllLessThan(Comparable<T> anObject)
{
      
       //creates a new bag initialized from ResizableArrayBag b/c it can't be initialized from the BagInterface directly.
       BagInterface<T> newBag = new ResizableArrayBag<T>();      
      
       // iterate over entire entries and compares the item passed as a parameter to the original bag array items
       // and adds items less than the item passed as the parameter to the new bag.
       for(int i= 0; i < numberOfEntries; i++){                                  
           if(anObject.compareTo(bag[i])>0){                                          
               newBag.add(bag[i]);                                                          
           }  
       }  
       return newBag;       // returns newBag, while the original bag was not altered.                                                                  
  
}

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