Question: Fill out the methods and add the following strings: D A C A B A. Then remove A. print number of A. /** An interface

Fill out the methods and add the following strings: D A C A B A. Then remove A. print number of A.

/**

An interface that describes the public operations of a bag of arbitraty objects.

*/

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 full.

* @return true if the bag is full, or false if not */

public boolean isFull();

/** 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 one unspecified entry from this bag, if possible.

* @return either the removed entry, if the removal was successful, or null */

public T 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 if not */

public boolean remove(T anEntry);

/** Removes all entries from this bag. */

public void 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 the bag */

public int getFrequencyOf(T anEntry);

/** 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);

/** Creates an array of all entries that are in this bag.

* @return a newly allocated array of all the entries in the bag */

public T[] toArray();

}//End Bag Interface

------------------------------------------

public class LinkedBag < T > implements BagInterface < T >

{

private Node head; // reference to first node

private int numberOfEntries; // how many items are in the bag

public LinkedBag ()

{

// need to fill it

} // end default constructor

// Implementations of the public methods declared in BagInterface go here.

// @result the current numberOfEntries

@Override

public int getCurrentSize() {

return numberOfEntries;

}

@Override

public boolean isFull()

{

return false;

// as long as Java virtual machine create a new node (i.e. your computer memory is not full), it is not full.

}

@Override

public boolean isEmpty() {

if (head==null)

return true;

else return false;

}

// @param T a data entry to add in the list

// @result return true if you add it successfully

@Override

public boolean add(T newEntry) {

//need to fill it

}

// remove the 1st item in the list

// @result return the data entry removed, return null if the list is empty

@Override

public T remove() {

//need to fill it

}

// remove the item in the list whose data is anEntry

// @param an Entry to remove.. Please note that the entry could be at any place in the list

// @result return true if you remove the entry successfully, false otherwise

@Override

public boolean remove(T anEntry) {

boolean result=false;

Node p=getReferenceTo(anEntry); // find a reference to the node which has the data entry

if (p!=null) {

p.setData(head.getData()); //nodeN.data=head.data;

remove(); // remove the first item in the list

result=true;

}

return result;

}

// @param : an Entry to locate

// @result : return the reference to the entry if you found the entry, or null if not found

private Node getReferenceTo (T anEntry) {

//need to fill it

}

@Override

public void clear() {

while (!isEmpty())

remove();

}

// @param : an Entry to figure out if it is inside the list

// @result : return true if the entry is in the list, false otherwise

@Override

public boolean contains(T anEntry)

{

if ( getReferenceTo(anEntry) != null )

return true;

else return false;

}

// copy the data entries in the list into the array

// @result : return the array

@Override

public T[] toArray() {

T[] result = (T[]) new Object[numberOfEntries];

Node p=head;

for (int index=0; index < numberOfEntries ; index++ ) {

result[index] = (T) p.getData();

p=p.getNextNode();

}

return result;

}

// @param : an Entry to count the number of occurrences

// @result : return the number of occurrences.

@Override

public int getFrequencyOf(T anEntry) {

int frequency=0;

int counter =0;

Node p=head;

while (counter < numberOfEntries) {

if (anEntry.equals(p.getData()))

frequency++;

counter++;

p=p.getNextNode();

}

return frequency;

}

} // end LinkedBag

-----------------------------------------

public class LinkedBagDemo1

{

public static void main (String [] args)

{

BagInterface < String > aBag = new LinkedBag < String > ();

//TestCase1

testToArray (aBag); // you should print empty string here

System.out.println (" ");

//TestCase2

String [] contentsOfBag = {"A", "D", "B", "A", "C", "A", "D"};

testAdd (aBag, contentsOfBag); // The content should have D A C A B D A

System.out.println (" ");

//TestCase3

aBag.remove(); // The content should have A C A B D A

testToArray (aBag);

System.out.println (" ");

//TestCase4

aBag.remove ("A"); // The content should have C A B D A

testToArray (aBag);

System.out.println (" ");

//TestCase5

int numItems=aBag.getFrequencyOf ("A");

System.out.println ("Number of A " + numItems); // how many "A" is there? 2

System.out.println (" ");

//TestCase6

boolean result=aBag.contains ("Z");

if (result)

System.out.println (" item is in the list...");

else System.out.println ("item is NOT in the list..."); // "item is not in the list" should be shown.

} // end main

// Tests the method add.

private static void testAdd (BagInterface < String > aBag,

String [] content)

{

System.out.print ("Adding to the bag: ");

for (int index = 0 ; index < content.length ; index++)

{

aBag.add (content [index]); //{"A", "D", "B", "A", "C", "A", "D"};

}

testToArray (aBag);

} // end testAdd

// Tests the method toArray while displaying the bag.

private static void testToArray (BagInterface < String > aBag)

{

System.out.println ("The bag contains the following string(s):");

Object [] bagArray = aBag.toArray ();

for (int index = 0 ; index < bagArray.length ; index++)

{

System.out.print (bagArray [index] + " ");

} // end for

System.out.println ();

} // end displayBag

} // end LinkedBagDemo1

---------------------------------------

class Node < T >

{

private T data;

private Node < T > next;

Node (T dataPortion)

{

this (dataPortion, null);

}

Node (T dataPortion, Node < T > nextNode)

{

data = dataPortion;

next = nextNode;

}

T getData ()

{

return data;

}

void setData (T newData)

{

data = newData;

}

Node < T > getNextNode ()

{

return next;

}

void setNextNode (Node < T > nextNode)

{

next = nextNode;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!