Deque Adapter Class Errors

Can't call the wrapper class methods

Page 1 of 1

9 Replies - 2085 Views - Last Post: 16 October 2010 - 11:09 AM Rate Topic: -----

#1 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Deque Adapter Class Errors

Posted 15 October 2010 - 05:37 PM

Hi I'm trying to create a Deque Adapter Class by implementing a Deque interface, and having a NodePositionList variable which is used to add, get and remove the Nodes at either end.
My only problem is that once I write all the methods for the class from the deque class I get an EmptyListException Error which I created. But I don't know why it is being thrown?

Any one with any ideas as to why I would be getting this error?
I've included the NodePositionList Class after the DequeAdapter Class.

here is the DequeAdapter Class
public class DequeAdapter<E> implements Deque<E> {
	
	private int size;
	protected NodePositionList<E> E;

    public DequeAdapter() {
    	
    	E = new NodePositionList<E>();
 		size = 0;
    }
    
    /*Returns the number of elements in the dequeAdapter.
     **/
    public int size()throws EmptyListException{
    	
    		if(isEmpty())throw new EmptyListException("The list is empty");
    		
    		return size;
    }
    
    /*Returns whether the deque is empty.*/
    public boolean isEmpty(){
    	return (size == 0);
    }
   
    /*Inserts a Element to be the first in the deque.*/
    public void addFirst(E element){
    	
    	 E.addFirst(element);
    }
    
    /*Inserts a Element to be the last in the deque.*/
    public void addLast(E element){
    	
    	E.addLast(element);
    }
    
     /*Returns the first element, an exception is thrown if the deque is empty .*/
    public E getFirst() throws EmptyDequeException{
    	
    	if(isEmpty())throw new EmptyListException("The list is empty");
    	
    	E temp = E.first().element();
    	return	temp;
    }
    
    /*Returns the last element, an exception is thrown if the deque is empty .*/
    public E getLast() throws EmptyDequeException{
    	
    	if(isEmpty())throw new EmptyListException("The list is empty");
    	
    	return E.last().element();
    }
    
    /*Removes the first element, an exception is thrown if the deque is empty .*/
    public E removeFirst() throws EmptyDequeException{
    	return E.remove(E.first());
    }
    
    /*Removes the last element, an exception is thrown if the deque is empty .*/
    public E removeLast() throws EmptyDequeException, InvalidPositionException{
    	return E.remove(E.last());
    }
}



here is NodePositionList Class
public class NodePositionList<E> implements PositionList<E> {
	
  protected int numElts;            	// Number of elements in the list
  protected DNode<E> header, trailer;	// Special sentinels

      /** Constructor that creates an empty list; O(1) time */
      public NodePositionList() {
        numElts = 0;
        header = new DNode<E>(null, null, null);	// create header
        trailer = new DNode<E>(header, null, null);	// create trailer
        header.setNext(trailer);	// make header and trailer point to each other
      }
      
      /** Checks if position is valid for this list and converts it to
        *  DNode if it is valid; O(1) time */
      protected DNode<E> checkPosition(Position<E> p)throws InvalidPositionException {
      	
        if (p == null)throw new InvalidPositionException("Null position passed to NodeList");
        
        if (p == header)throw new InvalidPositionException("The header node is not a valid position");
        
        if (p == trailer)throw new InvalidPositionException("The trailer node is not a valid position");
        
        try {
          DNode<E> temp = (DNode<E>) p;
          
          if ((temp.getPrev() == null) || (temp.getNext() == null))
    	throw new InvalidPositionException
    	  ("Position does not belong to a valid NodeList");
          return temp;
        } catch (ClassCastException e) {
          throw new InvalidPositionException
    	("Position is of wrong type for this list");
        }
      }
      
  /** Returns the number of elements in the list;  O(1) time */
  public int size() { return numElts; }
  
  /** Returns whether the list is empty;  O(1) time  */
  public boolean isEmpty() { return (numElts == 0); }
  
  /** Returns the first position in the list; O(1) time */
  public Position<E> first()throws EmptyListException {
  	
    	if (isEmpty())throw new EmptyListException("List is empty");
    	
    return header.getNext();
  }
  
