Question
A set is an unordered data structure like a bag, but with the restriction that no duplicates are allowed. Implement the add() method for ArraySet
A set is an unordered data structure like a bag, but with the restriction that no duplicates are allowed. Implement the add() method for ArraySet and LinkedSet. The method should add the item to the set only if it is not already present. It should return true if the item was successfully added, false otherwise:
boolean add (T item); Test your method using the driver program Sets.java. The output should look like this:
Here's what's in our set [orange, grape, kiwi, coconut, lime]
Let's add a banana Was the operation successful? yes Our set looks like this [orange, grape, kiwi, coconut, lime, banana]
Let's try to add another orange Was the operation successful? no Our set looks like this [orange, grape, kiwi, coconut, lime, banana]
Trying to add null to our set null not a possible value!
Code is below
/*
* Sets
*
* program for testing add() method in ArraySet and LinkedSet
*/
public class Sets
{
public static void main (String [] args)
{
try
{
Set
//Set
// create set of fruits
String [] fruits = {"orange", "grape", "kiwi", "coconut", "lime"};
for (int i = 0; i < fruits.length; i++)
{
words.add(fruits[i]);
}
System.out.println(" Here's what's in our set " + words);
// add new item
System.out.println(" Let's add a banana");
String result = words.add("banana") ? "yes" : "no";
System.out.println("Was the operation successful? " + result);
System.out.println("Our set looks like this " + words);
// try to add item already present
System.out.println(" Let's try to add another orange");
result = words.add("orange") ? "yes" : "no";
System.out.println("Was the operation successful? " + result);
System.out.println("Our set looks like this " + words);
// try to add null to set
System.out.println(" Trying to add null to our set");
words.add(null);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
}
/*
* interface for a set
*
* unordered collection, no duplicates allowed
*/
public interface Set
{
/*
* adds item to set if not already present
*
* returns true if item successfully added, false if not
*
*/
boolean add (T item);
T remove();
T get();
boolean contains (T item);
int size();
String toString();
}
/*
* ArraySet
*
* array-based implementation of a set
*
* unordered collection with no duplicates allowed
*/
public class ArraySetimplements Set
{
public static final int DEFAULT_CAPACITY = 10;
private T [] set;
private int size;
/*
* default constructor
*
* set initially set to 10 items
*/
public ArraySet()
{
this(DEFAULT_CAPACITY);
}
/*
* argument constructor
*
* size of set specified by user
*/
@SuppressWarnings("unchecked")
public ArraySet(int capacity)
{
if (capacity <= 0)
{
throw new IllegalArgumentException("size must be positive!");
}
set = (T []) new Object [capacity];
size = 0;
}
/*
* adds item to set if not already present
*
* returns true if item successfully added, false if not
*/
public boolean add (T item)
{
// to be implemented
}
/*
* removes item from set
*
* returns removed item
*/
public T remove ()
{
if (size == 0)
{
return null;
}
T removed = set[size-1];
set[size-1] = null;
size--;
return removed;
}
/*
* returns item from set
*/
public T get()
{
if (size == 0)
{
return null;
}
return set[size-1];
}
/*
* returns true if item is in set, false otherwise
*/
public boolean contains(T item)
{
for (int i = 0; i < size; i++)
{
if (set[i].equals(item))
{
return true;
}
}
return false;
}
/*
* returns size of set
*/
public int size()
{
return size;
}
/*
* returns string representation of contents of set
*/
public String toString()
{
if (size == 0)
{
return "[ ]";
}
String s = "[";
for (int i = 0; i < size()-1; i++)
{
s+= set[i] + ", ";
}
return s + set[size-1] + "]";
}
/*
* checks if item is null and throws exception if so
*/
private void checkForNull (T item)
{
if (item == null)
{
throw new IllegalArgumentException("null not a possible value!");
}
}
/*
* doubles size of array if at capacity
*/
private void ensureSpace()
{
if (size == set.length)
{
@SuppressWarnings("unchecked")
T [] larger = (T []) new Object [size * 2];
for (int i = 0; i < size; i++)
{
larger[i] = set[i];
}
set = larger;
larger = null;
}
}
}
/*
* LinkedSet
*
* linked structure implementation of a set
*
* unordered collection with no duplicates allowed
*/
public class LinkedSetimplements Set
{
/*
* inner class to represent node
*/
private class Node
{
private T data;
private Node next;
public Node(T item)
{
data = item;
next = null;
}
}
private Node head;
private int size;
/*
* adds item to bag if not already present
*
* returns true if item successfully added, false if not
*/
public boolean add(T item)
{
// to be implemented
}
/*
* removes item from set
*
* returns removed item
*/
public T remove()
{
if (size == 0)
{
return null;
}
T removed = head.data;
head = head.next;
size--;
return removed;
}
/*
* returns item from set
*/
public T get()
{
if (size == 0)
{
return null;
}
return head.data;
}
/*
* returns true if item is in set, false otherwise
*/
public boolean contains(T item)
{
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.data.equals(item))
{
return true;
}
current = current.next;
}
return false;
}
/*
* returns size of set
*/
public int size()
{
return size;
}
/*
* returns string representation of contents of set
*/
public String toString()
{
if (size == 0)
{
return "[ ]";
}
String s = "[";
Node current = head;
while (current.next != null)
{
s += current.data + ", ";
current = current.next;
}
return s + current.data + "]";
}
/*
* checks if item is null and throws exception if so
*/
private void checkForNull (T item)
{
if (item == null)
{
throw new IllegalArgumentException("null not a possible value!");
}
}
}
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