Question

In: Computer Science

Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to...

Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to this small section of Java code? Thumbs up always left for answers!

-------------------------------------------------------------------------------------------------------

// Check to see if two bags are equals.

   public boolean equals(LinkedBag<T> aBag) {
       boolean result = false; // result of comparison of bags

       if (this.numberOfEntries == aBag.numberOfEntries) {
           if (numberOfEntries == 0) {
               return true;
           }
           int i = 0, j = 0;
           Node scout = firstNode;
           Node retrieve = aBag.firstNode;
           while ((i < numberOfEntries) && (scout != null)) {
               while ((j < aBag.numberOfEntries) && (retrieve != null)) {
                   if (scout.data.equals(retrieve.data)) {
                       int freq1 = this.getFrequencyOf(scout.data);
                       int freq2 = this.getFrequencyOf(retrieve.data);
                       if (freq1 == freq2) {
                           result = true;
                       } else {
                           result = false;
                       }
                   } // end if
                   retrieve = retrieve.next;
                   j++;
               }
               scout = scout.next;
               i++;
           } // end while
       } else {
           result = false;
       }

       return result;
   } // end equals

//Duplicate all the items in a bag.

   public boolean duplicateAll() {
       boolean success = true;

       Node scout = firstNode;
       int i = 0;
       while (i < numberOfEntries) {
           Node temp = new Node(scout.data, firstNode);
           firstNode = temp;
           scout = scout.next;
           i++;
       }
       numberOfEntries *= 2;

       return success;
   } // end duplicateAll

// Remove all duplicate items from a bag

   public void removeDuplicates() {
       System.out.print("Initial > ");
       System.out.println(this.toString());
       LinkedBag<T> newBag = new LinkedBag<T>();

       if (numberOfEntries == 0) {
           return; // Do nothing
       }

// Create filler values to avoid null pointer
       for (int k = 0; k < numberOfEntries; k++) {

           newBag.add((T) "_");
       }

       Node scout = firstNode;
       Node replace = newBag.firstNode;

       int i = 0;
       while ((i < numberOfEntries) && (scout != null)) {
           int j = 0;
           while ((j < numberOfEntries) && (replace != null)) {
               if (newBag.contains(scout.data)) {
                   break;
               } else {
                   replace.data = scout.data;
                   replace = replace.next;
                   j++;
               }
           }
           if (scout != null) {
               scout = scout.next;
               i++;
           }

       }

// Remove filler values
       Node newScout = newBag.firstNode;
       while (newBag.contains((T) "_")) {
           newBag.remove((T) "_");
       }

       this.firstNode = newBag.firstNode;
       this.numberOfEntries = newBag.numberOfEntries;

       System.out.print("Final > ");
       System.out.println(toString());

       return;
   }
}

Solutions

Expert Solution

CODE

/**

* This method is used to determine if two bags are equal or not. Two bags are equal if both the bags

* contains the same elements equal number of times (in case of duplicates)

* @param aBag -> the other LinkedBag to be compared to

* @return a boolean representing if the bags are same

*/

public boolean equals(LinkedBag<T> aBag) {

boolean result = false; // result of comparison of bags

if (this.numberOfEntries == aBag.numberOfEntries) {

if (numberOfEntries == 0) {

return true;

}

int i = 0, j = 0;

Node scout = firstNode;

Node retrieve = aBag.firstNode;

while ((i < numberOfEntries) && (scout != null)) {

while ((j < aBag.numberOfEntries) && (retrieve != null)) {

if (scout.data.equals(retrieve.data)) {

int freq1 = this.getFrequencyOf(scout.data);

int freq2 = this.getFrequencyOf(retrieve.data);

if (freq1 == freq2) {

result = true;

} else {

result = false;

}

} // end if

retrieve = retrieve.next;

j++;

}

scout = scout.next;

i++;

} // end while

} else {

result = false;

}

return result;

} // end equals

//Duplicate all the items in a bag.

/**

* This util method is used to duplicate the first node in the bag and insert it until we have enough entries in the bag.

* @return

*/

public boolean duplicateAll() {

boolean success = true;

Node scout = firstNode;

int i = 0;

while (i < numberOfEntries) {

Node temp = new Node(scout.data, firstNode);

firstNode = temp;

scout = scout.next;

i++;

}

numberOfEntries *= 2;

return success;

} // end duplicateAll

/**

* This method is used to remove all duplicate elements from the bag

*/

public void removeDuplicates() {

System.out.print("Initial > ");

System.out.println(this.toString());

LinkedBag<T> newBag = new LinkedBag<T>();

if (numberOfEntries == 0) {

return; // Do nothing

}

// Create filler values to avoid null pointer

for (int k = 0; k < numberOfEntries; k++) {

newBag.add((T) "_");

}

Node scout = firstNode;

Node replace = newBag.firstNode;

int i = 0;

while ((i < numberOfEntries) && (scout != null)) {

int j = 0;

while ((j < numberOfEntries) && (replace != null)) {

if (newBag.contains(scout.data)) {

break;

} else {

replace.data = scout.data;

replace = replace.next;

j++;

}

}

if (scout != null) {

scout = scout.next;

i++;

}

}

// Remove filler values

Node newScout = newBag.firstNode;

while (newBag.contains((T) "_")) {

newBag.remove((T) "_");

}

this.firstNode = newBag.firstNode;

this.numberOfEntries = newBag.numberOfEntries;

System.out.print("Final > ");

System.out.println(toString());

return;

}


Related Solutions

Can I get a breakdown line by line of the time complexity, I know it is...
Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole. Input :- N , Array[N]                                function findSmallestPair()                                sort the Array                                int minimumDiff := INFINITY , A := -1 , B := -1;                                  for(int i : 1 to N-1)                                begin for                currentDiff = Array[i]-Array[i-1]                if(currentDiff < minimumDiff)                begin if   ...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for a program for Hansel's Housecleaning Service. The program prompts the user for a customer's last name only. While the last name is not “zzz” your program will ask for the number of bathrooms and the number of other rooms to be cleaned and display the cleaning charge. You should use a sentinel-controlled while loop in your main loop logic. A customer name of “zzz”...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Can I get some sample of journal analysis of management ?
Can I get some sample of journal analysis of management ?
Can I get provided a business plan of anything you could think of from the top...
Can I get provided a business plan of anything you could think of from the top of your head? I was thinking a cafe/bar/entertainment however if you provide something else that is fine. DESPERATELY need a good mark for this assignment. so Im going to need all the help I can get! the requirements are below 1.Introduction A clear introduction of your NEWbusiness idea, make sure that first time readers can understand your business idea. Please also don’treuse the business...
Can the italicized portion of the problem be explained? I understand up until the 0.015 moles...
Can the italicized portion of the problem be explained? I understand up until the 0.015 moles of protonated acetic acid remained and I understand the HH portion of the problem after but I would like some clarity on how this number was concluded. 2. The following question has two parts. a) What is the final pH of a solution obtained by mixing 250 ml of 0.3 M acetic acid with 300 ml of 0.2 M KOH? (pKb of acetate =...
Give an account of the synthesis of ATP in respiration.   ( can i please get some...
Give an account of the synthesis of ATP in respiration.   ( can i please get some help with this question)
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT