Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

recursion and retaining variable values

 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers -

I am writing an expression evaluator for arithmetic expressions. I have treated parentheticals in expressions such as: 25 * (7 + 3) + 19 as "sub-expressions" and have called methods recursively in order to evaluate them. The only way that I have been able to keep track of variable values across my recursive objects is to make the necessary variables static - but it feels as though this is sort of anti-encapsulation. I have tried using return statements lots of places, but the variable values still aren't retained. Also, I haven't found a discussion of recursion in Java in the books I have (Head First Java and Core Java, book 1). Could anyone suggest somewhere I could get some information on this?

Thanks!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generic answer is you hold all state for the current level of recursion in local variables. Show us some of what you have written. I'm pretty sure we'll be able to give you enough hints to get you going again.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan -

Thanks very much for your offer - and I'll be happy to send the code for comments and criticism. Please let me know.

But I think I might not have been very clear. The code works - meaning that I can give it a messy input expression and get the correct answer returned; it also has error messages for stuff like unbalanced parentheses. I'm doing this as a way to learn object orientation using Java as the vehicle - object orientation came in while I was raising & home-schooling my son, so I missed out when it was fresh. So when I was whining about needing something to do, because I have no confidence that I've learned something until I can actually write code, my (obnoxious) son suggested an expression evaluator. I have 3 classes and an enum in it and since I'm quite new at this it's probably pretty crude as well as being long. I asked the question because I'm all sorts of uncertain about whether I'm doing something crudely that more experienced folks can do more prettily or efficiently ...
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jinny..

I could not make a head or tail of your second message! However what ever little I understood of your problem is that you want to save some values across different recursive calls.

Stan is right that "you hold all state for the current level of recursion in local variables" and for holding data across levels, you can use static variables!

Hope I didn't get your question too wrong!!!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any global data you need to retain might also be put into an appropriate data structure (maybe a Stack) that is passed as a parameter of the recursive calls.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf -

Thank you! And since a data structure is an object, its reference is passed, so any changes made to the objects within the structure will be retained - ?

I will go look up data structures in Java!

Would this be considered better programming practice than making the needed variables static?
 
Bert Bates
author
Sheriff
Posts: 8905
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is verging on becoming an intermediate level topic...but I'll leave it here this time
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic