Question: Java program: Use the program below to start coding Write method replace that visits all the nodes and replaces all occurrences of a specific item

Java program: Use the program below to start coding

Write method replace that visits all the nodes and replaces all occurrences of a specific item with another item.

Copy the code below to start coding:

1. BagInterface.java

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(); /** Sees whether this bag is full. * @return true if the bag is full, or false if not */ public boolean isFull(); /** Sees whether this bag is empty. * @return true if the bag is empty, or false if not */ public boolean isEmpty(); /** 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); /** Removes one unspecified entry from this bag, if possible. * @return either the removed entry, if the removal * was successful, or null */ public T remove(); /** Removes one occurrence of a given entry from this bag, if possible. * @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** 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); /** Tests whether this bag contains a given entry. * @param anEntry the entry to locate @return true if the bag contains anEntry, or false otherwise */ public boolean contains(T anEntry); /** Creates an array of all entries that are in this bag. * @return a newly allocated array of all the entries in the bag */ public T[] toArray(); }//End Bag Interface 

2. NodeBaseBag.java

public class NodeBasedBag implements BagInterface { Node firstNode; int numberOfNodes = 0; public NodeBasedBag() { firstNode = null; numberOfNodes = 0; } @Override public int getCurrentSize() { return numberOfNodes; } @Override public boolean isEmpty() { return numberOfNodes == 0; } @Override public boolean add(T newEntry) { // 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 numberOfNodes++; return true; } @Override public T remove() { T result = null; if (firstNode != null) { result = firstNode.item; firstNode = firstNode.next; // remove first node from chain numberOfNodes--; } // end if return result; } private Node getReferenceTo(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return currentNode; } // end getReferenceTo @Override public boolean remove(T anEntry) { boolean result = false; Node nodeN = getReferenceTo(anEntry); if (nodeN != null) { nodeN.item = firstNode.item; // replace located entry with entry // in first node remove(); // remove first node result = true; } // end if return result; } @Override public void clear() { while (!isEmpty()) { remove(); } } @Override public int getFrequencyOf(T anEntry) { int frequency = 0; int counter = 0; Node currentNode = firstNode; while ((counter < numberOfNodes) && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { frequency++; } counter++; currentNode = currentNode.next; } // end while return frequency; } @Override public boolean contains(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return found; } @Override public T[] toArray() { T[] result = (T[]) new Object[numberOfNodes]; // unchecked cast int index = 0; Node currentNode = firstNode; while ((index < numberOfNodes) && (currentNode != null)) { result[index] = currentNode.item; index++; currentNode = currentNode.next; } // end while return result; } @Override public boolean isFull() { return false; } class Node { public T getItem() { return item; } public void setItem(T item) { this.item = item; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } T item; Node next; Node(T item) { this(item, null); } Node(T item, Node next) { this.item = item; this.next = next; } } } 

3. NodeBaseBagTest.java

public class NodeBasedBag implements BagInterface { Node firstNode; int numberOfNodes = 0; public NodeBasedBag() { firstNode = null; numberOfNodes = 0; } @Override public int getCurrentSize() { return numberOfNodes; } @Override public boolean isEmpty() { return numberOfNodes == 0; } @Override public boolean add(T newEntry) { // 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 numberOfNodes++; return true; } @Override public T remove() { T result = null; if (firstNode != null) { result = firstNode.item; firstNode = firstNode.next; // remove first node from chain numberOfNodes--; } // end if return result; } private Node getReferenceTo(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return currentNode; } // end getReferenceTo @Override public boolean remove(T anEntry) { boolean result = false; Node nodeN = getReferenceTo(anEntry); if (nodeN != null) { nodeN.item = firstNode.item; // replace located entry with entry // in first node remove(); // remove first node result = true; } // end if return result; } @Override public void clear() { while (!isEmpty()) { remove(); } } @Override public int getFrequencyOf(T anEntry) { int frequency = 0; int counter = 0; Node currentNode = firstNode; while ((counter < numberOfNodes) && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { frequency++; } counter++; currentNode = currentNode.next; } // end while return frequency; } @Override public boolean contains(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.item)) { found = true; } else { currentNode = currentNode.next; } } // end while return found; } @Override public T[] toArray() { T[] result = (T[]) new Object[numberOfNodes]; // unchecked cast int index = 0; Node currentNode = firstNode; while ((index < numberOfNodes) && (currentNode != null)) { result[index] = currentNode.item; index++; currentNode = currentNode.next; } // end while return result; } @Override public boolean isFull() { return false; } class Node { public T getItem() { return item; } public void setItem(T item) { this.item = item; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } T item; Node next; Node(T item) { this(item, null); } Node(T item, Node next) { this.item = item; this.next = next; } } } 

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!