  /* Returns the last position in the list; O(1) time*/
  public Position<E> last()throws EmptyListException{
  	
  		if(isEmpty())throw new EmptyListException("List is empty");
  		
  		return trailer.getPrev();
  }
  
  /** Returns the position before the given one; O(1) time */
  public Position<E> prev(Position<E> p)throws InvalidPositionException, BoundaryViolationException {
    DNode<E> v = checkPosition(p);
    DNode<E> prev = v.getPrev();
    
    if (prev == header)throw new BoundaryViolationException
    		("Cannot advance past the beginning of the list");
    	
    return prev;
  }
  
  /*Returns the position after the given one; O(1) time*/
  public Position<E> next(Position<E> p)throws InvalidPositionException, BoundaryViolationException{
  	DNode<E> t = checkPosition(p);
  	DNode<E> next = t.getNext();
  	
  		if(next == trailer)throw new BoundaryViolationException
  				("Cannot advance past the end of the list.");
  			
  		return next;
  }
  
  /** Insert the given element before the given position, returning
    * the new position; O(1) time  */
  public void addBefore(Position<E> p, E element)throws InvalidPositionException {			
  	
    DNode<E> v = checkPosition(p);
    numElts++;
    DNode<E> newNode = new DNode<E>(v.getPrev(), v, element);
    v.getPrev().setNext(newNode);
    v.setPrev(newNode);
  }

      /** Insert the given element at the beginning of the list, returning
        * the new position; O(1) time  */
      public void addFirst(E element) {
        numElts++;
        DNode<E> newNode = new DNode<E>(header, header.getNext(), element);
        header.getNext().setPrev(newNode);
        header.setNext(newNode);
      }
      
      /*Inserts the given element at the end of the list, returning 
       *the new position; O(1)time*/
       public void addLast(E element){
       	numElts++;
       	DNode<E> newNode = new DNode<E>(trailer.getPrev(), trailer, element);
       	trailer.getPrev().setNext(newNode);
       	trailer.setPrev(newNode);
       }
       
       /*Inserts the given element after the given position*/
       public void addAfter(Position<E> p, E element)throws InvalidPositionException{
       	
       		DNode<E> t = checkPosition(p);
       		numElts++;
       		DNode<E> newNode = new DNode<E>(t, t.getNext(), element);
       		t.getNext().setPrev(newNode);
       		t.setNext(newNode);
       }
       
      /**Remove the given position from the list; O(1) time */
      public E remove(Position<E> p)throws InvalidPositionException {
        DNode<E> v = checkPosition(p);
        numElts--;
        DNode<E> vPrev = v.getPrev();
        DNode<E> vNext = v.getNext();
        vPrev.setNext(vNext);
        vNext.setPrev(vPrev);
        E vElem = v.element();
        // unlink the position from the list and make it invalid
        v.setNext(null);
        v.setPrev(null);
        return vElem;
      }
      
      /** Replace the element at the given position with the new element
        * and return the old element; O(1) time  */
      public E set(Position<E> p, E element)throws InvalidPositionException {
        DNode<E> v = checkPosition(p);
        E oldElt = v.element();
        v.setElement(element);
        return oldElt;
      }

}



Edited by macosxnerd101: Please, :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Deque Adapter Class Errors

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10342
  • View blog
  • Posts: 38,276
  • Joined: 27-December 08

Re: Deque Adapter Class Errors

Posted 15 October 2010 - 05:49 PM

Post your stack trace and error messages, along with your main() method so we can see how you are setting everything up.
Was This Post Helpful? 0
  • +
  • -

#3 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Re: Deque Adapter Class Errors

Posted 15 October 2010 - 07:10 PM

Hello again I think this is the right place I should be putting this. Well this is in reply to Marcsxnerd101, I haven't made the main method yet, I wanted to make sure that these classes worked before I started to use it. As for the stack trace I haven't looked at it. But here is the errors

