Results 1 to 12 of 12

Thread: AssertionError?

  1. #1
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default AssertionError?

    So i feel obliged to inform that i am working on an assignment for school but i have spent a few hours on this and i can't see whats causing the problem.
    Anyways, upon running my code i am greeted with a functioning program for 6 out of 16 iterations of the test code's for loop before it still comes up with
    Java Code:
    'Exception in thread "main" java.lang.AssertionError'
    .

    If some helpful soul would waste some time and look over my code for the obvious mistake i have made, that would be great. Until then i shall continue to pour over my code in hopes of finding this error.

    Code here


    Thanks,

    Draymire


    EDIT: Program is supposed to randomly delete elements from the Queue efficiently.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: AssertionError?

    Please post the code that causes/shows the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    The manage attachements button isn't working for me so i posted it on pastebin.com. It is linked in the original post (unless i failed at that too) but just in case here is the

    url: [Java] Draymire Java Help Code - Pastebin.com

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: AssertionError?

    Please post the code here in the forum that shows your problem. Be sure to wrap the code in code tags.
    See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    Ahhh ok. Well the chunks that are giving me errors are:

    Java Code:
    RandomQueue<Integer> q = new RandomQueue<Integer>();
    		
    		// some simple tests and output to check for randomness
    		int n = 16;
    		for (int j = 0; j < 5; j++) {
    			for (int i = 0; i < n; i++) {
    				q.add(i);
    			}
    			boolean[] checked = new boolean[n];
    			for (int i = 0; i < n; i++) {
    				myassert(q.size() == n-i);
    				Integer k = q.remove();//this would most likely be the offending line
    				myassert(checked[k] == false);//due to this being the error producing line
    				checked[k] = true;
    				System.out.print(k);
    				if (q.isEmpty()) {
    					System.out.println();
    				} else {
    					System.out.print(",");
    				}
    			}
    			myassert(q.isEmpty());
    		}
    which points to:

    Java Code:
    public T remove1(int z) throws NoSuchElementException{//remove a random object from the list
    		if (m==0) throw new NoSuchElementException();
    		T x = l.get(z);
    		if (z<=l.size()){ //shift right
    			for(int i=z; i>0; i--){
    				l.set(i, l.get(i-1));
    			}
    			j++;
    			m--;
    		} else { //shift left
    			for(int i=z; i<l.size();i++){
    				l.set(i, l.get(i+1));
    			}
    			m--;
    		}
    
    		return x;
    	}
    
    	/**
    	 * Return at the next element that will be returned by poll()/remove()
    	 * without actually removing it
    	 */
    	public T peek() {
    		if (l.size() == 0)
    			return null;
    		assert(next >= 0 && next <= l.size()-1);
    		return l.get(next);
    	}
    
    	/**
    	 * Remove and return a random element from the queue
    	 */
    	public T poll() {
    		peek();
    		T x = remove1(next);
    		next = (l.size() > 0) ? r.nextInt(l.size()) : -1;
    		return x;
    	}
    
            public int size() {
    		return m;
    	}

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: AssertionError?

    Do you have the full exception (including stack trace)?
    If not then you need to change how exceptions are logged so you get that information.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    Java Code:
    Exception in thread "main" java.lang.AssertionError
        at comp2402a2.RandomQueue.myassert<RandomQueue.java:137>
        at comp2402a2.RandomQueue.main<RandomQueue.java:158>
    line 158 corresponds to line 13 of the first chunk of code i posted and line 137 corresponds to line 3 of the following chunk.

    Java Code:
    protected static void myassert(boolean b) throws AssertionError {
    		if (!b) {
    			throw new AssertionError();
    		}
    	}

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: AssertionError?

    Is your question about the value of checked[k]?
    Its value controls whether the Exception is thrown.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    Yeah, i can't figure out why it isn't changing properly. It works for a random amount of times (never more then 11 so far) before it fails miserable. After a bunch of debugging work i am thinking the issue is because the value k gets called twice sometimes and thus has been changed to true. Currently going to try and rework that so it guarantees a different value of k there.

    I'll post back if i figure it out.

    Thanks.

    EDIT:
    Never mind, i got it to function and i apparently butchered the efficiency of the original program instead of optimizing it.

    on a side note any ideas on how to go about making the following code to run faster. (since it is a school assignment just ideas on where to go would be great)

    Java Code:
    /**
    	 * Return at the next element that will be returned by poll()/remove()
    	 * without actually removing it
    	 */
    	public T peek() {
    		if (l.size() == 0)
    			return null;
    		assert(next >= 0 && next <= l.size()-1);
    		return l.get(next);
    	}
    
    	/**
    	 * Remove and return a random element from the queue
    	 */
    	public T poll() {
    		if (l.size() == 0)
    			return null;
    		assert(next >= 0 && next <= l.size()-1);
    		T x = l.remove(next);
    		next = (l.size() > 0) ? r.nextInt(l.size()) : -1;
    		return x;
    	}
    Last edited by Draymire; 10-04-2012 at 05:30 PM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: AssertionError?

    There are no loops, why do you think it needs to run faster?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    The assignment is to make poll() run at theta(size). The best i have done was eliminate the duplicate code which made it faster by .002 seconds.
    It's a Data Structures course, if that sheds any light.

    EDIT: the speeding up i guess would be the removal process of non-end point of ArrayList. Tried to make it into a ring buffer but ran into that AssertionError issue.
    Last edited by Draymire; 10-04-2012 at 05:47 PM.

  12. #12
    Draymire is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: AssertionError?

    Quite sure i have figured out my issue.

    I replace
    Java Code:
    T x = l.remove(next);
    with

    Java Code:
    T x = l.replace();
    where l.replace() is
    Java Code:
    Public T replace() {
        T x = l.set(next, l.get(l.size()-1));
        l.remove(l.size()-1);
        return x;
    }
    Thanks for being there to bounce my thoughts off of.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •