Presentation is loading. Please wait.

# Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan

## Presentation on theme: "Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan"— Presentation transcript:

Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

Java Project Project Name: JavaLab5 2 TruthTable Comparison TruthTable2

Class TruthTable Objectives –Understand the use of the type boolean and boolean expressions. –Applying boolean operators: &&, ||, ! Boolean Operators 3 JavaNameDescription && AndBinary: a && b is true if and only if a and b are both true || OrBinary: a || b is true if and only if at least one of a, b is true !NotUnary: !a is true if and only if a is false

Class TruthTable Assign boolean values, true or false, to variables p, q, and r depending on the values of a, b and c read in at the keyboard: If a = 3, b = -5, and c = 10, then Expr: shorthand for expression Print out the value of the boolean expression: (p && q) || !r 4 boolean p = ( a != 0 ); boolean q = ( b != 0 ); boolean r = ( c != 0 ); Expr(a != 0)(b != 0)(c != 0)(p && q)r!r(p && q) || !r Valuetrue falsetrue

Anatomy of Class TruthTable import java.util.Scanner; public class TruthTable { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter three numbers: "); int a = in.nextInt(); /* write statements to declare variables b and c of type int, and they are assigned input from the keyboard */ boolean p = (a != 0); // write similar statements to declare and // initialise the variables q and r of type boolean System.out.println( (p && q) || !r); } 5

Class TruthTable2 Objective –print out all eight lines of the truth table for (p && q) || !r Truth table 6 pqr(p && q)!r (p && q) || !r false true false truefalse truefalse true false true falsetrue falsetrue falsetrue false truefalsetruefalse

Anatomy of Class TruthTable2 public class TruthTable2 { public static void main(String[] args) { System.out.println("Truth Table for (p && q) || !r"); System.out.println( (false && false) || !false); System.out.println( (false && false) || !true); System.out.println( (false && true) || !false); /* Write more statements to print out the remaining five lines of the truth table. */ } 7

Class Comparison Objectives –Read in values of three variables x, y, z of type int –Find a Boolean expression which: takes the value true if exactly two of the variables (x, y, z) have the same value, and takes the value false otherwise. –Test your program using a range of inputs. 8

Class Comparison (2) Formulae to solve parts of the problem: Finally use an Or operator || to join all three boolean expressions into a single expression. An example: if x = 1, y =1 and z = 3, then the expression (x == y) && (x ! = z) returns true. 9 ( (x == y) && (x != z) ) ( (x == z) && (z != y) ) ( (y == z) && (y != x) )

Anatomy of Class Comparison public class Comparison { public static void main(String[] args) { /* declare three variables x, y, z of type int and read input from the keyboard for these variables. */ /* Write a Java statement to declare a variable p of type boolean, and assign it the boolean expression given in the previous slide. */ /* print out the value of the boolean expression. */ } 10

An example on using boolean operator && boolean a = (5 < 10); // return true boolean b = (10 > 3); // return true (a && b ) return true Boolean Truth Table for && Back to Class TruthTableClass TruthTable 11 aba && b true false truefalse

An example on using boolean operator || boolean a = (5 < 10); // return true boolean b = (10 < 3); // return false ( a || b ) return true Boolean Truth Table for || Back to Class TruthTableClass TruthTable 12 aba || b true falsetrue falsetrue false

An example on using boolean operator ! boolean a = (5 < 10); // return true ( !a ) return false Boolean Truth Table for ! Back to Class TruthTableClass TruthTable 13 a!a truefalse true

Download ppt "Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan"

Similar presentations

Ads by Google