The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I have an assignment for my introductory Java class and it involves recursion was hoping there were some programming pepes out there that could lead me in the right direction as I was hammering away at this for 6 hours straight last night and made no progress.
Here is the description of the assignment
Write two version of a Java static method that reads a sequence of positive integers from the console until zero or a negative integer is read. The method displays the numbers in the reverse order they were entered.
The first version of the method will use recursion. There should be no for, while, or do...while loops. There should be no lists, arrays, ArrayLists, files, Strings, or any other data structure used in this method.
The second version of the method will not use recursion.
Write a main method(s) that tests both methods using expected sequences of integers. Also include a test case for each method where the user enters only a negative value.
Example execution: Enter positive integers with -1 or 0 as the last value.
3 4 5 6 7 0
7 6 5 4 3
Now I know how to get this running by using a String and creating a recursion method (similar to the common example for recursion involving palindromes). However what gets me is the inability to use data structures. I tried having the number inputted as one big integer (each time user inputs a number, I would add to the total and multiply total by ten to get places for it) and then tried to take it apart using a recursion method dealing only with integers and dividing by ten (so the decimal place gets dropped and only the integer remains). I am utterly perplexed as to how he got the example execution to display as he did without using a data structure. Also, if using a String method, inputting negative values will not make a difference as they are merely characters types. The only hint that was given to us was that the base case for the recursion should be if value == 0 || -1, do nothing.
Thanks in advance to any help that may be offered.
PSN: ohvermie <- ADD ME FOR STREET FIGHTING ACTION!
thepassenger on
0
Posts
Apothe0sisHave you ever questioned the nature of your reality?Registered Userregular
edited March 2007
Clearly, you can use primitives?
Like a single Integer?
It's a fairly simple problem, I don't really want to answer the whole problem for you, as figuring it out for yourself will teach you about recursion...but here goes.
your method will be as follows.
read the input
check the input is greater than zero
if so, call the method again
print the integer to stdout
If you know anything about traversing and reversing trees, you should recognise that where you put the recursive step changes the order of thing occuring significantly.
Like the previous poster, figuring this out for yourself is very important.
The call stack can be used as a data structure to store your data because local variables get reallocated for each method invocation. You should not need to pass any state to your recursive function in this case.
Thanks for the help. For some reason I didn't even think about putting the recursion in the actual user input section of the method. Hopefully will have better luck when I try to tackle the problem again tomorrow.
thepassenger on
PSN: ohvermie <- ADD ME FOR STREET FIGHTING ACTION!
Posts
Like a single Integer?
It's a fairly simple problem, I don't really want to answer the whole problem for you, as figuring it out for yourself will teach you about recursion...but here goes.
your method will be as follows.
read the input
check the input is greater than zero
if so, call the method again
print the integer to stdout
If you know anything about traversing and reversing trees, you should recognise that where you put the recursive step changes the order of thing occuring significantly.
The call stack can be used as a data structure to store your data because local variables get reallocated for each method invocation. You should not need to pass any state to your recursive function in this case.