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). 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 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:

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> { 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("{");

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!