• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

doing arithmetic operations when the operator are stored as variables

 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think an enum would be preferable:
 
Campbell Ritchie
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha, Darryl wow. I wasn't aware of that one :P
 
No holds barred. And no bars holed. Except this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic