// truth table: David I. Schwartz, 1/2000 // demonstrate boolean values and rudimentary formatting // note that you can print a boolean expression public class truthtable1 { public static void main(String args[]) { System.out.println("============================="); // The \t prints a TAB in output System.out.println("A\tB\t| AND\tOR"); System.out.println("================+============"); // Boolean literals: true and false // You can actually use the words true and false in code // Java will treat them as actual values you can manipulate with // operators like && (AND) and || (OR) // Use T and F identifiers to make code easier to read boolean T = true; boolean F = false; // Produce a truth table // Use operations for AND and OR for all combinations of T and F // You must surround boolean expressions with () // Java needs to do those operations before printing System.out.println(T + "\t" + T + "\t| " + (T && T) + "\t" + (T || T)); System.out.println(T + "\t" + F + "\t| " + (T && F) + "\t" + (T || F)); System.out.println(F + "\t" + T + "\t| " + (F && T) + "\t" + (F || T)); System.out.println(F + "\t" + F + "\t| " + (F && F) + "\t" + (F || F)); } } /* Output: ============================= A B | AND OR ================+============ true true | true true true false | false true false true | false true false false | false false */