import java.util.*;
/**
* A class that implements the ADT set by using a linked bag.
* The set is never full.
*
*
*/
public class LinkedSetWithLinkedBag> implements SetInterface {
private LinkedBag setOfEntries;
/**
* Creates a set from a new, empty linked bag.
*/
public LinkedSetWithLinkedBag() {
//TODO Project1
} // end default constructor
public boolean add(T newEntry) {
//TODO Project1
// new node is at beginning of chain
if(this.setOfEntries.isEmpty()) {
if (!this.setOfEntries.contains(newEntry))
this.setOfEntries.add(newEntry);
}
return true;
// return false; //THIS IS A STUB
} // end add
public T[] toArray() {
//TODO Project1
return null; //THIS IS A STUB
} // end toArray
public boolean isEmpty() {
//TODO Project1
return false; //THIS IS A STUB
} // end isEmpty
public boolean contains(T anEntry) {
//TODO Project1
return false; //THIS IS A STUB
} // end contains
public void clear() {
//TODO Project1
} // end clear
public T remove() {
//TODO Project1
return null; //THIS IS A STUB
} // end remove
public boolean removeElement(T anEntry) {
//TODO Project1
return false; //THIS IS A STUB
} // end remove
// Displays a set.
public void displaySet() {
//TODO Project1
} // end displaySet
public static void main(String[] args) {
String[] inputData = {"A", "B", "C", "D", "A", "C", "B", "B"};
System.out.println("--> Creating set1 and adding to it elements from inputData: " + Arrays.toString(inputData));
SetInterface set1 = new LinkedSetWithLinkedBag<>();
for (int i = 0; i < inputData.length; i++) {
set1.add(inputData[i]);
}
System.out.println("--> Calling displaySet method to display elements in set1");
set1.displaySet();
System.out.println("--> Calling displaySet method to display elements in set1 one more time");
set1.displaySet();
System.out.println("\n--> Clearing set1");
set1.clear();
set1.displaySet();
System.out.println("--> set1 isEmpty returns: " + set1.isEmpty());
System.out.println("\n--> Creating set2 and set3");
SetInterface set2 = new LinkedSetWithLinkedBag<>();
SetInterface set3 = new LinkedSetWithLinkedBag<>();
System.out.println("\n--> Adding elements to set2");
set2.add("A");
set2.add("A");
set2.add("B");
set2.add("A");
set2.add("C");
set2.add("A");
System.out.println("--> set2 after adding elements");
set2.displaySet();
System.out.println("\n--> Adding elements to set3");
set3.add("A");
set3.add("B");
set3.add("B");
set3.add("A");
set3.add("C");
set3.add("C");
set3.add("D");
System.out.println("--> set3 after adding elements");
set3.displaySet();
System.out.println("\n--> set2 contains \"A\": " + set2.contains("A"));
System.out.println("--> set2 contains \"E\": " + set2.contains("E"));
System.out.println("\n--> Removing \"B\" from set2");
set2.removeElement("B");
System.out.println("--> After removing \"B\" from set2, ");
set2.displaySet();
System.out.println("\n--> Removing random element from set2");
String removed = set2.remove();
System.out.println("--> set2.remove() returned: \"" + removed + "\"");
set2.displaySet();
System.out.println("\n--> Removing \"A\" from set2");
set2.removeElement("A");
System.out.println("--> After removing \"A\" from set2, ");
set2.displaySet();
System.out.println("\n--> Removing random element from set2");
removed = set2.remove();
System.out.println("--> set2.remove() returned: \"" + removed + "\"");
set2.displaySet();
System.out.println("\n--> Adding 4 elements to set2");
set2.add("K");
set2.add("L");
set2.add("M");
set2.add("N");
System.out.println("--> After adding 4 elements to set2:");
set2.displaySet();
System.out.println("\n--> Trying to add duplicate element \"N\" to set2");
set2.add("N");
System.out.println("--> After adding a duplicate element \"N\" to set2");
set2.displaySet();
System.out.println("\nTrying to add null entry");
String nullEntry = null;
set2.add(nullEntry);
System.out.println("--> set2 after adding:");
set2.displaySet();
} // end main
} // end LinkedSetWithLinkedBag
Sample Run:
--> Creating set1 and adding to it elements from inputData: [A, B, C, D, A, C, B, B]
--> Calling displaySet method to display elements in set1
The set contains 4 element(s), as follows:
D C B A
--> Calling displaySet method to display elements in set1 one more time
The set contains 4 element(s), as follows:
D C B A
--> Clearing set1
The set is empty
--> set1 isEmpty returns: true
--> Creating set2 and set3
--> Adding elements to set2
--> set2 after adding elements
The set contains 3 element(s), as follows:
C B A
--> Adding elements to set3
--> set3 after adding elements
The set contains 4 element(s), as follows:
D C B A
--> set2 contains "A": true
--> set2 contains "E": false
--> Removing "B" from set2
--> After removing "B" from set2,
The set contains 2 element(s), as follows:
C A
--> Removing random element from set2
--> set2.remove() returned: "C"
The set contains 1 element(s), as follows:
A
--> Removing "A" from set2
--> After removing "A" from set2,
The set is empty
Here is the answer...
CODE:
import java.util.*;
class Creation{
static void displaySet(HashSet<String> set)
{
Iterator<String>
i=set.iterator();
while(i.hasNext())
{
System.out.print(i.next()+"\t");
}
}
public static void main(String[] args) {
HashSet<String> set1 = new
HashSet();
set1.addAll(Arrays.asList(new
String[] {"A","B","C","D","A","C","B","B"}));
displaySet(set1);
set1.clear();
System.out.println("\nisEmpty() : "
+ set1.isEmpty());
HashSet<String> set2 = new
HashSet();
HashSet<String> set3 = new
HashSet();
set2.add("C");
set2.add("B");
set2.add("A");
displaySet(set2);
System.out.println();
set3.addAll(Arrays.asList(new
String[] {"D","C","B","A"}));
displaySet(set3);
System.out.println("\ncontains A :
" + set2.contains("A"));
System.out.println("\ncontains E :
" + set2.contains("E"));
set2.remove("B");
displaySet(set2);
System.out.println();
set2.remove("C");
//we have to pass object to remove method it
doesn't remove random elements
displaySet(set2);
System.out.println();
set2.remove("A");
System.out.println("Finally set2
isEmpty: "+set2.isEmpty());
}
}
OUTPUT:

If you have any doubts please COMMENT...
If you understand the answer please give THUMBS UP...
import java.util.*; /** * A class that implements the ADT set by using a linked bag....
A set is a special kind of list, one that does not allow repeated, or duplicate, entries. Whenever you must process an item in a data collection only once, you can use a set. For example, a compiler must find the identifiers in a program and ensure that each one has been defined only once. It could add each identifier encountered to a set. If this addition is unsuccessful, the compiler will have detected an identifier previously found. Remark: There...
I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...
In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to thenext Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Data Next Node Then we will design another class named LinkedBag (LinkedBag.java). LinkedBag class has an instance variable called firstNode of...
Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...
Write a complete bag class implementation using linked implementation. The linked bag class name must be LinkedBag and name your test program as LinkedBagDemo. Your test program should include following test conditions: 1. Get the number of items currently in the bag 2. See whether the bag is full 3. See whether the bag is empty 4. Add a given object to the bag 5. Remove an unspecified (not random) object from the bag 6. Remove an occurrence of a...
public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The list works like this: entries can only be added to and removed from the beginning or end of the list entries can be accessed in any position entries begin at index 0 Write a class that implements this interface. The class uses arrays to implement the list. Your class header and instance data variables will be: public class ArrayHeadTailList<T> implements HeadTailListInterface<T> private T[] listArray;...
Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag. Be sure to include a default constructor to initialize the private members of the class. Test that your code works properly. BagInterface includes the methods: public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public boolean remove(T anEntry); public void clear(); public int getFrequencyOf(T anEntry); public boolean contains(T anEntry); public T[] toArray();
Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...
Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...
Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...