Win a copy of Design for the Mind this week in the Design forum!

# How to make the evaluation of an If expression dynamic?

Azeem Mohammed
Greenhorn
Posts: 12
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.

Matthew Brown
Bartender
Posts: 4566
8
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:

Rob Spoor
Sheriff
Posts: 20512
54
Matthew Brown wrote:

Surely you mean After all, Java has its own bitwise XOR operator

Matthew Brown
Bartender
Posts: 4566
8
I knew something was nagging at me as I wrote that .

Jimi Svedenholm
Ranch Hand
Posts: 53
Matthew Brown wrote:

Well, that doesn't respect the operator precedence, now does it?

Jimi Svedenholm
Ranch Hand
Posts: 53
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
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!!