Question: JAVA(Fill in *replace this*) 2. a. Write a class called SortedSet. A SortedSet object should contain a collection of objects, all of the same type.
JAVA(Fill in *replace this*)
2. a. 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
I have provided a constructor for the class. You must write the following:
b. 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.
c. 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.
d. 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.
e. 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.
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 {}
package hw2;
public class SortedSet
ans.append("}"); return ans.toString(); } public boolean isFull() { return size == items.length; } public boolean add(T item) { if (isFull()) return false; return false; // replace this with your own code } public boolean remove(T item) { return false; // replace this with your own code } public boolean contains(T item) { return false; // replace this with your own code } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
