Question: This is class that implements a bag interface. Need to add a display method to display contents of bags in main, then call method in
This is class that implements a bag interface. Need to add a display method to display contents of bags in main, then call method in main public final class LinkedBag implements BagInterface { private Node firstNode; private int numberOfEntries; public LinkedBag() { firstNode = null; numberOfEntries = 0; }
(Output cut.method display belong here)
-------------------- (main class)
public static void main(String[] args) { BagInterface firstbag = new LinkedBag<>(); BagInterface secondbag = new LinkedBag<>();
firstbag.add("One"); firstbag.add("Two"); secondbag.add("Three"); secondbag.add("Four");
(Output cutcall method display here)
(this is linked list class)
public final class LinkedBag
{
private Node firstNode;
private int numberOfEntries;
public LinkedBag()
{
firstNode = null;
numberOfEntries = 0;
}
public boolean isEmpty()
{
return numberOfEntries == 0;
}
public int getCurrentSize()
{
return numberOfEntries;
public boolean add(T newEntry)
{
Node newNode = new Node(newEntry);
newNode.next = firstNode;
firstNode = newNode;
numberOfEntries++;
return true;
}
public T[] toArray()
{
@SuppressWarnings("unchecked")
T[] result = (T[])new Object[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null))
{
result[index] = currentNode.data;
index++;
currentNode = currentNode.next;
}
return result;
}
public int getFrequencyOf(T anEntry)
{
int frequency = 0;
int counter = 0;
Node currentNode = firstNode;
while ((counter < numberOfEntries) && (currentNode != null))
{
if (anEntry.equals(currentNode.data))
{
frequency++;
}
counter++;
currentNode = currentNode.next;
}
return frequency;
}
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;
}
return found;
}
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;
}
return currentNode;
}
public void clear()
{
while (!isEmpty())
remove();
}
public T remove()
{
T result = null;
if (firstNode != null)
{
result = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--;
}
return result;
}
public boolean remove(T anEntry)
{
boolean result = false;
Node nodeN = getReferenceTo(anEntry);
if (nodeN != null)
{
nodeN.data = firstNode.data;
in first node
firstNode = firstNode.next;
numberOfEntries--;
result = true;
}
return result;
}
private class Node
{
private T data;
private Node next;
private Node(T dataPortion)
{
this(dataPortion, null);
}
private Node(T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;
}
}
}
(this is bag Interface being implemented)
public interface BagInterface
{
public int getCurrentSize();
public boolean isEmpty();
public boolean add(T newEntry);
public T remove();
public boolean remove(T anEntry);
public void clear();
public int getFrequencyOf(T anEntry);
public boolean contains(T anEntry);
public T[] toArray();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
