introduction to programming java lab 5: boolean operations 8 february 2013 1 javalab5 lecture...

13
Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan ([email protected])

Upload: hayden-thorpe

Post on 10-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Introduction to Programming

Java Lab 5:Boolean Operations

8 February 2013 1JavaLab5 lecture slides.ppt

Ping Brennan ([email protected])

Java Project

Project Name: JavaLab5

2

TruthTableTruthTable

Comparison

TruthTable2TruthTable2

Class TruthTable

• Objectives– Understand the use of the type boolean and boolean

expressions.– Applying boolean operators: && , || , !

• Boolean Operators

3

Java Name Description

&& And Binary: a && b is true if and only if a and b are both true

|| Or Binary: a || b is true if and only if at least one of a, b is true

! Not Unary: !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

Value

true true true true true

false

true

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

p q r (p && q) !r (p && q) || !r

false

false false false true true

false

false true false false false

false

true false false true true

true false false false true true

true true false true true true

true true true true false true

false

true true false false false

true false true false false false

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 trueboolean b = (10 > 3); // return true(a && b ) return true

Boolean Truth Table for &&

Back to Class TruthTable11

a b a && b

true true true

true false false

false true false

false false false

An example on using boolean operator ||

boolean a = (5 < 10); // return trueboolean b = (10 < 3); // return false( a || b ) return true

Boolean Truth Table for ||

Back to Class TruthTable

12

a b a || b

true true true

true false true

false true true

false false false

An example on using boolean operator !

boolean a = (5 < 10); // return true( !a ) return false

Boolean Truth Table for !

Back to Class TruthTable

13

a !a

true false

false true