Question: JAVA ASAP Write a program named First_Last_Project1.java that thoroughly tests the class LinkedBag attached below. Please make sure your source file is named as required
JAVA ASAP
Write a program named First_Last_Project1.java that thoroughly tests the class LinkedBag attached below. Please make sure your source file is named as required First_Last_Project1 where First is your first name and Last is your last name.
/** 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 5.0 */ public final class LinkedBag
/** 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 /** Removes one unspecified entry from this bag, if possible. @return Either the removed entry, if the removal was successful, or null. */ public T remove() { T result = null; if (firstNode != null) { result = firstNode.data; firstNode = firstNode.next; // Remove first node from chain numberOfEntries--; } // end if return result; } // end 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 otherwise. */ public boolean remove(T anEntry) { boolean result = false; Node nodeN = getReferenceTo(anEntry); if (nodeN != null) { nodeN.data = firstNode.data; // Replace located entry with entry in first node firstNode = firstNode.next; // Remove first node numberOfEntries--; result = true; } // end if return result; } // end remove /** Removes all entries from this bag. */ public void clear() { while (!isEmpty()) remove(); } // end 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 this bag. */ public int getFrequencyOf(T anEntry) { int frequency = 0; int loopCounter = 0; Node currentNode = firstNode;
while ((loopCounter < numberOfEntries) && (currentNode != null)) { if (anEntry.equals(currentNode.data)) { frequency++; } // end if loopCounter++; currentNode = currentNode.next; } // end while return frequency; } // end getFrequencyOf /** 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) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.data)) found = true; else currentNode = currentNode.next; } // end while return found; } // end contains // Locates a given entry within this bag. // Returns a reference to the node containing the entry, if located, // or null otherwise. private Node getReferenceTo(T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.data)) found = true; else currentNode = currentNode.next; } // end while return currentNode; } // end getReferenceTo private class Node { private T data; // Entry in bag private Node next; // Link to next node private Node(T dataPortion)
{ this(dataPortion, null); } // end constructor private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } // end constructor } // end Node } // end LinkedBag
/** An interface that describes the operations of a bag of objects. */ public interface BagInterface
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
