Thursday, December 4, 2014

Merge us!!

Hey dude, I have multiple options and I can not write multiple if-else for that. Do you have something better ?
Its a common problem. Life comes with multiple options and you have to choose the perfect one, keeping every aspect in mind.
For example, you are going to buy a new cellphone. You have some option for that.

  • Micromax A90S (INR. 14355/-) with Android v4 (Ice Cream Sandwich), 1 GHz, Dual Core,  8 mp camera, 0.3 mp front camera etc.
  • Nokia Lumia 730 (INR. 13599/-) with Windows 8.1 Phone,  1.2 Ghz, Quad Core, 6.7 mp camera etc.
  • Sony Experia C3 (INR. 15990/-) with Android v4.4 (Kitkat), 1.2 Ghz, Quad Core, 8 mp autofocus etc.
  • List continues...
Say you have your choices
  • Android and budget of INR. 15000/-, then you are most probably going for Micromax A90S. 
  • Windows and budget of INR. 15000/-, then you are most probably going for Nokia Lumia 730
  • Android and budget greater than INR. 15000/-, then you are most probably going for Sony Experia C3
  • List continues...
So, we can see that, our decision change based on more than one necessity.

Lets review the decisions with one more choice of camera specification.
  • Android and budget of INR. 15000/- and 8 mp camera, then you are most probably going for Micromax A90S. 
  • Windows and budget of INR. 15000/- and 6.7 mp camera suffice your need, then you are most probably going for Nokia Lumia 730
  • Android and budget greater than INR. 15000/- and you need 8 mp camera, then you are most probably going for Sony Experia C3
  • List continues...
OK, I got it. Stop selling cell phones now. Come to the point.
So, it is possibble in decision making where you need to deal with more than one parameter. The same situation can occur (or best to say, occurs every now and then in real life scenarios) where control flow changes based on different input. So, the previous section fails to deal with these scenarios.

Oh god, so we are stuck here. Better I leave behind the idea of learning Java.
Hey wait. Did I mention that you can not do that ?
There is a way out here. I am going to discuss next.

Now, I apologise that, I deliberately left the discussion of operators used in Java. That's what you are facing this scenario.

So, why did you leave that ?
Do you feel happy to see me tensed ?
No, I do not feel happy if you are facing problem while working with Java. But, my intention was to let you know about them as and when required. That will make a clear picture of when to use what.

OK, now give me the key to unlock the door or I am going to report your article...
OK, OK. Isn't it good you get a stress and your stress let you do something good ?

As you already know that if-else  deals with boolean expressions, you must feed boolean to if-else blocks. And boolean values define some operation to use,
  • AND
  • OR
  • NOT
  • XOR
  • Material Implication
  • Equivalence
Readers from Electronics background have a great understanding on these. Please let me know if anything goes wrong in this section. Cause I am not sure about all these boolean arithmetic. I am sure about the first four supported in Java.

Only 4 ? Why not all ?
I am not quite sure about this. But I assume that, in practical scenarios, you can build the last two using the preceding 4. Again, will request you to let me know if you have a better understanding on this.

Let me tell you about the first 4, we define the result of different boolean operations in truth tables. If you are not aware about truth tables, a short reading from this, can prove to be useful.
  • AND - We need two boolean operands to use this operation. If both operands execute to true, then the result becomes true.
    pqp & q
    TTT
    TFF
    FTF
    FFF
  • OR - We need two boolean operands to use this operation. If any one of the operands execute to true, then the result becomes true.
    pqp | q
    TTT
    TFT
    FTT
    FFF
  • NOT - We need single boolean operand to use this operation. If the operand executes to true, then the result becomes false and if the operand executes to false, then the result becomes true.
    p~ p
    TF
    FT
  • XOR - We need two boolean operands to use this operation. If both operands are in same state (either both true or both false), then the result becomes false. If the state of operands differ, the result becomes true.
    pqp X q
    TTF
    TFT
    FTT
    FFF
Sounds cool.

Again, I remind you the process of Why >> What >> How learning steps.
Why is defined in the first section of this article, we also knew What we need. Next section defines How to do that.

Now, we know where to use what. The question arises, how to put them in use.
Java, defines 4 boolean operators for the afore mentioned four operations.


  • AND - &
  • OR - |
  • NOT - !
  • XOR - ^
