Wednesday, April 16, 2008

ICA-CP Class Info: Boolean Algebra

State:
Boolean algebra is also known as predicate calculus. Boolean algebra uses 1 for true and zero for false, and applies logical truth tables to binary inputs to create an output or result. Boolean algebra is used in electronics to design chips for computers as well as in philosophy to determine the truth of logical statements. Java uses boolean algebra with if and while statements to make decisions.


Elaborate:

We are studying three boolean gates with their truth tables. They are OR, AND, and NOT. The AND gate is represented by && in Java, the OR gate by || and the NOT by ! (exclamation point). The truth tables for AND, OR, and NOT may be found at this link (since a blog does not display tables well): http://www.doc.ic.ac.uk/~dfg/hardware/HardwareLecture01.pdf
At this web site, see Diagram 1.2 Truth Tables. Remember, for an AND gate, the only time the output is true is when both inputs are true. An OR gate output is true if either of the two inputs are true. Finally, the output of a NOT gate is always opposite of the input. For more info, see this wikipedia entry: http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29

Exemplify:
AND example in Java:
boolean friday = 1;
boolean senior = 1;

if (friday && senior)
{
System.out.println("I can wear free dress.")
}

Since both friday and senior are true, then the program will display "I can wear free dress." However, if we changed the value of friday to false, the program would not execute the if statement and nothing would be displayed.

OR exmaple in Java:


boolean skyIsBlue = 1;
boolean sunIsShining = 0;

if (skyIsBlue || sunIsShining)
{
System.out.println("Weather is good!")
}

Since the skyIsBlue is true, but sunIsShining is false, the program will display "Weather is good." The if statement executes with an OR gate any time one of the conditions is true. In other words, both conditions (skyIsBlue and sunIsShining) do not have to be true to execute the code.

Example of NOT in Java:

boolean dayOff = 1;
boolean holiday = 1;

if (!dayOff && holiday)
{
System.out.println ("It's a school day.")
}

In this example, dayOff is true. But, the NOT changes the input to its opposite value. So, it makes dayOff false. Since the !dayOff is false and the holiday is true, the resultant output of the truth table for the AND gate is false. The if statement does not execute, nothing is printed. (Remember that and AND gate requires both inputs to be true for the output to be true.)

Illustrate:
Using boolean algebra is Java programs is much like making decisions in daily life. If it is Friday and I-am-a-senior, I may wear free dress. However, if I-am-a-senior, and it isn't Friday, then I can't wear free dress. However, boolean algebra in programs may be logically correct sometimes but not make practical sense. For example, if I say this:

boolean raining = 1;
boolean sunIsShining = 1;

if (raining && sunbIsShining)
{
System.out.println("Weather is good!");
}

the program displays "Weather is good!" even though I indicate that it is raining. When we use boolean algebra in Java programs, we follow the truth table logic even though it may not always make practical sense.

0 Comments:

Post a Comment

<< Home