aspose file tools*
The moose likes Beginning Java and the fly likes Recursive Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursive Question" Watch "Recursive Question" New topic
Author

Recursive Question

Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
I have written up the following code:



When I pass printBinary(13), I want it to print "13 in base 2 is 1101." I understand why it's printing "0 in base 2 is 1101," but I can't figure out how to somehow "store" the 13 so that I can use it in my S.O.P. method. Any help is GREATLY appreciated. Thanks!

Amy
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
Can someone please help???
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You've structured this so the deepest call to self in the stack has to know the original value. One way to get it there would be to just pass it along. So instead of one argument you'd have two - the original number and the part of the number yet to be converted.

Another way would be to split this into two methods:

Does that sound helpful?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
I kind of understand - I definitely understand the part about splitting into two methods, but I'm not sure that my professor would be OK with that.

So I'm interested in the first proposition; are you saying to pass two different parameters? Because I don't think I can do that (I mean I can, but would probably get points counted off). I tried "storing" the intial value somewhere in the method, but nothing works since N is repeatedly updated thru recursion...unless I'm missing something?

Amy
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Originally posted by Amy Lee:
I kind of understand - I definitely understand the part about splitting into two methods, but I'm not sure that my professor would be OK with that.

So I'm interested in the first proposition; are you saying to pass two different parameters? Because I don't think I can do that (I mean I can, but would probably get points counted off). I tried "storing" the intial value somewhere in the method, but nothing works since N is repeatedly updated thru recursion...unless I'm missing something?

Amy


According to me, Stan's implementation is the best. I don't know why your professor would have any problem with it. After all, aren't you suppossed to seperate your display logic from business logic. The second best way is to keep passing the original value to your recursive functions and never change it. But, if you don't want that then you will have to store the original value outside of your function, like in a class level variable. I don't think your proffesor should be happy with that because you can't use your class in a multi-threaded environment, and you are adding another dependency to your function. But then, I don't understand professors anyways
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm... If you can't break this into two methods (which I think is the best approach), and you can't keep a handle on the original value by passing two arguments, then it seems to me that you need to store the original value outside of the method. And that's a problem.

The code below achieves this with a static variable (so it's available from inside the static method), BUT this will be a disaster if you ever have more than one instance of Bin working at the same time. If you can make the method and the variable both non-static, then it might work (depending on threading); but that strikes me as a poor solution, because this appears to be a "factory" method that shouldn't require an instance of the enclosing class.

Again, I think Stan's suggestion of using two methods is the best approach.

(I'm assuming that the recursive method call is required as part of the assignment, right?)
[ March 25, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive Question
 
Similar Threads
2's compliment
K&B page398
Unable to Understand the left shift operator
Question from appliedreasoning mock exam
Interger.parseInt(String str, int radix), Long.parseLong(String str, int radix)