Search code examples
javatruthtable

Truth Table - Assistance


I have a question for an assignment. The questions is basically the following: There are 2 integer variables say A and B. Both these integers contain data. Using a truth table, which, if any, of the following IF statement tests is equivalent to:

if (!(A == 60 && B == 40))   

 -   if (A != 60 || B != 40)
 -  if (A == 60 && (!(B == 40)))

How would i tackle this please. Anything advice would be appreciated. I think that I have to create a table with three columns - one called A, another B, and the third column called RESULT (YES OR NO).

The statement: if (!(A == 60 && B == 40)) - I am not to sure how to read the part if (!. In other words, the part A == 60 && B == 40 is telling me essentially that A must equal 60 AND AT THE SAME TIME B must equal 40. Following that I am confused. Any help/advise please would be appreciated.

Thanks Chris


Solution

  • This really has nothing to do with Java per se. Yes, you can solve by writing the truth tables. The ! means logical negation or not or you may even think of it as opposite. Personally, I find it helpful to establish all the parts of a particular truth table.

       a    |   b    |    !b     |       a & b       |      a & !b        |      !(a & b)
     ----------------------------------------------------------------------------------------
     A = 60 | B = 40 | !(B = 40) | (A = 60 & B = 40) | A = 60 & !(B = 40) | !(A = 60 & B = 40)
       T    |   T    |     F     |         T         |        F           |          F
       T    |   F    |     T     |         F         |        T           |          T
       F    |   T    |     F     |         F         |        F           |          T
       F    |   F    |     T     |         F         |        F           |          T
    

    You should note your particular example is subject to one of De Morgan's Laws.

    enter image description here

    P is A = 60  
    Q is B = 40  
    ¬ is !  
    ∧ is &&   
    ∨ is ||   
    

    so...

    !(A && B) is really the same as !A || !B

    The truth table tells you the rest you need to know to solve that problem.