• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked List question  RSS feed

 
Liam Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment ask for:

Declare a variable at the beginning of the main method for a linked list that stores stings - completed

After the code that calculates, formats, and displays the result for each calculation, add code that formats a string with the results of the calculation and then stores the string in the linked list. - I completed the 2nd part (store string in the linked list) but I am at loss for first part. What does it mean by "add code that formats a string with the results of the calculation"?

Add code to display the elements in the linked list at the console when the user indicates that the program should end. This code should retrieve the elements of the linked list in reverse order. To do that, you'll need to use methods of the LinkedList class. Then, test the program by making at least 3 future value calculations. - completed

I am almost done with this assignment and only thing threw me off is 2nd question. What does it really mean?

Here's my code:

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try drawing a diagram of your linked list, with all the nodes and their values. That will remind you how the app works. Write down how to display each value, starting from the “last”. If you get it in English, and it looks really nice and simple, you can quite easily translate it to code, in a new method. You can call that method as the last statement in your application.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liam Smith wrote:After the code that calculates, formats, and displays the result for each calculation, add code that formats a string with the results of the calculation and then stores the string in the linked list. - I completed the 2nd part (store string in the linked list) but I am at loss for first part. What does it mean by "add code that formats a string with the results of the calculation"?

You need to create a String containing the result formatted in some way. You've already done this once (lines 40 - 48) so unless you've been told to use a different format for the final display I would just store the results String in your list. I would ask for clarification from your teacher about what format (s)he wants.

Liam Smith wrote:Add code to display the elements in the linked list at the console when the user indicates that the program should end. This code should retrieve the elements of the linked list in reverse order. To do that, you'll need to use methods of the LinkedList class. Then, test the program by making at least 3 future value calculations. - completed

No it's not. Your code doesn't do what the specification says.
 
Liam Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote: No it's not. Your code doesn't do what the specification says.


Can you clarify on this? When I run the program, input the at least 3 values and end the program. Linked list does return in reverse order which assignment asked for?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it doesn't since it won't compile as written above:

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liam Smith wrote:
Stuart A. Burkett wrote: No it's not. Your code doesn't do what the specification says.


Can you clarify on this? When I run the program, input the at least 3 values and end the program. Linked list does return in reverse order which assignment asked for?

Assuming that it's lines 64 - 69 that supposedly do that, the code is inside the while loop so will print out every time round the loop, not just after the user has pressed something other than 'y'.

There's also a better method than listIterator to get the list in descending order. The highlighted word is to give you a clue.
 
Liam Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I would say it doesn't since it won't compile as written above:



That's odd, it compile fine on mine:

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I would say it doesn't since it won't compile as written above:


That's a new feature of Java 7. The compiler can work out the type of the list from the type specified in the reference declaration.
 
Liam Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Liam Smith wrote:
Stuart A. Burkett wrote: No it's not. Your code doesn't do what the specification says.


Can you clarify on this? When I run the program, input the at least 3 values and end the program. Linked list does return in reverse order which assignment asked for?

Assuming that it's lines 64 - 69 that supposedly do that, the code is inside the while loop so will print out every time round the loop, not just after the user has pressed something other than 'y'.

There's also a better method than listIterator to get the list in descending order. The highlighted word is to give you a clue.


Thank you for pointing out that I have put code inside while. I have recode, hopefully I got right this time.

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liam Smith wrote:I have recode, hopefully I got right this time.


Why are you sorting the list ? The specification says only to print them in reverse order. Your original code would probably have worked. Before I was just pointing out that there is another method in the LinkedList class that makes it clearer what your code is doing. If you were happy that your original code was printing the values correctly then it would have been fine to use that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!