• Post Reply Bookmark Topic Watch Topic
  • New Topic

doing arithmetic operations when the operator are stored as variables  RSS feed

 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Lets say I want to do a simple arithmetic oiperation like 4+5. But the '+' sign is stored as a variable (the 4 and 5 are also stored as variables). How do i get java to operate on this expression?

thank you
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two choices:

1. Write some code that executes the appropriate code path based on the operator. That could be a series if if (oper.equals(...)) { ... } else if ... steps. Or it could be done using the Command Pattern, or it could be done using Java enums with each operator's enum value implementing the operation differently. Or it could be done with a switch statement on the operator. Or probably another half dozen ways I'm not thinking of. Regardless of the details, the end result is, you detect that it's a '+' as opposed to a '-' or '*' and then you choose the appropriate code to execute.

2. Use a scripting extension to Java, such as the JavaScript support that comes built-in, or the beanshell library, or Groovy.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write objects which implement an Arithmetic interface.Put them into a MapNote the difference between that Arithmetic interface, which you can probably think of a better name for, and a utility class. You need to instantiate those classes before you can put them into the Map.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think an enum would be preferable:
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how are you going to find those enum elements from the operators? Can you put "+" or "-" inside the enum elements?
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh no, I rather meant it as an alternative to your separate Addition class. You can still use a Map from Strings to Arithmetic. I just don't think you should make an instantiable type for every operation.

You could also give the BasicArithmetic enum a static method that returns the appropriate constant given an operator string.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Oh no, I rather meant it as an alternative to your separate Addition class. You can still use a Map from Strings to Arithmetic. I just don't think you should make an instantiable type for every operation.

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#301346
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall look on it as an enhancement of the Arithmetic interface. I see what you mean about not requiring an instantiable type for each operation.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, Darryl wow. I wasn't aware of that one :P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!