Results 1 to 5 of 5
  1. #1
    khorn06 is offline Member
    Join Date
    May 2014
    Location
    Massachusetts
    Posts
    7
    Rep Power
    0

    Default Why can I not get my append method to return itself?

    This is my class code below and the Junit test that is being run.

    My problem is that when I try to make an append to my list I have to return the actual list but I cant figure out where I am going wrong.

    Java Code:
    package structures;
    
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    public class ListImplementation<T> implements ListInterface<T> {
    
    	private Node<T> listHead, listTail;
    	
    	private int size;
    	
    	public ListImplementation() {
    		listHead = null;
    		listTail = null;
    		size = 0;
    	}
    	
    	public ListImplementation(Node<T> listHead) {
    		this.listHead = listHead;
    	}
    	
    	@Override
    	public int size() {
    		return size;
    	}
    
    	@Override
    	public boolean isEmpty() {
    		return listHead == null;
    	}
    
    	@Override
    	public T get(int n) throws NoSuchElementException {
    		if(n > size) {
    			throw new NoSuchElementException();
    		} else {
    			Node<T> head_ = listHead;
    			for(int i = 0; i < n; i++) {
    				head_ = head_.getNext();
    			}
    			
    			return head_.getData();
    		}
    	}
    
    	@Override
    	public ListInterface<T> append(T elem) {
            Node<T> list_ = new Node<T>(elem, null); 
            
            System.out.println("append: "+elem);
            if(this.isEmpty()) {
                listHead = list_;
                listTail = listHead;
            } else {
                listTail.setNext(list_);
                listTail = list_;
            }
    
            size++;
    		
            System.out.println("size: "+size);
    		return new ListImplementation<T>(listHead);
    	}
    
    	@Override
    	public Iterator<T> iterator() {
    			return new ListIterator<T>(listHead, size);
    	}
    	
    }
    Java Code:
    	@Test(timeout = 500)
    	public void testList2() {
    		assertTrue("Newly constructed list should be empty", impl.isEmpty());
    		assertEquals("Newly constructed list should have size 0", 0,
    				impl.size());
    		assertSame("Appending the list should return itself.", impl,
    				impl.append(5));
    
    	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can I not get my append method to return itself?

    How are we supposed to know? You haven't even declared impl anywhere. Please submit an Short, Self Contained, Correct Example that demonstrates the problem. Keep it less than 100 readable lines.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    khorn06 is offline Member
    Join Date
    May 2014
    Location
    Massachusetts
    Posts
    7
    Rep Power
    0

    Default Re: Why can I not get my append method to return itself?

    Sorry for the confusion, but impl is implemented in the JUnit test file

    private ListInterface<Integer> impl = new ListImplementation<Integer>();

    I also asked one of my friends about my dilemma and the answer was to "return this;"

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can I not get my append method to return itself?

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why can I not get my append method to return itself?

    From the end of your append method:
    Java Code:
    return new ListImplementation<T>(listHead);
    You are returning a new ListImplementation, not the one the append was called on.

    Look up the 'this' keyword.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 03:15 PM
  2. Cannot return Method's value
    By darshanaprasad in forum New To Java
    Replies: 2
    Last Post: 09-08-2012, 09:25 AM
  3. What can a method return?
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 01-29-2012, 03:22 PM
  4. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 08:37 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Tags for this Thread

Posting Permissions

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