[Java]Write a class called SortedSet. A SortedSet object should contain a collec
ID: 3851331 • Letter: #
Question
[Java]Write a class called SortedSet. A SortedSet object should contain a collection of objects, all of the same type. This means that SortedSet should be generic (i.e., SortedSet<T>). We would like our SortedSet class not to contain any duplicate elements. In SortedSet, the data should be kept in an array, and the array should be kept in ascending order (as determined by the compareTo method). This means that the data in a SortedSet must implement the Comparable<T> interface. The objects in the set should be stored in a private instance variable which is an array.
I have provided a constructor for the class. You must write the following:
a. (1 point) An add method. It is passed an object of type T (the type variable). It returns true if the object is added to the set, or false otherwise. An item is not added to a set if an equivalent item is already in the set, or if the set is full. Equivalence is defined by the compareTo method of the T class. Remember that at all times the set must have its items in ascending order. b. (1 point) A remove method. It is passed an object of type T. It returns true if an equivalent object exists in the set, in which case it is removed. The method should return false if no equivalent item exists in the set. c. (1 point) A toString method. As always, toString should return a string which represents the object. In the case of SortedSet, we would like the string to reflect which items are in the set, and in what order. d. (1 point) A contains method. It is passed an object of type T. It should return true if an equivalent item is found in the set, or false otherwise.
Here is an example program which uses the SortedSet class:
public class UseSortedSet { public static void main(String[] args) { SortedSet<String> set = new SortedSet<String>(4); String data[] = { "a", "c", "a", "b", "z", "o" }; String otherData[] = {"f", "c", "a", "o"}; for (String s : data) { System.out.println("Add " + s + " " + set.add(s)); System.out.println(set); } for (String s : otherData) System.out.println("Does the set contain " + s + "? " + set.contains(s)); for (String s : data) { System.out.println("Remove " + s + " " + set.remove(s)); System.out.println(set); } } }
The output of this code should be:.
Add a true {a} Add c true {a,c} Add a false {a,c} Add b true {a,b,c} Add z true {a,b,c,z} Add o false {a,b,c,z} Does the set contain f? false Does the set contain c? true Does the set contain a? true Does the set contain o? false Remove a true {b,c,z} Remove c true {b,z} Remove a false {b,z} Remove b true {z} Remove z true {} Remove o false {}
Explanation / Answer
Code:
File Name: SortedSet.java
package hw2;
public class SortedSet<T extends Comparable<T>>
{
private T[] items;
private int size;
// the constructor is passed the length of the array called "items",
// which is then created.
public SortedSet(int capacity)
{
// Java arrays and type variables don't mix very well
items = (T[]) new Comparable[capacity];
size = 0;
}
// you must complete the toString method
public String toString()
{
StringBuilder ans = new StringBuilder("{");
//Loop
for(int kk=0;kk<size;kk++)
{
//Add
ans.append(items[kk]);
//Check condition
if(kk!=size-1)
//Append
ans.append(",");
}
//Append }
ans.append("}");
//Return
return ans.toString();
}
//Define method to check items is full.
public boolean isFull()
{
//Check condition
return size == items.length;
}
//Define method to add
public boolean add(T item)
{
//Check condition
if (isFull())
//Return
return false;
//Check condition
if(contains(item))
//Return
return false;
//Define variable
boolean retVal=false;
//Define
int kk=0;
//Loop
for(kk=0;kk<size;kk++)
{
//Check condition
if(items[kk].compareTo(item)>0)
//Break
break;
}
//Loop
for(int aa=items.length-2; aa>=kk;aa--)
{
//Move
items[aa+1]=items[aa];
}
//Add item
items[kk]=item;
//Increment size
size++;
//Return
return true;
}
//Define method to remove
public boolean remove(T item)
{
//Define variable
boolean retVal=false;
//Loop
for(int kk=0;kk<size;kk++)
{
//Check condition
if(items[kk]==item)
{
//Set value
retVal=true;
//Loop
for(int aa=kk;aa<size-1;aa++)
{
//Move
items[aa]=items[aa+1];
}
//Decrement size
size--;
//Break
break;
}
}
//Return
return retVal;
}
//Define method to check
public boolean contains(T item)
{
//Define variable
boolean retVal=false;
//Loop
for(int kk=0;kk<size;kk++)
{
//Check condition
if(items[kk]==item)
{
//Set value
retVal=true;
//Break
break;
}
}
//Return
return retVal; // replace this with your own code
}
}
File Name: HelloWorld.java
package hw2;
public class HelloWorld
{
public static void main(String[] args)
{
SortedSet<String> st=new SortedSet<String>(4);
System.out.println("Add a "+st.add("a"));
System.out.println(st.toString());
System.out.println("Add c "+st.add("c"));
System.out.println(st.toString());
System.out.println("Add a "+st.add("a"));
System.out.println(st.toString());
System.out.println("Add b "+st.add("b"));
System.out.println(st.toString());
System.out.println("Add z "+st.add("z"));
System.out.println(st.toString());
System.out.println("Add o "+st.add("0"));
System.out.println(st.toString());
System.out.println("Does the set contain f? "+st.contains("f"));
System.out.println("Does the set contain c? "+st.contains("c"));
System.out.println("Does the set contain a? "+st.contains("a"));
System.out.println("Does the set contain o? "+st.contains("o"));
System.out.println("Remove a "+st.remove("a"));
System.out.println(st.toString());
System.out.println("Remove c "+st.remove("c"));
System.out.println(st.toString());
System.out.println("Remove a "+st.remove("a"));
System.out.println(st.toString());
System.out.println("Remove b "+st.remove("b"));
System.out.println(st.toString());
System.out.println("Remove z "+st.remove("z"));
System.out.println(st.toString());
System.out.println("Remove o "+st.remove("a"));
System.out.println(st.toString());
}
}
Sample output:
sh-4.3$ javac HelloWorld.java
Note: ./SortedSet.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
sh-4.3$ java -Xmx128M -Xms16M HelloWorld
Add a true
{a}
Add c true
{a,c}
Add a false
{a,c}
Add b true
{a,b,c}
Add z true
{a,b,c,z}
Add o false
{a,b,c,z}
Does the set contain f? false
Does the set contain c? true
Does the set contain a? true
Does the set contain o? false
Remove a true
{b,c,z}
Remove c true
{b,z}
Remove a false
{b,z}
Remove b true
{z}
Remove z true
{}
Remove o false
{}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.