8 february 2013birkbeck college, u. london1 introduction to programming lecturer: steve maybank...
TRANSCRIPT
8 February 2013 Birkbeck College, U. London 1
Introduction to Programming
Lecturer: Steve Maybank
Department of Computer Science and Information Systems
[email protected] 2013
Week 5: Boolean Operations
Overview Java Lab 3, Exercises 2 and 4 Relational Operators Equality of strings Boolean operators De Morgan’s laws See Java for Everyone, Ch. 3
8 February 2013 Birkbeck College, U. London 2
Java Lab 3, Exercise 2 Integer calculations: write a
program that prompts the user for two integers and then prints
The sumThe difference…
8 February 2013 Birkbeck College, U. London 3
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 2013 Birkbeck College, U. London 4
Overflow Integers of type int must be in the range –231 to 231-
1. The following two integers are in this range:
int i=1500000000;int j = 1500000001;System.out.println(i+j);/* result: -1294967295 */System.out.println(i-j);/* result: -1 */
There are no error messages when i+j is evaluated.
8 February 2013 Birkbeck College, U. London 5
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 16348 is printed out as 1 6 3 4 8
8 February 2013 Birkbeck College, U. London 6
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 2013 Birkbeck College, U. London 7
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 2013 Birkbeck College, U. London 8
Relational Operators
8 February 2013 Birkbeck College, U. London 9
Java
Math
Description
> > greater than
>= greater than or equal
< < less than
<= less than or equal
== = equal
!= not equal
Relational Operator Examples
8 February 2013 Birkbeck College, U. London 10
Expression Value Comment
3 <= 4 true <= is less than or equal
3 =< 4 error Use <=
3 > 4 false > is the opposite of <=
4 < 4 false < is strict inequality
3 != 5-1 true != tests for inequality
1.0/3.0 ==0.33333333
false The numbers are similar but not equal
“10” > 5 error A 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 13.
8 February 2013 Birkbeck College, U. London 11
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 2013 Birkbeck College, U. London 12
Boolean Operators
8 February 2013 Birkbeck College, U. London 13
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.
Boolean Operator Examples
8 February 2013 Birkbeck College, U. London 14
Expression Value Comment
0<20 && 20<10 false Only the first condition is true
0<20 || 20<10 true The first condition is true
0<x<100 error 0<x has a boolean value, 100 is an integer
!(0<200) false 0<200 is true
frozen==true frozen No 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 2013 Birkbeck College, U. London 15
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 2013 Birkbeck College, U. London 16
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 2013 Birkbeck College, U. London 17
Equality of Strings Use
boolean test = string1.equals(string2);
ExampleString str1 = “red”;String str2 = “blue”;System.out.println(str1.equals(str2));/* Prints: false */
8 February 2013 Birkbeck College, U. London 18
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”); //trueboolean test2 = (str1==str2.substring(0,3)); //false
8 February 2013 Birkbeck College, U. London 19
Lexicographic Ordering
int sc=string1.compareTo(string2);
/* sc<0: string1 precedes string2 sc==0: string1 equals string2 sc>0: string1 follows string2
*/
8 February 2013 Birkbeck College, U. London 20
De Morgan’s 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 2013 Birkbeck College, U. London 21
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 2013 Birkbeck College, U. London 22
Example of a De Morgan’s Law
Buying apples:
boolean reject1 = (!fromUK) && (!fromFR);
boolean reject2 = !(fromUK || fromFR);
/* reject1 has the same value as reject2 */
8 February 2013 Birkbeck College, U. London 23
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 2013 Birkbeck College, U. London 24