Recursive Question

Amy Lee
Greenhorn
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
Posts: 25

Stan James
(instanceof Sidekick)
Ranch Hand
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:

Amy Lee
Greenhorn
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
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
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 ]