Results 1 to 5 of 5
Thread: Linked node assignment help
- 05-04-2012, 10:48 PM #1
Member
- Join Date
- May 2012
- Posts
- 14
- Rep Power
- 0
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; } }
- 05-04-2012, 11:05 PM #2
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!
- 05-04-2012, 11:15 PM #3
Member
- Join Date
- May 2012
- Posts
- 14
- Rep Power
- 0
Re: Linked node assignment help
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-04-2012 at 11:27 PM.
- 05-05-2012, 08:59 PM #4
Member
- Join Date
- May 2012
- Posts
- 14
- Rep Power
- 0
Re: Linked node assignment help
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 09:14 PM.
- 05-05-2012, 11:54 PM #5
Member
- Join Date
- May 2012
- Posts
- 14
- Rep Power
- 0
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
-
mapping one XML node to another XML node in GUI.
By ashu_i20 in forum XMLReplies: 1Last Post: 04-16-2012, 02:29 PM -
Adding tail node to a doubly linked list
By Klusps in forum New To JavaReplies: 4Last Post: 09-21-2011, 10:48 PM -
Creating a node for polynomial linked list
By Java-Guy in forum New To JavaReplies: 8Last Post: 03-17-2011, 08:27 PM -
Linked List node class help!!
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 05-03-2010, 01:21 AM -
How to link a Array elemant to a Linked list Node
By ravinda in forum New To JavaReplies: 2Last Post: 04-18-2009, 09:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks