Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */

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(" --> Clearing set1"); set1.clear(); set1.displaySet(); System.out.println("--> set1 isEmpty returns: " + set1.isEmpty()); System.out.println(" --> Creating set2 and set3"); SetInterface set2 = new LinkedSetWithLinkedBag<>(); SetInterface set3 = new LinkedSetWithLinkedBag<>(); System.out.println(" --> 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(" --> 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(" --> set2 contains \"A\": " + set2.contains("A")); System.out.println("--> set2 contains \"E\": " + set2.contains("E")); System.out.println(" --> Removing \"B\" from set2"); set2.removeElement("B"); System.out.println("--> After removing \"B\" from set2, "); set2.displaySet(); System.out.println(" --> Removing random element from set2"); String removed = set2.remove(); System.out.println("--> set2.remove() returned: \"" + removed + "\""); set2.displaySet(); System.out.println(" --> Removing \"A\" from set2"); set2.removeElement("A"); System.out.println("--> After removing \"A\" from set2, "); set2.displaySet(); System.out.println(" --> Removing random element from set2"); removed = set2.remove(); System.out.println("--> set2.remove() returned: \"" + removed + "\""); set2.displaySet(); System.out.println(" --> 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(" --> 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(" Trying 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

Question:

Please complete the TO DO

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

Step: 3

blur-text-image

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

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions