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

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

Research Methods Lecturer: Steve Maybank
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Department of Computer Science and Information Systems Autumn 2013 Preliminary.
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
22 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Computer Systems
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
CS110 Programming Language I
11 November 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Building Java Programs
Computer Programming Lab 8.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Computers & Programming Languages
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Presentation transcript:

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