Results 1 to 5 of 5
  1. #1
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Linked node assignment help

    So we have to write a class that basically runs this little game "Affection." Well we have to used the instructor provided class AffectionNode for it and am having trouble implementing the constructor right off the bat because I don't know exactly how I can do that with linked nodes. The Affection node class gives us .next(next node in the list), .name(returns this persons name), and .kisser(returns name of who kiss this person). I should say that we aren't allowed to use arrayLists, arrays, or any other collections like that. If someone can point me in the right direction I would really appreciate it. Here is my code, went through a few different ways on the constructor but I just couldn't get it working:

    Java Code:
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = names.size(); i > 0; i++){
    				AffectionNode newNode = new AffectionNode(names.get(i));
    				
    				new AffectionNode(names.get(i - 1), newNode);
    
    			}
    	
    			
    	}
    	
    	public void printKissRing(){
    		
    	
    	
    	}
    	
    	public void printSchoolyard(){
    		
    	}
    	
    	public boolean killRingContains(String name){
    		return false;
    	}
    	
    	public boolean schoolyardContains(String name){
    		return false;
    	}
    	
    	public boolean isGameOver(){
    		return false;
    	}
    	
    	public String winnder(){
    		return null;
    	}
    	
    	public void kill(String name){
    		
    	}
    	
    	public String toString(){
    		System.out.println("[");
    		String s = " ";
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			s = s + current.name;
    			current = current.next;
    		}
    		return s;
    	}
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Linked node assignment help

    When you say you couldn't get it working, what exactly is the problem? Compiler error? Weird behavior? Have you stepped through this with a debugger, or at the least added some print statements to figure out what's going on? Have you gone over the basic tutorials on the things you're having trouble with? For example, if you're having trouble with the syntax for a constructor, googling "java constructor tutorial" is a great place to start.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Linked node assignment help

    Quote Originally Posted by KevinWorkman View Post
    When you say you couldn't get it working, what exactly is the problem? Compiler error? Weird behavior? Have you stepped through this with a debugger, or at the least added some print statements to figure out what's going on? Have you gone over the basic tutorials on the things you're having trouble with? For example, if you're having trouble with the syntax for a constructor, googling "java constructor tutorial" is a great place to start.
    Yeah I wrote a small test class using the toString() method I wrote and added names to an ArrayList and passed in the ArrayList but it only printed the first name I added to console. I have tried to look up a lot about linked nodes but they are fairly rare it seems, because the easier way of using the java.util.ArrayList.

    Edit: This is the code snippet for the constructor that gives me the first name only then breaks or something.

    Java Code:
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = 1; i < names.size(); i++){
    				String currentName = names.get(i);
    				AffectionNode next = current;
    				AffectionNode newNode = new AffectionNode(currentName, next);
    				next = next.next;
    				
    			}
    	
    			
    	}
    Last edited by Schooling; 05-05-2012 at 12:27 AM.

  4. #4
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Linked node assignment help

    Quote Originally Posted by Schooling View Post
    Yeah I wrote a small test class using the toString() method I wrote and added names to an ArrayList and passed in the ArrayList but it only printed the first name I added to console. I have tried to look up a lot about linked nodes but they are fairly rare it seems, because the easier way of using the java.util.ArrayList.

    Edit: This is the code snippet for the constructor that gives me the first name only then breaks or something.

    Java Code:
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = 1; i < names.size(); i++){
    				String currentName = names.get(i);
    				AffectionNode next = current;
    				AffectionNode newNode = new AffectionNode(currentName, next);
    				next = next.next;
    				
    			}
    	
    			
    	}
    Anybody got any ideas? Still struggling to get this working lol.

    Edit: and here is updated code:
    Java Code:
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = 1; i < names.size(); i++){
    				String currentName = names.get(i);
    				AffectionNode next = current;
    				AffectionNode newNode = new AffectionNode(currentName, next);
    				next = next.next;
    				
    			}
    	
    			
    	}
    	
    	public void printKissRing(){
    		String tempName = frontOfRing.name;
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			String name = current.name;
    			System.out.println(name + " is so into " + current.next.name);
    			current = current.next;
    		}
    	
    	
    	}
    	
    	public void printSchoolyard(){
    		
    	}
    	
    	public boolean kissRingContains(String name){
    		AffectionNode temp = frontOfRing;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;                  
    		}
    		return false;
    	}
    	
    	public boolean schoolyardContains(String name){
    		AffectionNode temp = frontOfYard;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;
    		}
    		return false;
    	}
    	
    	public boolean isGameOver(){
    		return false;
    	}
    	
    	public String winner(){
    		return null;
    	}
    	
    	public void kiss(String name){
    		
    	}
    	
    	public String toString(){
    		System.out.println("[");
    		String s = " ";
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			s = s + current.name;
    			current = current.next;
    		}
    		return s;
    	}
    
    }
    Last edited by Schooling; 05-05-2012 at 10:14 PM.

  5. #5
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Linked node assignment help

    Okay, got the constructor and other methods working properly I believe now am working on the "kiss" method. For it I have to take the name being kissed from the KissRing and remove it from there and add it to the "SchoolYard". I just am not quite sure how to go about re-referencing the nodes to get them in the right order. Here is my code at the moment:

    Java Code:
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = 1; i < names.size(); i++){
    				String currentName = names.get(i);
    
    				current.next = new AffectionNode(currentName);
    				current = current.next;
    				
    			}
    	
    			
    	}
    	
    	public void printKissRing(){
    		String tempName = frontOfRing.name;
    		AffectionNode current = frontOfRing;
    		while(current.next != null){
    			String name = current.name;
    			String nextName = current.next.name;
    			System.out.println("  " + name + " is so into " + nextName);
    			current = current.next;
    		}
    		System.out.println("  " + current.name +" is so into " + tempName);
    	
    	
    	}
    	
    	public void printSchoolyard(){
    		String tempName = frontOfYard.name;
    		AffectionNode current = frontOfYard;
    		while(current.next != null){
    			String name = current.name;
    			String nextName = current.next.name;
    			System.out.println("  " + name + " was kissed by " + nextName);
    			current = current.next;
    		}
    		System.out.println(current.name +" was kissed by " + tempName);
    	
    	
    	}
    	
    	
    	public boolean kissRingContains(String name){
    		AffectionNode temp = frontOfRing;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;                  
    		}
    		return false;
    	}
    	
    	public boolean schoolyardContains(String name){
    		AffectionNode temp = frontOfYard;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;
    		}
    		return false;
    	}
    	
    	public boolean isGameOver(){
    		if(frontOfRing.next == null){
    			return true;
    		}
    		return false;
    	}
    	
    	public String winner(){
    		if(frontOfRing.next == null){
    			return frontOfRing.name;
    		}
    		return null;
    	}
    	
    	public void kiss(String name){
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			String currentName = current.name;
    			if(name == currentName){
    				frontOfYard
    				current.next = current.next.next;
    				
    			}
    			current = current.next;
    		}
    	}
    	
    	public String toString(){
    		System.out.println("[");
    		String s = " ";
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			s = s + current.name;
    			current = current.next;
    		}
    		return s;
    	}
    
    }

Similar Threads

  1. Replies: 1
    Last Post: 04-16-2012, 03:29 PM
  2. Adding tail node to a doubly linked list
    By Klusps in forum New To Java
    Replies: 4
    Last Post: 09-21-2011, 11:48 PM
  3. Creating a node for polynomial linked list
    By Java-Guy in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 09:27 PM
  4. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 02:21 AM
  5. How to link a Array elemant to a Linked list Node
    By ravinda in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 10:16 AM

Posting Permissions

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