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 8 of 8

Thread: MultiSet - Enable assertions.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MultiSet - Enable assertions.

    I have a class for my MultiSet<E>, and an associated test-program.

    My class is a follows:

    public class MultiSet<E> extends AbstractCollection<E>
    {
    	private int size = 0;
    	private Map<E, Integer> values = new HashMap<E, Integer>();
     
    	public MultiSet()
    	{
     
    	}
     
    	public MultiSet(Collection<E> c)
    	{
    		addAll(c);
    	}
     
    	@Override
    	public boolean add(E e)
    	{
    		Integer i = values.get(e);
    		if (i == null)
    			values.put(e, 1);
    		else
    			values.put(e, i + 1);
    		return true;
    	}
     
    	@Override
    	public boolean remove(Object o)
    	{
    		Integer i = values.remove(o);
    		return i != null;
    	}
     
    	public Iterator<E> iterator()
    	{
    		return new Iterator<E>()
    		{
    			private Iterator<E> iterator = values.keySet().iterator();
    			private int remaining = 0;
    			private E current = null;
     
    			public boolean hasNext()
    			{
    				return remaining > 0 || iterator.hasNext();
    			}
     
    			public E next()
    			{
    				if (remaining == 0)
    				{
    					current = iterator.next();
    					remaining = values.get(current);
    				}
    				remaining--;
    				return current;
    			}
    			public void remove()
    			{
    				throw new UnsupportedOperationException();
    			}
    		};
    	}
    		@Override
    		public boolean equals(Object object)
    		{
     
    			if (this == object) return true;
    			if (object == null) return false;
    			if (this.getClass() != object.getClass()) return false;
    			MultiSet<?> o = (MultiSet<?>) object;
    			return o.values.equals(values);
    		}
     
    		@Override
    		public int hashCode()
    		{
    			return values.hashCode()*163 + new Integer(size).hashCode()*389;
    		}
     
    		@Override
    		public String toString()
    		{
    			String res = "";
    			for (E e : values.keySet())
    			{
    					res = res + "[" + e.toString() + ":" + values.get(e) + "]";
    			}
    			return getClass().getName() + res;
    		}
     
    		public int size()
    		{
    			return values.size();
    		}
    	}

    And the test program:

    public class MultiSetTest {
     
        public static void main(String[] args) {
    	MultiSet<String> a = new MultiSet<String>();
    	MultiSet<String> b = new MultiSet<String>();
     
    	a.add("Foo");
    	a.add("Bar");
    	a.add("Foo");
    	System.out.println("a:" + a); // test toString
     
    	b.add("Bar");
    	b.add("Foo");
    	b.add("Bar");
    	b.add("Foo");
    	System.out.println("b:" + b);
     
    	assert !a.equals(b) : "Failed test 1!"; // test equals
    	assert b.remove("Bar") : "Failed test 2!"; // test remove
    	assert a.equals(b) : "Failed test 3!";
    	for(String s : a) { // test iterator
    	    assert b.remove(s) : "Failed test 4!";
    	}
    	assert b.size() == 0 : "Failed test 5!";
     
    	Set<String> baseSet = new HashSet<String>(a);
    	assert baseSet.size()==2 : "Failed test 6!";  
     
    	b = new MultiSet<String>(a);
    	assert a.equals(b) : "Failed test 7!";
     
    	try {
    	    assert false;
    	    System.out.println("Please enable assertions!");
    	}
    	catch(AssertionError e) {
    	    System.out.println("Success!");
    	}
        }
    }

    To me it seems to be working, but if my MultiSet-class was working proberly, then the output should be: "Success!"

    Instead i get: "Please enable assertions!".

    Where/how do i enable assertions?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: MultiSet - Enable assertions.

    Google is your friend, learn to embrace it as you will get results far quicker than posting here.
    Use the forums when Google doesn't return anything relevant .

    Back on topic: To enable assertions all you need to do is add -ea to the command line arguments / program arguments before running your program.
    If you're running from the command line, usage is as such: java -ea TestHarness

    Or if you're using an IDE, such as eclipse, you will find the textfields for adding -ea in the area of run configurations.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MultiSet - Enable assertions.

    args.jpg

    Is that correct?
    I wrote "-ea" at Program arguments for my Test-class,
    and when I run the program I still get the same fail-output :/

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: MultiSet - Enable assertions.

    Apologies, should be under VM arguments in the IDE
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MultiSet - Enable assertions.

    Oh wait.
    It has to be VM arguments, ofcourse.

    Now; "Failed test 3!" xD
    So i guess there's a problem with my equals then :/

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MultiSet - Enable assertions.

    Do any of you see what the problem is?

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: MultiSet - Enable assertions.

    well A is not equal to B, so it will fail
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MultiSet - Enable assertions.

    I found the problem.
    I modified my Add and remove-methods. No problems with the equals-method
    It works - the output is: Success.

    Success!!!
    And thank you for your help!

Similar Threads

  1. how to enable tombol
    By ndoe in forum AWT / Java Swing
    Replies: 0
    Last Post: August 13th, 2009, 08:21 AM

Tags for this Thread