C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:50: unreported exception EmptyListException; must be caught or declared to be thrown
if(isEmpty())throw new EmptyListException("The list is empty");
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:52: unreported exception EmptyListException; must be caught or declared to be thrown
E temp = E.first().element();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:52: unreported exception InvalidPositionException; must be caught or declared to be thrown
E temp = E.first().element();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:59: unreported exception EmptyListException; must be caught or declared to be thrown
if(isEmpty())throw new EmptyListException("The list is empty");
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:61: unreported exception EmptyListException; must be caught or declared to be thrown
return E.last().element();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:61: unreported exception InvalidPositionException; must be caught or declared to be thrown
return E.last().element();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:66: unreported exception EmptyListException; must be caught or declared to be thrown
return E.remove(E.first());
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:66: unreported exception InvalidPositionException; must be caught or declared to be thrown
return E.remove(E.first());
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\DequeAdapter.java:71: unreported exception EmptyListException; must be caught or declared to be thrown
return E.remove(E.last());
^
9 errors

Process completed.
Was This Post Helpful? 0
  • +
  • -

#4 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Re: Deque Adapter Class Errors

Posted 15 October 2010 - 07:13 PM

I posted a reply on the forum.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10342
  • View blog
  • Posts: 38,276
  • Joined: 27-December 08

Re: Deque Adapter Class Errors

Posted 15 October 2010 - 07:22 PM

Duplicate topics merged. Please avoid duplicate posting.

Not really good practice to use type parameters (or types in general) as names of variables. It may not confuse the JVM, but surely confusing to anyone working with your code.
protected NodePositionList<E> E; 



Basically what your errors are telling you is that if you want to throw an Exception in your method, you must declare that you want to throw it in the method header.
public void method() throws MyException{
   throw new MyException();
}



The same applies with invoking methods that throw Exceptions. You must either declare that the method in which these methods are invoked throws the Exception, or you must use a try-catch block.
public void method() throws MyException{
   myObj.meth(); //meth() throws MyException

}

//OR
public void method(){
   try{
      myObj.meth();
   }
   catch(MyException e){
     //handle the Exception
   }
}


Was This Post Helpful? 1
  • +
  • -

#6 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Re: Deque Adapter Class Errors

Posted 15 October 2010 - 07:45 PM

BooYaa Baby, thanks so much for the help mr. MarcosxNerd101. Thanks to you I realized that I was not declaring all the exceptions that the methods I was calling were going to throw, I missed a bunch of them. Thanks again!

Oh ya thanks for the style advice I was only thinking about me looking at the code.
Was This Post Helpful? 0
  • +
  • -

#7 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Re: Deque Adapter Class Errors

Posted 16 October 2010 - 08:25 AM

Hi,I posted this yesterday and got to another bump in the road. My Deque Adapter Class keeps throwing some of my exceptions when i call some methods in the main method. For example when i call my DequeAdapter's getFirst() method it always throws my EmptyDequeException when I've already added elements to my Deque. This exception should not be thrown. Any ideas? the error massages are at the bottom.

Here is my DequeAdapter Class
public class DequeAdapter<E> implements Deque<E> {
	
	private int size;
	private NodePositionList<E> E;

	/*
	 This constructor sets up the DequeAdapter class 
	 **/
    public DequeAdapter() {
    	
    	E = new NodePositionList<E>();
 		size = 0;
    }
    
    /*Returns the number of elements in the dequeAdapter.
     *@return the size of the list
     **/
    public int size(){
  		
   		return size;
    }
    
    /*Returns whether the deque is empty.
     *@return return a boolean wheather the list is empty*/
    public boolean isEmpty(){
    	return (size == 0);
    }
   
    /*Inserts a Element to be the first in the deque.*/
    public void addFirst(E element){
    	
    	 E.addFirst(element);
    	 size++;
    }
    
    /*Inserts a Element to be the last in the deque.*/
    public void addLast(E element){
    	
    	E.addLast(element);
    	size++;
    }
    
     /*Returns the first element, an exception is thrown if the deque is empty .
      *@return the first element in the list.*/
    public E getFirst() throws EmptyDequeException, InvalidPositionException, EmptyListException{
    	
    	if(isEmpty())throw new EmptyDequeException("The list is empty");
    	
    	E temp = E.first().element();
    	return	temp;
    }
    
