(Solved) : ~ File 1 ~ Package Asmt02part01 Interface Describes Operations Bag Objects Please Change F Q40574994 . . .

$9.00

~ {File 1} ~

package asmt02Part01;

/**
* An interface that describes the operations of a bag ofobjects.
*
* **************************** PLEASE DO NOT CHANGE THIS FILE***********
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @param <T>
*/
public interface BagInterface<T> {

    /**
     * Gets the current number of entries inthis bag.
     *
     * @return The integer number of entriescurrently in the bag.
     */
    public int getCurrentSize();

    /**
     * Sees whether this bag is empty.
     *
     * @return True if the bag is empty, orfalse if not.
     */
    public boolean isEmpty();

    /**
     * Adds a new entry to this bag.
     *
     * @param newEntry The object to be addedas a new entry.
     * @return True if the addition issuccessful, or false if not.
     */
    public boolean add(T newEntry);

    /**
     * Removes all occurrences of the givenentries
     *
     * @param entries
     */
    public void removeAllOccurences(T[][]entries);

    /**
     * Retrieves all entries that are in thisbag.
     *
     * @return A newly allocated array of allthe entries in the bag. Note: If
     * the bag is empty, the returned array isempty.
     */
    public T[] toArray();

} // end BagInterface

~ {File 2} ~

package asmt02Part01;

import java.util.Arrays;

/**
* A class of bags whose entries are stored in a chain of linkednodes. The bag
* is never full.
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @author (modifier) Duc Ta
* @param <T>
*/
public final class LinkedBag<T> implementsBagInterface<T> {

    private NodefirstNode;       // Reference tofirst node
    private int numberOfEntries;

    public LinkedBag() {
        firstNode = null;
        numberOfEntries =0;
    } // end default constructor

    /**
     * Gets the number of entries currently inthis bag.
     *
     * @return The integer number of entriescurrently in this bag.
     */
    @Override
    public int getCurrentSize() {
        returnnumberOfEntries;
    } // end getCurrentSize

    /**
     * Sees whether this bag is empty.
     *
     * @return True if this bag is empty, orfalse if not.
     */
    @Override
    public boolean isEmpty() {
        return numberOfEntries== 0;
    } // end isEmpty

    /**
     * Adds a new entry to this bag.
     *
     * @param newEntry The object to be addedas a new entry
     * @return True if the addition issuccessful, or false if not.
     */
    @Override
    public boolean add(T newEntry) //OutOfMemoryError possible
    {
        // Add to beginning ofchain:
        Node newNode = newNode(newEntry);
        newNode.next =firstNode; // Make new node reference rest of chain
        // (firstNode is null ifchain is empty)      
        firstNode =newNode;      // New node is at beginningof chain
        numberOfEntries++;

        return true;
    } // end add

    /**
     * Retrieves all entries that are in thisbag.
     *
     * @return A newly allocated array of allthe entries in this bag.
     */
    @Override
    public T[] toArray() {
        // The cast is safebecause the new array contains null entries
       @SuppressWarnings(“unchecked”)
        T[] result = (T[]) newObject[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 thisbag.
    // Returns a reference to the node containingthe 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

        returncurrentNode;
    } // end getReferenceTo

    /**
     * Removes all occurrences of the givenentries
     *
     */
    @Override
    public void removeAllOccurences(T[][] entries){

        // Convert 2D arrayto 1D array

        // Remove duplicatesin array

        // Remove alloccurences of given items
  
    } // end removeAllOccurences

    private class Node {

        private T data; //Entry in bag
        private Node next; //Link to next node

        private Node(TdataPortion) {
           this(dataPortion, null);
        } // end constructor

        private Node(TdataPortion, Node nextNode) {
           data = dataPortion;
           next = nextNode;
        } // endconstructor
    } // end Node
} // end LinkedBag

~ {File 3} ~

package asmt02Part01;

/**
* A demonstration of the class LinkedBag.
*
* **************************** PLEASE DO NOT CHANGE THIS FILE***********
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @author (modifier) Duc Ta
*
*/
public class LinkedBagTester {

