Results 1 to 4 of 4
  1. #1
    Smithy is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Problem with Nodes - need help!

    I've got a couple of issues with this darn topic, please help me and tell me what's wrong..! Probably something quite basic but still...

    1) Write a code that returns an integer chain that hold numNodes values of random numbers between 1-100.
    Java Code:
    	public static Node<Integer> createRandomChain(int numNodes)
    	{
    		Random r= new Random();
    		Node<Integer> chain= new Node<Integer>(r.nextInt(100));
    		Node<Integer> pos=chain;
    		for(int i=0;i<numNodes;i++)
    		{
    			pos.setNext(new Node<Integer> (r.nextInt(100)));
    			pos=pos.getNext();
    		}
    		return chain;
    2) Write a code that returns a character chain, but removes the sequence of identical characters, leaving only one.
    For example: k->k->k->b->b->k->z->e->e->z
    will return: k->b->k->z->e->z
    Java Code:
    public static void compressSequences(Node<Character> chain)
    	{
    		Node<Character> temp= new Node<Character>(null);
    		Node<Character> pos=chain;
    		while(pos!=null)
    		{
    			if(pos.getInfo()==pos.getNext().getInfo())
    			{
    				temp=pos.getNext();
    				pos.setNext(temp.getNext());
    			}
    			pos=pos.getNext();
    		}

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Smithy View Post
    I've got a couple of issues with this darn topic
    What are the issues? Compilation problems? Runtime exceptions? Please be detailed about those issues if you want to get help.

    kind regards,

    Jos

  3. #3
    Smithy is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Compilation problems, I assume. The code isn't working properly.
    When I try to run them I don't get the full solution.. For the random one I only get the last (or first) node and for the sequence problem I only get the first Node..

    I'm assuming something is either wrong with the main code, or with the function code..
    Java Code:
    		Node<Integer> chain= createRandomChain(30);
    		System.out.println(chain);
    		Node<Character> test= new Node<Character>('z', new Node<Character>('z', new Node<Character>('a', new Node< Character> ('a')))); 
    		compressSequences(test);
    		System.out.println(test);

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Smithy View Post
    Compilation problems, I assume. The code isn't working properly.
    Hold on for a second: when your compiler complains about something no .class file is generated and you can't run anything at all. Is your compiler whining about something?

    kind regards,

    Jos

Similar Threads

  1. Using JMusic to make Nodes
    By racewithferrari in forum New To Java
    Replies: 0
    Last Post: 02-18-2010, 03:48 AM
  2. How to dynamically add Nodes to JTree?
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 07-13-2009, 07:20 AM
  3. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 04:22 PM
  4. Nodes displayed in JTree
    By Orange in forum AWT / Swing
    Replies: 6
    Last Post: 08-08-2008, 05:07 AM
  5. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 03:09 PM

Posting Permissions

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