• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make the evaluation of an If expression dynamic?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Suppose I have three boolean variables a,b and c that can be used along with the relational and boolean operators to make an expression that has to be evaluated by an if statement.

Now, Suppose The Operators involved are selected by means of radio buttons from the front end.

For eg : a OP1 b OP2 c

Values of OP1 and OP2 are got from a set of radio button selections made by the user.

Now,

I can get the selected operators in the following manner:


String op1=(cmb1[0].getSelectedItem()).toString();
String op2=(cmb1[1].getSelectedItem()).toString();


Now,

I have want the expression

if(a OP1 b OP2 c)

to be evaluated dynamically according to above user choices...and I don't want to use an if ladder for all the permutations involving
a,b,c and the different values that OP1 and OP2 can take.


Please suggest some methods to do this.

Thank You.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off the top of my head, I'd build a method that can carry out the binary operations you need based on the String. You wouldn't need to separately consider different permutations then.

Something like:
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:


Surely you mean After all, Java has its own bitwise XOR operator
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew something was nagging at me as I wrote that .
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:



Well, that doesn't respect the operator precedence, now does it?
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other way to solve this could be to do it in a more object oriented way. For example create an operator class for each logical operator (I guess only AND, OR and XOR are relevant) and make each type be responsible for calculating the result of its operation, given two operands. And create some factory class that takes an string as input and outputs an instance of the corresponding operator class. So if the input is "AND" then the output is an instance of the AndOperator class.

And then create one class that is responsible for assigning the operands to the operators. Note that when you have an expression like "a OP1 b OP2 c" this is easier said then done. I mean, which operator should the b-operand belong to? And if we say that it belongs to OP1, then what should be the "left" operand for OP2?
Well the answer to this lies in the operator precedence. AND comes before OR, so if the expression is "a AND b OR c" then the AND operator should get the operands "a" and "b". So what should the OR operator get as left operand? Well, the result of the AND operation! And if you design this cleverly you don't have to know the result of the AND operation when you assign the operands for the OR operation. I'll give you a hint: the operand fields doesn't have to be regular primitive booleans, they can actually be operators themselves.

I actually wrote something like this in school some years back, and it was quite fun.
 
Azeem Mohammed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your help Matthew,Rob And Jimi.
I think Matthew's approach is best suited to my current needs.But, I'll surely try out the method suggested by Jimi.
Cheers!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic