Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Posted is my code, with everything done but the removeAllOccurences method(this is the only package I need help with). Please fill in the removeAllOccurences method

Posted is my code, with everything done but the removeAllOccurences method(this is the only package I need help with). Please fill in the removeAllOccurences method with a code that converts the 2D array (T[] [] entries) into a 1D array, then remove the duplicates in the array, then remove all occurrences of given item. Please feel free to add any methods if needed

package asmt02Part01; import java.util.Arrays; /**  * 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  * @author (modifier) Duc Ta  * @param <T> */ public final class LinkedBag implements BagInterface { private Node firstNode; // Reference to first node private int numberOfEntries; public LinkedBag() { firstNode = null; numberOfEntries = 0; } // end default constructor /**  * Gets the number of entries currently in this bag.  *  * @return The integer number of entries currently in this bag.  */  @Override public int getCurrentSize() { return numberOfEntries; } // end getCurrentSize /**  * Sees whether this bag is empty.  *  * @return True if this bag is empty, or false if not.  */  @Override public boolean isEmpty() { return numberOfEntries == 0; } // end 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.  */  @Override 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.  */  @Override 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 < numberOfEntries) && (currentNode != null)) { result[index] = currentNode.data; index++; currentNode = currentNode.next; } // end while return result; } // end toArray // 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 /**  * Removes all occurrences of the given entries  *  */  @Override public void removeAllOccurences(T[][] entries) { // Convert 2D array to 1D array // Remove duplicates in array // Remove all occurences of given items } // end removeAllOccurences 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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions