Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help for the following in the removeAllOccurence method? rate high a

ID: 3721998 • Letter: C

Question


can someone help for the following in the removeAllOccurence method?
rate high as much as I can after

// Convert 2D array to 1D array


// Remove duplicates in array


// Remove all occurences of given items


there are the given code

===========================BagInterface.java===================================


package asmt02part1;


/**
* An interface that describes the operations of a bag of objects.
*
* **************************** PLEASE DO NOT CHANGE THIS FILE ***********
*

* @param
*/
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 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 all occurrences of the given entries
*
* @param entries
*/
public void removeAllOccurences(T[][] entries);


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


} // end BagInterface


=================================LinkedBag.java================================


package asmt02part1;


import java.util.Arrays;


/**


* A class of bags whose entries are stored in a chain of linked nodes. The bag


* is never full.


*


* @param


*/


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


==================================LinkedBagTester.java==========================


package asmt02part1;


/**
* A demonstration of the class LinkedBag.
*
* **************************** PLEASE DO NOT CHANGE THIS FILE ***********
*
*/
public class LinkedBagTester {


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


// Adding strings
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 all occurence of the given entries from a bag
System.out.println("[+] Creating a 2D testArray... ");
String[][] testArray = {
{"A", "A", "A", "A", "A", "A"},
{"B", "Bb", "Bb"},
{"C", " "},
{"n", "u", "l", "l"}
};
for (String[] row : testArray) {
System.out.print(" ");
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 void testAdd(BagInterface aBag, String[] content) {
System.out.print("[+] Adding the bag items to the bag: ");
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 displaying the bag.
private static void displayBag(BagInterface aBag) {
System.out.print("- The bag now contains " + aBag.getCurrentSize()
+ " string(s): ");
Object[] bagArray = aBag.toArray();
for (Object bagArray1 : bagArray) {
System.out.print(bagArray1 + " ");
} // end for


System.out.println(" ");
} // end displayBag


} // end LinkedBagDemo


Define the method removeA11occurrences for the class LinkedBag that removes all occurrences of the given entries from a bag. Your program's output must be identical to this output: 1 creating an empty bag. - The bag now contains 0 string (s): [+ Creating bag items. + Adding the bag items to the bag: A G Bb A un oA od BbAA1 1 A. +1 Creating a 2D testArray... B Bb Bb n ul1 +1 Removing testArray items from the bag.. - The bag now contains 4 string (s)Go od

Explanation / Answer

Solution :

To Convert 2D array into 1D using java, the code is written below :

public class SingleSize {

public static void main(String[] args) {

// Create new Scanner and Random and Decimal objects

        Scanner s = new Scanner (System.in);

        Random g = new Random ();

        DecimalFormat DecimalFormat (".000");

         

        // Create integers to store user input # of rows and columns

        int rows;

        int columns;

        // User prompts

        System.out.print ("How many rows in the array? ");

        rows = s.nextInt();

        System.out.print ("How many columns in the array? ");

        columns = s.nextInt();

         

        // Create Multidimensional array

        double [][] array2d = new double [rows][columns];

         

        // Generate random numbers to fill array

        for (int i = 0; i < array2d.length; i++){

             

            for (int j = 0; j < array2d[i].length; j++){

                 

                array2d[i][j] = Math.random() * 100;

           // Fill array with decimal numbers between 0 and 100

            }

        }

        System.out.print (" This 2D array contains: ");

         

        for (int k = 0; k < array2d.length; k++){

            for (int l = 0; l < array2d[k].length; l++){    

                System.out.print (oneplaces.format(array2d[k][l]) + ", ");

                // Print 2D array of rows and columns

            }

            System.out.println ();

           // Organize array into a chart

        }

         

        System.out.println (" Converted to a 1D array: ");

         

        int count = 0; // Create counter variables

        int count2 = 0;

         

        for (int m = 0; m < array2d.length; m++){

           // Determine length of 2D array

            count += array2d[m].length;

        }

     

            double [] array1d = new double[count];

          // Initialize 1D array

     

            for (int m = 0; m < array2d.length; m++){

                System.arraycopy (array2d[m], 0, array1d, count2, array2d[m].length);

                // Copy 2D array's contents to 1D array elements

                count2 += array2d[m].length;

                System.out.print (oneplaces.format(array1d[m]) + ", ");

               // Print out 1d array with 2d array's variables

            }

    }  

}

try this out and check if its working fine or not. Now,

For removing duplicates from the array.

There are three ways with which we can remove duplicates from array

Choose the way u want and comment ur selection, i will post the way which u want for removing duplicates.

Thirdly, to remove all the occurences of the given items, you just have to add 1 statememt, i.e.

or if you are using java 8, then definitely this method

=======================================================================================

# To remove duplicate elements in the array using temporary array

public class removeSimilarElementsInArray {

public static int removeSimilarElements(int arr[], int n){

if (n==0 || n==1){  

            return n;  

        }  

        int[] temp = new int[n];  

        int j = 0;  

        for (int i=0; i<n-1; i++){  

            if (arr[i] != arr[i+1]){  

                temp[j++] = arr[i];  

            }  

         }  

        temp[j++] = arr[n-1];  

// Changing original array  

        for (int i=0; i<j; i++){  

            arr[i] = temp[i];  

        }  

        return j;  

    }  

public static void main (String[] args) {  

        int arr[] = {10,20,20,30,30,40,50,50};  

        int length = arr.length;  

        length = removeSimilarElements(arr, length);  

        //printing array elements  

        for (int i=0; i<length; i++)  

           System.out.print(arr[i]+" ");  

    }  

}  

Note : To remove duplicates the array must be in sorted order. If not sorted, you have to sort it by calling Array.sort(arr) method.

# To remove duplicates using seperate index :

public class removeDuplicatesInArray {

public static int removeDuplicates(int arr[], int n){  

        if (n==0 || n==1){  

            return n;  

}    

        int j = 0;//for next element  

        for (int i=0; i < n-1; i++){  

            if (arr[i] != arr[i+1]){  

                arr[j++] = arr[i];  

            }  

        }  

        arr[j++] = arr[n-1];  

        return j;  

    }  

public static void main (String[] args) {  

        int arr[] = {10,20,20,30,30,40,50,50};  

        int length = arr.length;  

        length = removeDuplicates(arr, length);  

        //printing array elements  

        for (int i=0; i<length; i++)  

           System.out.print(arr[i]+" ");  

    }  

===========================================================================================

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote