Question

A set is a special kind of list, one that does not allow repeated, or duplicate,...

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 is no particular ordering of items in set.

IntSet.java file

/** An interface that describes the operations of a set of objects. */

public interface IntSet {

            public int size();

            public boolean isEmpty();

/** Adds a new entry to this set, avoiding duplicates.

@param newEntry The object to be added as a new entry.

@return True if the addition is successful, or

false if the item already is in the set. */

public boolean add(int newEntry);

/** Removes a specific entry from this set, if possible.

@param anEntry The entry to be removed.

@return True if the removal was successful, or false if not. */

public boolean remove(int anEntry);

public void clear();

public boolean contains(int anEntry); // return true if anEntry is currently in the Set

public int[] toArray(); //return all the items currently in the list as an array

public String toString():// returns the content of set as a string

}

Task 1:

Step 1: Create IntSet.java interface and save it as IntSet.java file

Step 2:Imlement IntSet Java interface using an Array. Create ArrayBasedSet.java and complete the following class definition.

public class ArrayBasedSet implements IntSet{

// your implementation details and methods

}

Step 3: Implement Application.java that uses IntSet to perform the following operations in its main method:

            IntSet set1=new ArrayBasedSet();

            set1.add(33);

            set1.add(23);

            set1.add(14);

            set1.add(1);

            set1.add(2);

            set1.add(23);

            set1.add(18);

            set1.add(33);

            set1.add(33);

            set1.add(33);

            System.out.println(set1); //set1.toString is invoked automatically

            if(set1.contains(44))

                        System.out.println(“Set1 contains 44”);

            else

                        System.out.println(“Set1 doesn’ttcontain 44”);

            set1.remove(23);

            System.out.println(set1);

            int []items=set1.toArray();

for(int a:items)

                        System.out.println(a);  

}

Task 2:

Step 1:Create Node.java that contains Node class

Step 2: Create IntSet.java interface and save it as IntSet.java file

Step 3:Imlement IntSet Java interface using Linked Structures. Create LinkedBaseSet.java and complete the following class definition.

public class LinkedBasedSet implements IntSet{

}

Step 4: Implement Application.java that uses IntSet to perform the following operations in its main method:

            IntSet set1=new LinkedBaseSet ();

            set1.add(33);

            set1.add(23);

            set1.add(14);

            set1.add(1);

            set1.add(2);

            set1.add(23);

            set1.add(18);

            set1.add(33);

            set1.add(33);

            set1.add(33);

            System.out.println(set1);//set1.toString is invoked automatically

            if(set1.contains(44))

                        System.out.println(“Set1 contains 44”);

            else

                        System.out.println(“Set1 doesn’ttcontain 44”);

            set1.remove(23);

            System.out.println(set1);

            int []items=set1.toArray();

            for(int a:items)

                        System.out.println(a);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File Hierarchy:

/IntSet/IntSet.java

/Node/Node.java

/Application.java

/LinkedBaseSet.java

/ArrayBasedSet.java

IntSet.java:

package IntSet;

public interface IntSet {

            public int size();

            public boolean isEmpty();

       /** Adds a new entry to this set, avoiding duplicates.

       @param newEntry The object to be added as a new entry.

       @return True if the addition is successful, or

       false if the item already is in the set. */

       public boolean add(int newEntry);

       /** Removes a specific entry from this set, if possible.

       @param anEntry The entry to be removed.

       @return True if the removal was successful, or false if not. */

       public boolean remove(int anEntry);//removes the given entry from the list.

       public void clear();//clears the set

       public boolean contains(int anEntry); // return true if anEntry is currently in the Set

       public int[] toArray(); //return all the items currently in the list as an array

       public String toString();// returns the content of set as a string

}

ArrayBasedSet.java:

import IntSet.*;
import java.util.Arrays;

public class ArrayBasedSet implements IntSet{

// your implementation details and methods
   private int[] set;

   ArrayBasedSet()
   {
       //initialize the set array
       set = new int[0];
   }

  
   public int size()
   {
       //return length of set
       return set.length;
   }

   public boolean isEmpty()
   {
       //return true of set is empty and false if not empty
       return (set.length==0);
   }

   public boolean add(int newEntry){

       //returns false if newEntry is already found
       if(contains(newEntry))
       return false;

       //Increments the size of the array
       set = Arrays.copyOf(set,set.length+1);
       //inserts the new value at end of the array
       set[set.length-1]=newEntry;
       return true;
   }

   public boolean remove(int anEntry){
       if(contains(anEntry))
       // if anEntry is found in the set then search for it's index
       for(int i=0;i<set.length;i++)
       {
           if(set[i]==anEntry)
           {
               for(int j=i;j<set.length-1;j++)
               {
                   //deletes the current element by moving all it's succeeders to previous location.
                   set[j]=set[j+1];
               }
               //decrements the size of array.
               set = Arrays.copyOf(set, set.length-1);
               return true;
           }
       }
       //returns false if array doesn't contain the anEntry
       return false;
   }

   public void clear(){
       // clears the set array
       set = new int[0];
   }

   public boolean contains(int anEntry){
       for(int i=0;i<set.length;i++)
       {
           //checks for all values in set and if it is equal to the given anEntry returns true.
           if(set[i]==anEntry) return true;
       }
       //returns flase if not found.
       return false;
   }

