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

Thread: can't figure whats wrong with my add method?? help!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default can't figure whats wrong with my add method?? help!

    I have a interface called "ListInterface", a class called "LList" and a main "LListTest"

    my an add method that is not running.

    public class LList<T> implements ListInterface<T> {
     
    	private Node firstNode;	// reference to first node
    	private int length;		// number of entries in list
     
    	public LList(){
    		clear();
    	}
     
    	public final void clear(){
    		firstNode = null;
    		length = 0;
    	}
     
    	@Override
    	public boolean add(T newEntry) {
    		Node newNode = new Node(newEntry);
    		if (isEmpty()){
    			firstNode = newNode;
    		}else{
    			Node lastNode = getNodeAt(length);
    			lastNode.next = newNode;	// make last node reference new node
    		}
    		length++;
    		return true;
    	}
     
    	@Override
    	public boolean add(int newPosition, T newEntry) {
    		boolean isSuccessful = true;
    		if (1 <= newPosition && newPosition <= length+1){
    			Node newNode = new Node(newEntry);
    			if (isEmpty() || newPosition == 1){	// case 1
    				newNode.next = firstNode;
    				firstNode = newNode;
    			}else{								// case 2
    				Node nodeBefore = getNodeAt(newPosition-1);
    				Node nodeAfter = nodeBefore.next;
    				newNode.next = nodeAfter;
    				nodeBefore.next = newNode;
    			}
    			length++;
    		}else{
    			isSuccessful = false;
    		}
    		return isSuccessful;
    	}
     
    	@Override
    	public void display() {
    		Node currentNode = firstNode;
    		while(currentNode != null){
    			System.out.println(currentNode.data);
    			currentNode = currentNode.next;
    		}
    	}
     
    	@Override
    	public boolean isEmpty() {
    		boolean result;
    		if (length == 0){
    			assert firstNode == null : "length = 0 but firstNode is not null";
    			result = true;
    		}else{
    			assert firstNode != null : "length not 0 but firstNode is null";
    			result = false;
    		}
    		return result;
    	}
    	/**	Task: Returns a reference to the node at a given position.
    	 * 	Precondition: List is not empty; 1 <= givenPosition <= length.
    	 * @param givenPosition
    	 * @return
    	 */
    	private Node getNodeAt(int givenPosition){
    		assert (!isEmpty() && 1 <= givenPosition && givenPosition <= length);
    		Node currentNode = firstNode;
    		for (int counter = 1; counter < givenPosition; counter++){
    			currentNode = currentNode.next;
    		}
    		assert currentNode != null;
    		return currentNode;
    	}
     
    	private class Node //private inner class
    	{	
    		private T data;
    		private Node next;
     
    		private Node(T dataPortion){
    			data = dataPortion;
    			next = null;
    		}
     
    		private Node(T dataPortion, Node nextNode){
    			data = dataPortion;
    			next = nextNode;
    		}
    	}// end Node
     
    }
    Last edited by b094mph; October 8th, 2011 at 05:11 PM.


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

    Default Re: can't figure whats wrong with my add method?? help!

    here is the interface "ListInterface" (no issues with this part of the code) I am giving it just in case you would like to run the program yourself.

    /** An interface for the ADT list.
     * Entries in the list have positions that begin with 1.
     */
    public interface ListInterface<T> 
    {
    	/** Task: Adds a new entry to the end of the list.
    	 * 		  Entries currently in the list are unaffected.
    	 * 		  The list's size is increased by 1.
    	 * 	@param newEntry	the object to be added as a new entry
    	 * 	@return true if the addition is successful, or false if the list is full */
    	public boolean add(T newEntry);
     
    	/** Task:	Adds a new entry at a specified position within the list.
    	 * 			Entries originally at and above the specified position are at the 
    	 * 			next higher position within the list.
    	 * 			The list's size is increased by 1.
    	 *  @param 	newPosition		an integer that specifies the desired position of the new entry
    	 *  @param	newEntry		the object to be added as a new entry
    	 *  @return	true if the addition is successful, or false if either the list is full, newPosition < 1, 
    	 *  		or newPosition > getLength()+1	*/
    	public boolean add(int newPosition, T newEntry);
     
     
    	/** Task: Remmoves all entries from the list. */
    	public void clear();
     
    	/**	Task: Sees whether the list is empty
    	 * @return	true if the list is empty, false if the list is not empty */
    	public boolean isEmpty();
     
    	/**	Task: 	Displays all entries that are in the list, one per line, in the order in which they 
    	 * 			occured in the list */
    	public void display();
     
    }
    Last edited by b094mph; October 14th, 2011 at 07:42 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: can't figure whats wrong with my add method?? help!

    and here is my Main: the line of code: list.add("bobby");

    is giving me an issue.

     
    public class LListTest {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		LList<String> list = new LList<String>();
     
     
    		list.add("bobby");
    		list.add("sue");
    		list.add("john");
    		list.add(2,"joey");
    		list.add("andres");
    		list.display();
    	}
     
    }
    Last edited by b094mph; October 8th, 2011 at 05:09 PM.

  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: can't figure whats wrong with my add method?? help!

    Yeah... I'd recommend surrounding relevant code in code tags, (as found in my signature) and removing a lot of that unnecessary code before an angry mod stamps all over you

    Could you please just a short code snip-it where you believe a problem is occurring, say what the problem is, how it's behaving and how it's supposed to be behaving and you will greatly increase your chances of being helped.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: can't figure whats wrong with my add method?? help!

    I am basically trying to use my add method to add nodes together. ( similiar to a linked list) but i am not able to add my first node.. i get this error when i run the code. i am not very good at determining what these errors mean...

    Exception in thread "main" java.lang.ClassFormatError: Duplicate method name&signature in class file LList$Node
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at LList.add(LList.java:19)
    at LListTest.main(LListTest.java:11)

    this line of code is currently giving me the error: Node newNode = new Node(newEntry);

    public boolean add(T newEntry) {
    Node newNode = new Node(newEntry);
    if (isEmpty()){
    firstNode = newNode;
    }else{
    Node lastNode = getNodeAt(length);
    lastNode.next = newNode;	// make last node reference new node
    }
    length++;
    return true;
    }

Similar Threads

  1. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  2. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  3. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  4. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM