8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2013 Week 5: Boolean Operations
Overview Java Lab 3, Exercises 2 and 4 Relational Operators Equality of strings Boolean operators De Morgans laws See Java for Everyone, Ch. 3 8 February 2013Birkbeck College, U. London2
Java Lab 3, Exercise 2 Integer calculations: write a program that prompts the user for two integers and then prints The sum The difference … 8 February 2013Birkbeck College, U. London3
Keyboard Input import java.util.Scanner; … Scanner in = new Scanner(System.in); System.out.print(Please input an integer: ); int i = in.nextInt(); System.out.print(Please input a second integer: ); int j = in.nextInt(); 8 February 2013Birkbeck College, U. London4
Overflow Integers of type int must be in the range –2 31 to The following two integers are in this range: int i= ; int j = ; System.out.println(i+j); /* result: */ System.out.println(i-j); /* result: -1 */ There are no error messages when i+j is evaluated. 8 February 2013Birkbeck College, U. London5
Java Lab 3, Exercise 4 Separate digits: write a program that reads in a five digit positive integer and prints out the individual digits, separated by spaces. For example is printed out as February 2013Birkbeck College, U. London6
Possible Solutions Input a five digit integer i Extract the digits, eg. d1=i%10; Print the digits and spaces. Alternative: use in.next() to input the five digits of i in the form of a string. Divide the string into five substrings, one for each digit. Reassemble the five substrings, with spaces. 8 February 2013Birkbeck College, U. London7
Solution String digits=in.next(); String d1=digits.substring(0,1); String d2=digits.substring(1,2); String d3=digits.substring(2,3); String d4=digits.substring(3,4); String d5=digits.substring(4,5); String gap= ; String result = d1+gap+d2+gap+d3+gap+d4+gap+d5; 8 February 2013Birkbeck College, U. London8
Relational Operators 8 February 2013Birkbeck College, U. London9 JavaMathDescription >>greater than >=greater than or equal <<less than <=less than or equal ===equal !=not equal
Relational Operator Examples 8 February 2013Birkbeck College, U. London10 ExpressionValueComment 3 <= 4true<= is less than or equal 3 =< 4errorUse <= 3 > 4false> is the opposite of <= 4 < 4false< is strict inequality 3 != 5-1true!= tests for inequality 1.0/3.0 == falseThe numbers are similar but not equal 10 > 5errorA string cannot be compared with a number
Precedence Relational operators have a lower precedence than arithmetical operators, eg. int floor = 10; boolean v = floor-1 < 13; The expression floor-1 is evaluated first and the value is then compared with February 2013Birkbeck College, U. London11
Boolean Data Type A variable of type boolean has either the value true or the value false, eg. boolean temp = true; boolean is a reserved word, true and false are values. It is not the case that true is 1 and false is 0. 8 February 2013Birkbeck College, U. London12
Boolean Operators 8 February 2013Birkbeck College, U. London13 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.
Boolean Operator Examples 8 February 2013Birkbeck College, U. London14 ExpressionValueComment 0<20 && 20<10falseOnly the first condition is true 0<20 || 20<10trueThe first condition is true 0<x<100error0<x has a boolean value, 100 is an integer !(0<200)false0<200 is true frozen==truefrozenNo need to compare a boolean value with true
Combining Conditions Think carefully about the difference between && and || Buying a shirt: white, cotton, size 15 Buying apples: from the UK, from France, from South Africa 8 February 2013Birkbeck College, U. London15
Lazy Evaluation of Boolean Expressions Logical expressions are evaluated left to right. The evaluation stops as soon as the truth value is determined, eg. int quantity = 0; boolean test1 = quantity > 0 && price/quantity < 10; /* test1 is false */ boolean test2 = quantity == 0 || price/quantity < 10; /* test2 is true */ 8 February 2013Birkbeck College, U. London16
Compile Time Errors int temp = 40; boolean test1 = (0 <= temp <=100); /* 0<=temp is true, true<=100 is an error */ boolean test2 = (temp == 40||50); /* 40||50 is an error because || cannot be applied to integers */ 8 February 2013Birkbeck College, U. London17
Equality of Strings Use boolean test = string1.equals(string2); Example String str1 = red; String str2 = blue; System.out.println(str1.equals(str2)); /* Prints: false */ 8 February 2013Birkbeck College, U. London18
More on Equality of Strings str1==str2 returns true if and only if str1 and str2 are initialised with the same string literal, eg. String str1=Rob, str2=Robert; boolean test1=(str1==Rob); //true boolean test2 = (str1==str2.substring(0,3)); //false 8 February 2013Birkbeck College, U. London19
Lexicographic Ordering int sc=string1.compareTo(string2); /* sc<0: string1 precedes string2 sc==0: string1 equals string2 sc>0: string1 follows string2 */ 8 February 2013Birkbeck College, U. London20
De Morgans Laws Let a, b be Boolean variables. Then !(a && b) and (!a) || (!b) have the same truth table. Similarly !(a || b) and (!a) && (!b) have the same truth table. 8 February 2013Birkbeck College, U. London21
Check the First Law Suppose a is false. Then !(a && b) = !(false && b) = !false = true (!a) || (!b)= (!false) || b = true||b = true Suppose a is true. Then !(a && b) = !(true && b) = !b (!a)||(!b) = (!true)||(!b) = false||(!b)=!b 8 February 2013Birkbeck College, U. London22
Example of a De Morgans Law Buying apples: boolean reject1 = (!fromUK) && (!fromFR); boolean reject2 = !(fromUK || fromFR); /* reject1 has the same value as reject2 */ 8 February 2013Birkbeck College, U. London23
Test Suppose that x and y are integers. Test whether both of them are zero. Test whether at least one of x, y is zero. Test whether exactly one of x, y is zero. What is the value of !!(x>y)? 8 February 2013Birkbeck College, U. London24