Now putting them altogether in a code,


 /**  
  *   
  */  
 package blog;  
 /**  
  * @author Palash  
  *  
  */  
 public class BooleanOperator {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           boolean operand1True = true, operand1False = false;  
           boolean operand2True = true, operand2False = false;  
           // AND Operations  
           System.out.println("AND Operations");  
           System.out.println("AND operation between two true values: "  
                     + (operand1True & operand2True));  
           System.out.println("AND operation between true and false values: "  
                     + (operand1True & operand2False));  
           System.out.println("AND operation between false and true values: "  
                     + (operand1False & operand2True));  
           System.out.println("AND operation between two false values: "  
                     + (operand1False & operand2False));  
           System.out.println();  
           // OR Operations  
           System.out.println("OR Operations");  
           System.out.println("OR operation between two true values: "  
                     + (operand1True | operand2True));  
           System.out.println("OR operation between true and false values: "  
                     + (operand1True | operand2False));  
           System.out.println("OR operation between false and true values: "  
                     + (operand1False | operand2True));  
           System.out.println("OR operation between two false values: "  
                     + (operand1False | operand2False));  
           System.out.println();  
           // NOT Operations  
           System.out.println("NOT Operations");  
           System.out.println("NOT on true value: " + (!operand1True));  
           System.out.println("NOT on false value: " + (!operand1False));  
           System.out.println();  
           // XOR Operations  
           System.out.println("XOR Operations");  
           System.out.println("XOR operation between two true values: "  
                     + (operand1True ^ operand2True));  
           System.out.println("XOR operation between true and false values: "  
                     + (operand1True ^ operand2False));  
           System.out.println("XOR operation between false and true values: "  
                     + (operand1False ^ operand2True));  
           System.out.println("XOR operation between two false values: "  
                     + (operand1False ^ operand2False));  
      }  
 }  

And the output will look like the following,

 AND Operations  
 AND operation between two true values: true  
 AND operation between true and false values: false  
 AND operation between false and true values: false  
 AND operation between two false values: false  
 OR Operations  
 OR operation between two true values: true  
 OR operation between true and false values: true  
 OR operation between false and true values: true  
 OR operation between two false values: false  
 NOT Operations  
 NOT on true value: false  
 NOT on false value: true  
 XOR Operations  
 XOR operation between two true values: false  
 XOR operation between true and false values: true  
 XOR operation between false and true values: true  
 XOR operation between two false values: false  

OK, that's cool.Now I think, the example you provide, should look soemthing like the following, right ?

 /**  
  *   
  */  
 package blog;  
 /**  
  * @author Palash  
  *  
  */  
 public class BooleanOperator {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           if((budget < 15000) & (os_choice == "Android")){  
                System.out.println("Buy Micromax A90S");  
           }else if((budget < 15000) & (os_choice == "Windows")){  
                System.out.println("Buy Nokia Lumia 730");  
           }else if(){  
                System.out.println("Buy Sony Experia C3");  
           }  
      }  
 }  

Wow! Exactly perfect. You are going right path.

So, it ends here, right ?
No, dear reader, it does not end here.

If you go back the evaluation of boolean expressions, you will find situations, where only single operand defines the control some times.

WHAT ? THEY WORK ON TWO OPERANDS. You confuse me so much...
Yes, you are right, but all the time you don't need both operands. For example, if the first operand in AND operation is false, you will end up getting false. Or, in OR operation, if the first operand is true, you end up having a result true.
To verify check the output of these two operations, you will find the clue.

I think, you got it. In these scenarios, you don't need to go to the next operand or evaluate the second boolean expression. You can safely ignore the second expression.

Well, to ignore the second expressions, Java defines SHORT-CIRCUIT. You may not find it useful for the time being, but they really helps when we work with objects. For now, just know, how to use them. We will define the uses later.

Rewriting the previous program using short circuit,

 /**  
  *   
  */  
 package blog;  
 /**  
  * @author Palash  
  *  
  */  
 public class BooleanOperator {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           boolean operand1True = true, operand1False = false;  
           boolean operand2True = true, operand2False = false;  
           // Short circuit AND  
           System.out.println("AND Operations");  
           System.out.println("AND operation between two true values: "  
                     + (operand1True && operand2True));  
           System.out.println("AND operation between true and false values: "  
                     + (operand1True && operand2False));  
           System.out.println("AND operation between false and true values: "  
                     + (operand1False && operand2True));  
           System.out.println("AND operation between two false values: "  
                     + (operand1False && operand2False));  
           // Short circuit OR  
           System.out.println("OR Operations");  
           System.out.println("OR operation between two true values: "  
                     + (operand1True || operand2True));  
           System.out.println("OR operation between true and false values: "  
                     + (operand1True || operand2False));  
           System.out.println("OR operation between false and true values: "  
                     + (operand1False || operand2True));  
           System.out.println("OR operation between two false values: "  
                     + (operand1False || operand2False));  
      }  
 }  
Notice the short circuit operators in red. They basically are the operators written twice without spaces.

Output of the program is left on your responsibility.

Next we are going to, check out some operators and their uses with some examples.

Thank you for your patience and interest. I agree, it was really a big article...
Would request you to spread the word if you liked this article.


Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment