Truth Table - Assistance

北城以北 提交于 2019-12-06 20:59:27

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.

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.

I hope this code that I create help you to get an idea, this is a simple code to display the Truth Table using binary (0s and 1s) numbers instead of Boolean (TRUE, FALSE)

/**
 * Truth table for the logical operators. Using
 * zeros and ones.
 * 
 * @ Samuel Mayol
 */

public class LogicalOpTable {
    public static void main(String[] args) {

        boolean p, q; 
        byte p1, q1, pAndQ, pOrQ, pXORq, notP, pq;

        System.out.println("Using 0s and 1s for the Truth Table:");
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); // using tabs \t

        pq =1;

        switch (pq) { // without break it will execute the whole swich case statements
            case 1 : 
                p = true; q = true;
                p1=(byte) (p?1:0); // The ? : operator in Java
                q1=(byte) (q?1:0); // Short form for Java If statement
                pAndQ=(byte) ((p&q)?1:0); 
                pOrQ=(byte) ((p|q)?1:0);
                pXORq=(byte) ((p^q)?1:0);
                notP= (byte) ((!p)?1:0);

                System.out.print(p1 + "\t" + q1 + "\t");
                System.out.print(pAndQ + "\t" + pOrQ + "\t");
                System.out.println(pXORq + "\t" + notP);
            case 2: 
                p = true; q = false;
                p1=(byte) (p?1:0); // The ? : operator in Java
                q1=(byte) (q?1:0); // Short form for Java If statement
                pAndQ=(byte) ((p&q)?1:0); 
                pOrQ=(byte) ((p|q)?1:0);
                pXORq=(byte) ((p^q)?1:0);
                notP= (byte) ((!p)?1:0);

                System.out.print(p1 + "\t" + q1 + "\t");
                System.out.print(pAndQ + "\t" + pOrQ + "\t");
                System.out.println(pXORq + "\t" + notP);
            case 3: 
                p = false; q = true;
                p1=(byte) (p?1:0); // The ? : operator in Java
                q1=(byte) (q?1:0); // Short form for Java If statement
                pAndQ=(byte) ((p&q)?1:0); 
                pOrQ=(byte) ((p|q)?1:0);
                pXORq=(byte) ((p^q)?1:0);
                notP= (byte) ((!p)?1:0);

                System.out.print(p1 + "\t" + q1 + "\t");
                System.out.print(pAndQ + "\t" + pOrQ + "\t");
                System.out.println(pXORq + "\t" + notP);
            case 4: 
                p = false; q = false;
                p1=(byte) (p?1:0); // The ? : operator in Java
                q1=(byte) (q?1:0); // Short form for Java If statement
                pAndQ=(byte) ((p&q)?1:0); 
                pOrQ=(byte) ((p|q)?1:0);
                pXORq=(byte) ((p^q)?1:0);
                notP= (byte) ((!p)?1:0);

                System.out.print(p1 + "\t" + q1 + "\t");
                System.out.print(pAndQ + "\t" + pOrQ + "\t");
                System.out.println(pXORq + "\t" + notP);
        }
    }
}

The result after running this code is:

Using 0s and 1s for the Truth Table:
P   Q   AND OR  XOR NOT
1   1   1   1   0   0
1   0   0   1   1   0
0   1   0   1   1   1
0   0   0   0   0   1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!