Need Help on testing Code
The following is the code that I have written so far for an assignment. I am having difficulty testing my methods so far. In the main method, I try to create a SimpleSet setA = new SimpleSet() but I keep getting an error. Can anyone help me? Thank you.
Code :
import java.util.*;
public class SimpleSet<K extends Comparable<? super K>>{
/* use SimpleSortedDictionary object to hold set items
Note: Value is not needed, just set it to Boolean */
private DictionaryInterface<K, Boolean> items;
/* default constructor */
public SimpleSet() {
items = new SimpleSortedDictionary<K, Boolean>();
}
/* Adds a given object to the set. If the given object already exists in the set,
return false; otherwise, return true */
public boolean add(K item){
boolean result;
if(items.add(item, true) != null)
{
result = true;
}else result = false;
return result;
}
/* Removes a given object from the set. */
public void remove(K item){
this.items.remove(item);
}
/* Sees whether the set contains a given object. */
public boolean contains(K item){
return this.items.contains(item);
}
/* Clears all objects from the set. */
public void clear(){
items.clear();
}
/* Gets the number of objects in the set. */
public int getSize(){
return items.getSize();
}
/* Returns an iterator to the set. */
public Iterator<K> getIterator(){
return items.getKeyIterator();
}
/* may define tests here */
public static void main(String args[]) {
SimpleSet setA = new SimpleSet<K>();
int five = 5;
setA.add(five);
System.out.print(setA);
//SimpleSet setA = new SimpleSet();
//SimpleSet setA = new SimpleSortedDictionary();
//Integer five = new Integer(5);
//setA.add(five);
//setA.toString();
}
}
} // end SimpleSet
Re: Need Help on testing Code
You need to use the generic parameters to specify a type which is Comparable. Objects by default are not Comparable.
Code Java:
SimpleSet<String> stringSet = new SimpleSet<String>(); // String implements the Comparable interface
Re: Need Help on testing Code
When I try to execute this:
public static void main(String args[]) {
SimpleSet<String> setA = new SimpleSet<String>();
String one = "one";
setA.add(one);
//System.out.print("print");
}
I get this error:
printException in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor SimpleSortedDictionary.Entry(K, V, null) is undefined
Type mismatch: cannot convert from Object to V
at SimpleSortedDictionary.add(SimpleSortedDictionary. java:34)
at SimpleSortedDictionary.add(SimpleSortedDictionary. java:1)
at SimpleSet.add(SimpleSet.java:18)
at SimpleSet.main(SimpleSet.java:73)