• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get current value of calculator  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, my calculator is supposed to have a getCurrentValue method, which is to display the current value after every calculation is performed. Does anyone have any recommendations on how that might be done? Even some links to information on how it is to be done would be helpful.



 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question isn't all that clear. You might be asking how to declare a method, or you might be asking what to put in that method, or what "current value" means, or how to display something, or how to do that after every calculation. I can guess at which of these is more likely (I think you probably know how to declare a method by now, for example) but it would help if you could expand on your problem a bit, so we can see where your confusion lies.
 
Simon Ritchie
Ranch Hand
Posts: 134
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The approach I would take is to create a global variable to contain the result of your calculations then simply create a getCurrentValue method that just prints this variable's value to the console.  EG:

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is both bad style, I am afraid, and bad design in that solution.
Start by getting rid of the //**************** and other comments which don't tell you anything which isn't obvious from looking at the code. It would be better if your method names sounded like what they did: divide rather than div, for example.
Your indentation and spacing are inconsistent. Look at lines 7‑17. You don't need all those blank lines; use blank lines between successive methods or other logical divisions of the code.
It isn't int op[] but int[] op. The [] are part of the type and shou‍ld therefore go before the identifier.
Only use the linefeed character \n if somebody has told you they want LF. Otherwise, use printf and %n.
Join all your print statements in lines 44‑ into one.
Use one Scanner object per program; don't recreate it in a loop.
Don't use System.exit(); use control flow which goes from the exit number to the end of the method and then your app will shut down gracefully. You might not notice the hazards of System.exit in a single threaded program, but it can be very dangerous in a multi‑threaded program.
There are much easier ways to verify that your options are in the correct range than lines 54‑59.

Your explanation for initialising the int in line 5 is incorrect. It is bad design to use a global variable. It is also bad design to mark everything static. Java® is supposed to be an object language and the keyword static moves you out of the realm of objects.
Why are you initialising op in line 15, not using that value, and immediately reassigning it? You can lose most of line 15.
Your methods shou‍ld do one thing; don't calculate and display the result in the same method.
I am not convinced that an array is the best way to carry your two operands around.
 
Simon Ritchie
Ranch Hand
Posts: 134
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Your explanation for initialising the int in line 5 is incorrect. It is bad design to use a global variable.


Point taken, though I think you've confused me for the original poster.  :-)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Ritchie wrote:. . . I think you've confused me for the original poster.  :-)
I have. Very sorry to have done that
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!