Download presentation

Presentation is loading. Please wait.

Published byHayden Thorpe Modified over 4 years ago

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

2
Java Project Project Name: JavaLab5 2 TruthTable Comparison TruthTable2

3
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

4
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

5
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

6
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

7
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

8
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

9
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) )

10
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

11
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

12
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

13
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

Similar presentations

OK

1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,

1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on cmos image sensor Ppt on credit default swaps greece Ppt on right to information act Ppt on area of parallelogram and triangles in geometry Download ppt on mind controlled robotic arms for humans Ppt on relations and functions for class 11th Ppt on management by objectives pdf Ppt on movable bridge Ppt on javascript events trigger Ppt on modern techniques of agriculture