Question
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
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 doesnttcontain 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 doesnttcontain 44);
set1.remove(23);
System.out.println(set1);
int []items=set1.toArray();
for(int a:items)
System.out.println(a);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started