Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Recursive Question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help???
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
What are you doing in my house? Get 'em tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic