Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need Help on testing Code

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    	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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    SimpleSet<String> stringSet = new SimpleSet<String>(); // String implements the Comparable interface

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)
    Last edited by indidude89; May 9th, 2011 at 01:00 PM.

Similar Threads

  1. testing Java jsp/servlets
    By cgodfrey in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: March 30th, 2011, 03:44 PM
  2. SMS Encryption Testing
    By ants_dj07 in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: March 27th, 2011, 11:14 AM
  3. Testing if dates are real or not.
    By cardsfan33 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 07:46 PM
  4. Testing equation of a line
    By TimoElPrimo in forum Object Oriented Programming
    Replies: 8
    Last Post: February 23rd, 2011, 12:40 AM
  5. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM