Question
Set Class In this project you will implement a Set class which represents a general collection of values. For this assignment, a Set is generally
Set Class
In this project you will implement a Set class which represents a general collection of values. For this
assignment, a Set is generally defined as a list of values that is always sorted and does not contain
duplicate values. More specifically, a set shall contain no pair of elements e1 and e2 such that
e1.equals(e2) and no null elements.
Requirements
To ensure consistency among all implementations there are some requirements that all implementations
must maintain.
Your implementation will reflect the definition of a Set at all times.
For simplicity, your set will be used to store Integer objects.
An ArrayList
All methods that have an Object parameter must be able to handle an input of null.
Methods such as Collections.sort that automatically sort a list may not be used.
Recommendations
There are deviations in program implementation that are acceptable and will not impact the overall
functionality of the set class.
A minimum size (initial capacity) may be specified for the ArrayList object.
When elements are added, the add() methods should ensure that elements are always in order.
Set Class Methods
There are many methods that one would expect to be supported in a set class. This section will describe
the interface of the set class. Unless specified, you will have to implement all of the described methods.
Constructors
Set()
o Description: constructs a set by allocating an ArrayList
Set(int size)
o Parameters: size - the desired size of the set.
o Description: constructs a set with an initial capacity of size.
Set(int low, int high)
o Parameters:
low an integer specifying the start value of a range of values.
high an integer specifying the end value of a range of values.
o Description: constructs a set of Integer objects containing all the inclusive values
from the range lowhigh. The default size of the Set must accommodate this mode of
construction.
Addition
boolean add(Integer i)
o Parameters: i element to be added to this Set.
o Description: if i is not in this Set, i is added to this Set.
o Returns:
true if the element is successfully added to this Set.
false if the element is not added to this Set.
int add(Integer[] a)
o Parameters: a array of elements to be added to this Set.
o Description: adds all elements of a to this set.
o Returns: the number of elements successfully added to this Set.
Removal
Integer remove(Integer i)
o Parameters: i element to be deleted from this Set.
o Description: if i is in this Set, the element is deleted.
o Returns: the Object that will be removed from this Set. If the element is not contained
in this Set, null is returned.
int remove(Set s)
o Parameters: s set of elements to be deleted from this Set.
o Description: deletes all elements of s from this Set.
o Returns: the number of elements successfully deleted from this Set.
Miscellaneous
boolean contains(Integer i)
o Parameters: i the element to be searched for in this Set.
o Description: determines whether the given element is in this Set.
o Returns: true or false whether i is in this Set.
void clear()
o Description: Removes all the elements from this Set.
boolean isEmpty()
o Description: determines whether this Set contains any elements.
o Returns: true or false whether this Set contains no elements.
int size()
o Description: determines the number of elements in this Set.
o Returns: the number of elements in this Set.
Integer get(int index)
o Parameters: index the integer index of the desired element in this Set.
o Description: returns the Object at the specified index if the index is valid.
o Returns: the Object at the specified index; null if the specified index is out of
range:(index < 0 || index >= size()).
Union
Set union(Set s)
o Parameters: s a set of Integers.
o Description: constructs and returns a new Set object that contains the objects in either
this Set or the input Set s.
o Returns: a newly constructed Set object containing the union of the Sets.
Intersection
Set intersection(Set s)
o Parameters: s a Set of Integers.
o Description: constructs and returns a new Set object that contains the objects in both
this Set and the input Set s.
o Returns: a newly constructed Set object containing the intersection of the sets.
Other Methods
boolean subset(Set s)
o Parameters: s a set of Integers.
o Description: determines if this set is a superset of s.
o Returns: true or false if all the elements of s are contained in this set.
o Notes: This method can be implemented easily using other methods described in this
assignment. null should be considered a subset of any other set.
boolean superset(Set s)
o Parameters: s a set of Integers.
o Description: determines if this set is a subset of s.
o Returns: true or false if all the elements of this set are contained in s.
o Notes: This method can be implemented easily using other methods described in this
assignment. null can only be considered a superset of the null set. However, this
cannot conceivably happen when using an instance of the Set class.
Supplied Methods
Besides the interface specification described above, there are other methods that have been provided; feel
free to use them as required in your implementation and testing. Do not modify these methods.
String toString()
o Description: provides a means of viewing the values contained within the Set object.
o Returns: a String representation of this set.
o Notes: It is important that this method remain unmodified since it will be used for
evaluation purposes. It has been provided for debugging purposes.
boolean equals(Object o)
o Parameters: s a set of values to compare against this set.
o Description: determines whether the content of o equals this set.
o Returns: TRUE or FALSE whether o is an instance of class Set and contains all
elements of this set.
o Notes: It is important that this method remain unmodified since it will be used for
evaluation purposes. It has been provided for debugging purposes.
Testing
public static void main(String args[])
o Parameters: args unused in this implementation.
o Description: this is a method that will be part of a test class SetTester.
import java.util.ArrayList;
import java.lang.ClassCastException;
/**
* For this assignment a set is generally defined as a list of values that is
* sorted and does not contain any duplicate values. More specifically, a set
* shall contain no pair of elements e1 and e2 such that e1.equals(e2), and at
* most one null element.
*/
public class Set
{
//
// TODO members
//
//
// TODO constructors
//
//
// TODO miscellaneous methods
// boolean contains(Integer i)
// void clear()
// boolean isEmpty()
// int size()
// Integer get(int index)
//
// TODO add() methods
//
//
// TODO remove() methods
//
public Set union(Set s)
{
}
public Set intersection(Set s)
{
}
public boolean subset(Set s)
{
}
public boolean superset(Set s)
{
}
// override for displaying the contents of the Set
public String toString()
{
String ret_string = "";
for (int i = 0; i < _set_array.size(); i++)
{
ret_string += _set_array.get(i).toString() + (i != _set_array.size() - 1 ? " " : "");
}
return ret_string;
}
// used to compare one Set to Another
public boolean equals(Object o) throws ClassCastException
{
if (!(o instanceof Set))
{
throw new ClassCastException();
}
Set s = (Set) (o);
for (int i = 0; i < _set_array.size(); i++)
{
if (!s.contains(_set_array.get(i)))
{
return false;
}
}
for (int i = 0; i < s.size(); i++)
{
if (!_set_array.contains(s.get(i)))
{
return false;
}
}
return true;
}
}
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