8 february 2013birkbeck college, u. london1 introduction to programming lecturer: steve maybank...

24
8 February 2013 Birkbeck College, U. London 1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Spring 2013 Week 5: Boolean Operations

Upload: hayden-reilly

Post on 28-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 2: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 3: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 4: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 5: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 6: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 7: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 8: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 9: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 10: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 11: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 12: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 13: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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.

Page 14: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 15: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 16: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 17: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 18: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 19: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 20: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 21: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 22: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 23: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 24: 8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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