    public static void main(String[] args){
        System.out.println(“[+]Creating an empty bag…”);
       BagInterface<String> aBag = new LinkedBag<>();
        displayBag(aBag);

        // Addingstrings
        System.out.println(“[+]Creating bag items…”);
        String[] contentsOfBag ={“A”, ” “, ” “, “G”, “Bb”, “A”, ” “, “u”, “n”,
                                 “o”, “A”, “o”, “d”, “Bb”, “A”, “A”, “l”, “l”};
        testAdd(aBag,contentsOfBag);

        // Removing alloccurence of the given entries from a bag
        System.out.println(“[+]Creating a 2D testArray… t”);
        String[][] testArray ={
           {“A”, “A”, “A”, “A”, “A”, “A”},
           {“B”, “Bb”, “Bb”},
           {“C”, ” “},
           {“n”, “u”, “l”, “l”}
        };
        for (String[] row :testArray) {
           System.out.print(“ttttt”);
           for (String col : row) {
               System.out.print(col + ” “);
           }
           System.out.println(“”);
        }

       System.out.println(“”);
        System.out.println(“[+]Removing testArray items from the bag…”);
       aBag.removeAllOccurences(testArray);
        displayBag(aBag);

    } // end main

    // Tests the method add.
    private static voidtestAdd(BagInterface<String> aBag, String[] content) {
        System.out.print(“[+]Adding the bag items to the bag: t”);
        for (String content1 :content) {
           aBag.add(content1);
           System.out.print(content1 + ” “);
        } // end for
       System.out.println();

       displayBag(aBag);
    } // end testAdd

    // Tests the method toArray while displayingthe bag.
    private static voiddisplayBag(BagInterface<String> aBag) {
        System.out.print(“- Thebag now contains ” + aBag.getCurrentSize()
               + ” string(s): t”);
        Object[] bagArray =aBag.toArray();
        for (Object bagArray1 :bagArray) {
           System.out.print(bagArray1 + ” “);
        } // end for

       System.out.println(“n”);
    } // end displayBag

} // end LinkedBagDemo

Please add code to the provided code to define method removeAlloccurrences for the class LinkedBag. This method removes all o

Please add code to the provided code to define method removeAlloccurrences for the class LinkedBag. This method removes all occurrences of the given entries from a bag. Our program’s output must be identical to this output: [+] Creating an empty bag… – The bag now contains 0 string(s): [+] Creating bag items… [+] Adding the bag items to the bag: – The bag now contains 18 string(s): A G Bb A uno A od Bb A All 11 A A Bb do Aonu A Bb G A [+] Creating a 2D testArray… Α Α Α Α Α Α B Bb Bb nu li [+] Removing testArray items from the bag… – The bag now contains 4 string(s): Good Show transcribed image text Please add code to the provided code to define method removeAlloccurrences for the class LinkedBag. This method removes all occurrences of the given entries from a bag. Our program’s output must be identical to this output: [+] Creating an empty bag… – The bag now contains 0 string(s): [+] Creating bag items… [+] Adding the bag items to the bag: – The bag now contains 18 string(s): A G Bb A uno A od Bb A All 11 A A Bb do Aonu A Bb G A [+] Creating a 2D testArray… Α Α Α Α Α Α B Bb Bb nu li [+] Removing testArray items from the bag… – The bag now contains 4 string(s): Good

Expert Answer


Answer to ~ {File 1} ~ package asmt02Part01; /** * An interface that describes the operations of a bag of objects. * * ***********…

Description

~ {File 1} ~

package asmt02Part01;

/**
* An interface that describes the operations of a bag ofobjects.
*
* **************************** PLEASE DO NOT CHANGE THIS FILE***********
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @param <T>
*/
public interface BagInterface<T> {

    /**
     * Gets the current number of entries inthis bag.
     *
     * @return The integer number of entriescurrently in the bag.
     */
    public int getCurrentSize();

    /**
     * Sees whether this bag is empty.
     *
     * @return True if the bag is empty, orfalse if not.<... View full description