   public int[] toArray(){
       //returns the set array as it is.
       return set;
   }

   public String toString(){
       //creates an empty string
       String s=new String();
       for(int i=0;i<set.length;i++)
       {
           //converts each value to string and appends it to existing string.
           s += String.valueOf(set[i])+" ";
       }

       //returns the whole string
       return s;
   }

}

Node.java:

package Node;

//imports LinkedList class from java.util package
import java.util.LinkedList;

public class Node
{
   //creates an empty list.
   public LinkedList<Integer> list;
   public Node()
   {
       //creates new object of the empty list.
       list = new LinkedList<Integer>();
   }
}

LinkedBaseSet.java:

import IntSet.*;
import Node.*;
import java.util.Arrays;

public class LinkedBaseSet implements IntSet{

// your implementation details and methods
   private Node set;

   LinkedBaseSet()
   {
       //creates an empty Node and assigns it to the set.
       set = new Node();
   }

   public int size()
   {
       //return the size of the list in the Node class which is a linked list.
       return set.list.size();
   }

   public boolean isEmpty()
   {
       //return true if the length is 0 i.e., empty. otherwise returns flase.
       return (set.list.size()==0);
   }

   public boolean add(int newEntry){

       if(contains(newEntry))
       //return false if the element is already present in list.
       return false;
       //adds the new entry into the list in the set.
       set.list.add(newEntry);
       return true;
   }

   public boolean remove(int anEntry){
       if(contains(anEntry))
       //return true if the given entry present int he list.
       return set.list.remove(new Integer(anEntry));

       //returns flase otherwise.
       return false;
   }

   public void clear(){
       //clears the list.
       set.list.clear();
   }

   public boolean contains(int anEntry){
       //returns true if the list contains the given entry.
       return set.list.contains(anEntry);
   }

   public int[] toArray(){

       //size for storing the size of the linked list.
       int size = set.list.size();
       //creating an array to store the output.
       int[] arrset = new int[size];
       //storing the object type to Integer array
       Integer[] ret = set.list.toArray(new Integer[size]);
       for (int i = 0; i < size; ++i) {
           //assigning values to the arrset.
           arrset[i] = ret[i];
       }
       //returning the arrset
       return arrset;
   }

   public String toString(){
       //returning the string by converting the data in our list to a string.
       return set.list.toString();
   }

}

Application.java:

import IntSet.*;

public class Application
{
   public static void main(String[] args) {
       System.out.println("ArrayBasedSet");
       IntSet set1=new ArrayBasedSet();

            set1.add(33);

            set1.add(23);

            set1.add(14);

            set1.add(1);

            set1.add(2);

            set1.add(23);

            set1.add(18);

            set1.add(33);

            set1.add(33);

            set1.add(33);

            System.out.println(set1); //set1.toString is invoked automatically

            if(set1.contains(44))

                        System.out.println("Set1 contains 44");

            else

                        System.out.println("Set1 doesn'ttcontain 44");

            set1.remove(23);

            System.out.println(set1);

            int []items=set1.toArray();

           for(int a:items)

                        System.out.println(a);
                       
        System.out.println("LinkedBasedSet");
        IntSet set2=new LinkedBaseSet();

            set2.add(33);

            set2.add(23);

            set2.add(14);

            set2.add(1);

            set2.add(2);

            set2.add(23);

            set2.add(18);

            set2.add(33);

            set2.add(33);
            set2.add(33);

            System.out.println(set2); //set1.toString is invoked automatically

            if(set2.contains(44))

                        System.out.println("Set1 contains 44");

            else

                        System.out.println("Set1 doesn'ttcontain 44");

            set2.remove(23);

            System.out.println(set2);

            int []items1=set2.toArray();

           for(int a:items1)

                        System.out.println(a);
   }                  
}

output:

Hope you got a good idea about what's done.

If you have any doubts please comment in comment section.

Add a comment
Know the answer?
Add Answer to:
A set is a special kind of list, one that does not allow repeated, or duplicate,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    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; //...

  • Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag....

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

  • Write a complete bag class implementation using linked implementation. The linked bag class name must be...

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

  • Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main...

    Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...

  • c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are...

    c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are given an interface: SetInterfac This is a public interface and completely specifies what the Set class operations must be. SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods. Set differs in the fact that it does not allow duplicates. This also means that add()must check that an element is not...

  • Study the provided BagInterface and the methods implemented in the ResizableArrayBag For the following methods that...

    Study the provided BagInterface and the methods implemented in the ResizableArrayBag For the following methods that you are to implement in ResizableArrayBag for Project3 of Lab02:  equals, removeMax, removeEvery, replace, union, intersection, difference, getAllLessThan, isSubset give an algorithm in pseudocode draw memory diagrams showing how the respective bags will look like as the result of the given method call public interface BagInterface<T extends Comparable<? super T>> { public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public...

  • Define a class ArraySet using an array that represents a set and implements the ADT Set....

    Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...

  • Suppose that you have several numbered billiard balls on a pool table. The smallest possible number...

    Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball, you replace it with balls numbered “2”, “1”, “1”, “4”, and “3”, where numbers 2, 1, 1, 4, and 3 were randomly...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The...

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT