Question: this is the assignment and the 2 codes please do what is needed and thanks (I only subscribed to chegg for this question so please
this is the assignment and the 2 codes please do what is needed and thanks (I only subscribed to chegg for this question so please I really need help)
Consider the LinkedBag class which implements the interface Bag Interface (the source code for both has been provided to you on Moodle). Add the following methods to BagInterface and implement them in class LinkedBag: 1) void replace (T oldEntry, T newEntry) replaces any entry that is equal to oldEntry with the new entry newntry. For example, suppose that replace (A, D) is called on this bag {A, B, C, A}. The resulting bag should be {D, B, C, D). 2) boolean is Duplicated (T anEntry) returns true if the entry anEntry appears more than one time in the bag and false otherwise. For example, calling is Duplicated (B) on the resulting bag in part 1 should return false, but is Duplicated (D) should return true. 3) void double () adds to the bag a copy of each entry that appears in the bag. For example, calling double () on the resulting bag in part 1 should change the bag contents to {4'D, 2'B, 2'C}. Create a test class called LinkedBagTest. In its main method, create and initialize the contents of a bag and use the methods you implemented above to test your implementation. Your implementation must be as efficient as possible. 11 12 13 public interface BagInterface { /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. public int getCurrentSize(); - 15 16 17 P /** Sees whether this bag is empty. @return True if the bag is empty, or false if not. / public boolean isEmpty(); 19 20 21 L /** Adds a new entry to this bag. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean add (T newEntry); 22 1 24 25 26 L /** Removes one unspecified entry from this bag, if possible. @return Either the removed entry, if the removal. was successful, or null. */ public I remove(); 27 1 29 30 TH 31 32 1 7** Removes one occurrence of a given entry from this bag. @param anEntry The entry to be removed. @return True if the removal was successful, or false if not. / public boolean remove (T anEntry); 34 35 /** Removes all entries from this bag. */ public void clear(); 37 38 39 7** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. . 37 38 - 0 39 40 1 /** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in the bag. */ public int getFrequencyOf(T anEntry); 42 43 44 /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false if not. */ public boolean contains (T anEntry); 45 I 4 47 48 49 50 /** Retrieves all entries that are in this bag. @return A newly allocated array of all the entries in the bag. Note: If the bag is empty, the returned array is empty. */ public T[] toArray(); public T[] toArray(); // Alternate 17 public Object[] toArray(); // Alternate 1 } // end BagInterface 52 53 54 55 un 5 56 public final class LinkedBag implements BagInterface { private Node firstNode; 1/ Reference to first node private int numberOf Entries; public LinkedBag() { firstNode = null; numberOfEntries = 0; } // end default constructor /** Sees whether this bag is empty. @return True if this bag is empty, or false if not. */ public boolean isEmpty { return numberof Entries } // end isEmpty /** Gets the number of entries currently in this bag. @return the integer number of entries currently in this bag. */ public int getCurrentSize() { return numberofEntries; } // end getCurrentSize 7** Adds a new entry to this bag. @param newEntry The object to be added as a new entry @return True if the addition is successful, or false if not. */ public boolean add(T newEntry) // OutOfMemory Error possible { // Add to beginning of chain: Node newNode = new Node (newEntry); newNode.next = firstNode; // Make new node reference rest of chain 1/ (firstNode is null if chain is empty) firstNode = newNode; // New node is at beginning of chain numberOfEntries++; = return true; } // end add /** Retrieves all entries that are in this bag. = Retrieves all entries that are in this bag. @return A newly allocated array of all the entries in this bag. */ public T[] toArray() { // The cast is safe because the new array contains null entries @suppresswarnings ("unchecked") T[] result = (T[] new Object[numberOfEntries]; // Unchecked cast int index = 0; Node currentNode = firstNode; while ((index