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

solve the whole questions please from 50-54. thnks LTE 4G+ \"il 29% 6:38 PM X Qu

ID: 3910219 • Letter: S

Question

solve the whole questions please from 50-54. thnks

LTE 4G+ "il 29% 6:38 PM X 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 The method getMin that returns the smallest object in a bag he method getMax that returns the largest object in a bag 53- Suppose that a bag contains Comparable objects. Define a method for the ss 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

Explanation / Answer

Hello there,

I have gone through the book "Datastructures and Abstractions with java" Which is related to above questions.

I have seen all the definitions over there about LinkedBag implementation.

And writing the answer codes for functions that you have asked below:

##########################################################################

[50] function to remove random element from LinkedBag:

public T remove()
{
T result = null;
Random rand = new Random();
int r = rand.next(this.numberOfEntries)
int i=0;
Node temp=firstNode;
Node prev=null;

if(r==0) {

if(firstNode != null)
{
result = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--
}

else {
    while(i<r && firstNode!=null) {    
        i++;
        prev=firstNode;
        firstNode=firstNode.next;
    }

    result = firstNode.data;
    prev.next=firstNode.next;
    firstNode=temp;
    numberOfEntries--;
}

return result;

}

/////////////////////////////////////

Above function will not affect any other function given in book. As I have handled all the edge cases in the above function definition it has nothing to affect other functions.

###########################################################################

[51] Function to remove all occurences of given entry from LinkedBag:

public void removeEvery(T entry) {

Node temp = this.firstNode;

Node prev = null;

  

while(temp!=null) {

if(temp.data==entry) {

if(temp==firstNode) {

firstNode=temp.next;

temp=firstNode;

noOfEntries--;

}

else {

prev.next=temp.next;

temp=temp.next;

noOfEntries--;

}

}

else {

prev=temp;

temp=temp.next;

}

}

}

///////////////////////////////////////////////////////////////////////////////////////

Above function will delete all the entries of given element from the LinkedBag and will maintain all links in remaining LinkedBag properly. It will not return anything. The updated LinkedBag is stored in LinkedBag object's firstNode correctly.

Do comment if any query/update needed regarding above function.

##############################################################################

[52] Assuming LinkedBag contains comparable objects [i.e. T data] So functions to get the minimum and maximum object from given LinkeBag are as follows:

public T getMin() {

T min = firstNode.data;

Node temp = firstNode;

while(temp!=null) {

if(temp.data<min)

min=temp.data;

temp=temp.next;

}

return min;

}

public T getMax() {

T max = firstNode.data;

Node temp = firstNode;

while(temp!=null) {

if(temp.data>max)

max=temp.data;

temp=temp.next;

}

return max;

}

////////////////////////////////////////////////////

Above functions will work properly for getting min and max objects.

#########################################################################

[53] Considering LinkedBag has comparable objects [ T data] and provided a argument of same type. Function to return ArrayBag that will contain elements from LinkedBag which are less than given argument is as follows:

public ArrayBag getAllLessThan(T data) {

ArrayBag arrBag = new ArrayBag(this.noOfEntries);

//above statement will create ArrayBag of maximum size equals to total elements in LinkedBag for storing result.

Node temp = this.firstNode;

int i = 0;

while(temp!=null) {

if(temp.data<data)

arrBag.bag[i++]=data;

temp=temp.next;

}

return arrBag;

}

###########################################################################

[54] Function to check whether two LinkedBags are equal or not is as follows:

public boolean equals(LinkedBag bag) {

if(this.noOfEntries!=bag.noOfEntries)

return false;

Node temp1 = this.firstNode;

Node temp2 = bag.firstNode;

while(temp1!=null && temp2!=null) {

if(temp1.data!=temp2.data)

return false;

temp1=temp1.next;

temp2=temp2.next;

}

if(temp1==null && temp2==null)

return true;

else

return false;

}

////////////////////////////////////////////////////////////////////////

Do ask if any update is required in above function also.......

########################################################################

I can give further modifications according to requirement also.

Thank you!!!

Have a good day!