    /*Returns the last element, an exception is thrown if the deque is empty .
     *@return the last element in the list.*/
    public E getLast() throws EmptyDequeException, InvalidPositionException, EmptyListException{
    	
    	if(isEmpty())throw new EmptyDequeException("The list is empty");
    	
    	E temp = E.last().element();
    	return temp;
    }
    
    /*Removes the first element, an exception is thrown if the deque is empty.
     *@return the first element in the list.*/
    public E removeFirst() throws EmptyDequeException, InvalidPositionException, EmptyListException{
    	
    	if(isEmpty())throw new EmptyDequeException("The list is empty!");
    	
    		size--;
    		return E.remove(E.first());
    		
    }
    
    /*Removes the last element, an exception is thrown if the deque is empty .
     *@return the last element in the list.*/
    public E removeLast() throws EmptyDequeException, InvalidPositionException, EmptyListException{
    	
    	if(isEmpty())throw new EmptyDequeException("The list is empty!");	
    	
    	size--;
    	return E.remove(E.last());
    }
}


And Here is my main method

public class Assignment02 {
    
    public static void main(String[] args) {
    	
    	int curSize;
    	Object j;
    	System.out.println("Demonstrating the DequeAdapter Class...");
    	
    	DequeAdapter myDeque = new DequeAdapter();
    	
    	myDeque.addFirst(8);
    	myDeque.addFirst("Seven");
    	System.out.println("The Deque is now " + myDeque.size());
    	myDeque.addLast(6);
    	myDeque.addLast("Five");

        //WHEN I TRY TO CALL THESE METHODS A EMPTYDEQUEEXCEPTION IS TRHOWN

    	myDeque.removeLast();
    	myDeque.getLast();
    	j = myDeque.getFirst();
    	
    
    	String v = "Yoyo";
    	myDeque.addFirst(v);
    	
    	// TODO, add your application code
    	System.out.println("Hello World!");
    }
}



errors

C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\Assignment02.java:25: unreported exception EmptyDequeException; must be caught or declared to be thrown
myDeque.removeLast();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\Assignment02.java:26: unreported exception EmptyDequeException; must be caught or declared to be thrown
myDeque.getLast();
^
C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\Assignment02.java:27: unreported exception EmptyDequeException; must be caught or declared to be thrown
j = myDeque.getFirst();
^
Note: C:\Users\Sonny\Documents\JCreator LE\MyProjects\ACS-2947-Data Structures Algorithms2\Assignment02\src\Assignment02.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors

Process completed.

This post has been edited by Slimfool101: 16 October 2010 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10342
  • View blog
  • Posts: 38,276
  • Joined: 27-December 08

Re: Deque Adapter Class Errors

Posted 16 October 2010 - 09:06 AM

Duplicate topics merged. Slimfool101, please stop opening new threads for each question you have regarding your Dequeue Adapter Class project. Please use this thread.

The three errors can be fixed in the same way as I demonstrated in my last post.

For the -Xlint warning (note that this is not an error), you declare your DequeueAdapter class as generic, but you declare without the generic type: DequeAdapter myDeque = new DequeAdapter();. What type do you want it to store? Declare it as DequeAdapter<TypeYouWantToStore> myDeque = new DequeAdapter<TypeYouWantToStore>();.
Was This Post Helpful? 0
  • +
  • -

#9 Slimfool101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-October 10

Re: Deque Adapter Class Errors

Posted 16 October 2010 - 10:14 AM

Okay ill add to this post, but if I have a new question I should post a new one?

Yes i know that i can put a try/catch but those exceptions should not be getting thrown in the first place because I already added two elements, and when I call removeFirst() I should get the first one returned, not an EmptyDequeException thrown. So I'm thinking it could be a logical error somewhere in the DequeAdapter class.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10342
  • View blog
  • Posts: 38,276
  • Joined: 27-December 08

Re: Deque Adapter Class Errors

Posted 16 October 2010 - 11:09 AM

If you have a new question or project not related to your Dequeue Adapter class project, then you are welcome to open a new thread for it.

The Exception isn't being thrown. The errors you are getting are simply telling you that they *could* be thrown, so you should handle them appropriately by defining your method header to state that it could throw the Exception using the throws clause, or by enclosing the method call in a try-catch block.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1