[Java]Write a class called SortedSet. A SortedSet object should contain a collec
ID: 3851330 • 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
UseSortedSet.java
public class UseSortedSet {
public static void main(String[] args) {
SortedSet set = new SortedSet();//creating object
String data[] = { "a", "c", "a", "b", "z", "o" };//creating dataset to insert
String otherData[] = {"f", "c", "a", "o"};//dataset to search
String remData[] = {"f", "o"};//dataset to remove, can be taken from user
for (String s : data) {//loop to insert dataset
System.out.println("Add " + s + " " + set.add(s));//calling add function to add values
System.out.println(set.sets);
}
for (String s : otherData)//loop for searching dataset
System.out.println("Does the set contain " + s + "? " + set.sets.contains(s));//function call to search value
for (String s : remData) {//loop to remove element
System.out.println("Remove " + s + " " + set.sets.remove(s));//calling function to remove dataset
System.out.println(set.sets);//printing the dataset values
}
}
}
SortedSet.java
import java.util.ArrayList;
public class SortedSet {
ArrayList<String> sets;
public SortedSet() {
sets=new ArrayList<>(6);//creating arraylist
}
public boolean add(String str){
sets.add(str);//adding element to the list
return true;
}
public boolean contains(String str){
boolean flag=false;
for(int i=1;i<=sets.size();i++){
int k=str.compareTo(sets.get(i));//will return 0 if values are same
if(k==0){
flag=true;
break;
}
}
return flag;
}
public boolean remove(String str){
boolean flag=false;
for(int i=1;i<=sets.size();i++){
int k=str.compareTo(sets.get(i));//will return 0 if values are same
if(k==0){
sets.remove(str);
flag=true;
break;
}
}
return flag;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.