Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need assistance writing the following Java code (#6) The class DoublyLinkedNodes uses methods from the class LinkedBag and its interface BagInterface The code from

I need assistance writing the following Java code (#6)

image text in transcribed

The class DoublyLinkedNodes uses methods from the class LinkedBag and its interface BagInterface

The code from LinkedBag should be updated to use DoublyLinkedNodes and the methods described below should be added to the DoublyLinkedNodes class.

Define and implement the following methods for DoublyLinkedNodes classes.

A method union. The union of two collections consists of their contents combined into a new collection. Add a method union to the interface BagInterface for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's one argument. Include sufficient comments to fully specify the method.

Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are Bag objects, where Bag implements BagInterface; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement

BagInterface everything = bag1.union(bag2);

executes, the bag everything contains the strings a, b, b, b, c, d, and e. Note that union does not affect the contents of bag1 and bag2. Header for this method needs to be as follows: public BagInterface union(BagInterface anotherBag);

A method intersection. The intersection of two collections is a new collection of the entries that occur in both collections. That is, it contains the overlapping entries. Add a method intersection to the interface BagInterface for the ADT bag that returns as a new bag the intersection of the bag receiving the call to the method and the bag that is the method's one argument. Include sufficient comments to fully specify the method.

Note that the intersection of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the intersection of these bags contains x twice. Specifically, suppose that bag1 and bag2 are Bag objects, where Bag implements BagInterface; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement

BagInterface commonItems = bag1.intersection(bag2);

executes, the bag commonItems contains only the string b. If b had occurred twice in bag1 twice, commonItems would have contained two occurrences of b, since bag2 also contains two occurrences of b. Note that intersection does not affect the contents of bag1 and bag2.

A method difference. The difference of two collections is a new collection of the entries that would be left in one collection after removing those that also occur in the second. Add a method difference to the interface BagInterface for the ADT bag that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the method's one argument. Include sufficient comments to fully specify the method.

Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times. Specifically, suppose that bag1 and bag2 are Bag objects, where Bag implements BagInterface; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement

BagInterface leftOver1 = bag1.difference(bag2);

executes, the bag leftOver1 contains the strings a and c. After the statement

BagInterface leftOver2 = bag2.difference(bag1);

executes, the bag leftOver2 contains the strings b, d, and e. Note that difference does not affect the contents of bag1 and bag2.

A method replace that replaces and returns any object currently in a bag with a given object. Header of this method is as follows: public T replace(T replacement)

A method removeEntry that removes all occurrences of a given entry from a bag. Header of this method is as follows: public void removeEntry(T anEntry)

An equals method (by overriding Object class's method) that returns true when the contents of two bags are the same. Note that two equal bags contain the same number of entries, and each entry occurs in each bag the same number of times. The order of the entries in each array is irrelevant.

Note: You need to make sure all of the methods work without breaking the data structure; in the case of doubly linked nodes, you have to update both the forward and backward links correctly, not just the forward link as in a singly-linked nodes.

The BagInterface class

/**

* An interface that describes the operations of a bag of 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 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.

* @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 if not.

*/

public boolean contains(T anEntry);

/**

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

/**

* The union of two collections consists of their contents combined into a new collection.

* @param otherBag: The other bag that is to have its contents combined with this bag.

* @return A new bag the contains the combined contents of this bag and the other bag.

*/

public BagInterface union(BagInterface otherBag);

/**

* A new collection of the entries that occur in both collections (the overlapping entries).

* @param otherBag: The other bag is to have its contents compared to this bag.

* @return A new bag containing the overlapping entries of this bag and the other bag.

*/

public BagInterface intersection(BagInterface otherBag);

/**

* A new collection of the entries that would be left in one collection after removing

* the overlapping entries.

* @param otherBag: The other bag that is to have its contents compared to this bag.

* @return A new bag containing the contents that are left in one bag after removing the

* duplicates that occur in this bag and the other bag.

*/

public BagInterface difference(BagInterface otherBag);

}

The LinkedBag class

/**

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

* The bag is never full.

*

*/

public final class LinkedBag implements BagInterface

{

private Node firstNode; // Reference to first node

private int numberOfEntries;

/**

* Default constructor that sets initial values for private data fields.

*/

public LinkedBag()

{

firstNode = null;

numberOfEntries = 0;

}

/**

* Sees whether this bag is empty.

* @return True if this bag is empty, or false if not.

*/

public boolean isEmpty()

{

return numberOfEntries == 0;

}

/**

* Gets the number of entries currently in this bag.

* @return The integer number of entries currently in this bag.

*/

public int getCurrentSize()

{

return numberOfEntries;

}

/**

* 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) // OutOfMemoryError possible

{

Node newNode = new Node(newEntry);

newNode.next = firstNode;

firstNode = newNode;

numberOfEntries++;

return true;

}

/**

* Retrieves all entries that are in this bag.

* @return A newly allocated array of all the entries in this bag.

*/

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

{

result[index] = currentNode.data;

index++;

currentNode = currentNode.next;

}

return result;

}

/**

* 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 this bag.

*/

public int getFrequencyOf(T anEntry)

{

int frequency = 0;

int counter = 0;

Node currentNode = firstNode;

while ((counter

{

if (anEntry.equals(currentNode.data))

{

frequency++;

}

counter++;

currentNode = currentNode.next;

}

return frequency;

}

/**

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

{

boolean found = false;

Node currentNode = firstNode;

while (!found && (currentNode != null))

{

if (anEntry.equals(currentNode.data))

{

found = true;

}

else

{

currentNode = currentNode.next;

}

}

return found;

}

/**

* Locates a given entry within this bag.

* @param anEntry: The entry to locate.

* @return 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;

}

}

return currentNode;

}

/**

* Removes all entries from this bag.

*/

public void clear()

{

while (!isEmpty())

{

remove();

}

}

/**

* Removes one unspecified entry from this bag, if possible.

* @return Either the removed entry, if the removal was successful, or null.

*/

public T remove()

{

T result = null;

if (firstNode != null)

{

result = firstNode.data;

firstNode = firstNode.next; // Remove first node from chain

numberOfEntries--;

}

return result;

}

/**

* 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 otherwise.

*/

public boolean remove(T anEntry)

{

boolean result = false;

Node nodeN = getReferenceTo(anEntry);

if (nodeN != null)

{

nodeN.data = firstNode.data; // Replace located entry with entry in first node

firstNode = firstNode.next; // Remove first node

numberOfEntries--;

result = true;

}

return result;

}

/**

* Class for the node.

* @author DARNESHA RANDLE

*

*/

private class Node

{

private T data; // Entry in bag

private Node next; // Link to next node

/**

* Default constructor to set initial values

* @param dataPortion

*/

private Node(T dataPortion)

{

this(dataPortion, null);

}

/**

* Constructor to set initial values

* @param dataPortion

* @param nextNode

*/

private Node(T dataPortion, Node nextNode)

{

data = dataPortion;

next = nextNode;

}

} // end Node class

}// end LinkedBag class

6. In a doubly linked chain, each node can reference the previous node as well as the next node. Below figure shows a doubly linked chain and its head reference. firstNode next next next next NULL prev prev prev prev Define a class to represent a node in a doubly linked chain. Write the class as an inner class named DoublyLinkedNode of a class named DoublyLinkedBag that implements the latest version of interface Baglnterface. Also, you need to implement the replace, removeEntry, and equals methods as defined above for this class as well 6. In a doubly linked chain, each node can reference the previous node as well as the next node. Below figure shows a doubly linked chain and its head reference. firstNode next next next next NULL prev prev prev prev Define a class to represent a node in a doubly linked chain. Write the class as an inner class named DoublyLinkedNode of a class named DoublyLinkedBag that implements the latest version of interface Baglnterface. Also, you need to implement the replace, removeEntry, and equals methods as defined above for this class as well

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

9.12 Kongsberg Maritime, a Norwegian technology enterprise,

Answered: 1 week ago