Question: solve by using the code that i put Consider the LinkedBag class which implements the interface BagInterface (the source code for both has been provided

solve by using the code that i put
solve by using the code that i put Consider the LinkedBag class
which implements the interface BagInterface (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
newEntry. For example, suppose that replace (A, D) is called on this

Consider the LinkedBag class which implements the interface BagInterface (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 newEntry. 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 isDuplicated (D) should return true. 3) void doubleBag () adds to the bag a copy of each entry that appears in the bag. For example, calling doubleBag () 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. Data structure and Algorithms, Spring 2021-22 3/3 package Bag: /** A class of bags whose entries are stored in a chain of linked nodes. The bag is never full. @author Frank M. Carrano @author Timothy M. Henry @version 4.1 *7 public final class LinkedBag implements Baginterface { private Node firstNode; || Reference to first node private int numberOfEntries; public LinkedBago) { 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 numberOfEntries == 0; } // 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 /** 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) || OutOfMemoryError possible { Add to beginning of chain: Node newNode = new Node(newEntry); newNode.next = firstNode; // Make new node reference rest of chain // (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. @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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!