Introduction to Programming Java Lab 5: Boolean Operations 8 February 2013 1 JavaLab5 lecture slides.ppt Ping Brennan

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Lecture 4 More on Java® Data Types, Control Structures.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
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.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
1 March 2013Birkbeck College, U. London1 Introduction to Programming 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
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
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.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
February 12, 2013 COMP Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
This is Java Jeopardy.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
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.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Computer Programming Lab(7).
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Computer Programming Lab 8.
Java Planning our Programs Flowcharts Arithmetic Operators.
If Statements & Relational Operators Programming.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to Programming
Something about Java Introduction to Problem Solving and Programming 1.
OPERATORS (2) CSC 111.
Introduction to Programming
Introduction to Classes and Methods
Lecture Notes – Week 3 Lecture-2
Fundamentals 2.
Introduction to Programming
Presentation transcript:

Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan

Java Project Project Name: JavaLab5 2 TruthTable Comparison TruthTable2

Class TruthTable Objectives –Understand the use of the type boolean and boolean expressions. –Applying boolean operators: &&, ||, ! Boolean Operators 3 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

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 Valuetrue falsetrue

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 pqr(p && q)!r (p && q) || !r false true false truefalse truefalse true false true falsetrue falsetrue falsetrue false truefalsetruefalse

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 true boolean b = (10 > 3); // return true (a && b ) return true Boolean Truth Table for && Back to Class TruthTableClass TruthTable 11 aba && b true false truefalse

An example on using boolean operator || boolean a = (5 < 10); // return true boolean b = (10 < 3); // return false ( a || b ) return true Boolean Truth Table for || Back to Class TruthTableClass TruthTable 12 aba || b true falsetrue falsetrue false

An example on using boolean operator ! boolean a = (5 < 10); // return true ( !a ) return false Boolean Truth Table for ! Back to Class TruthTableClass TruthTable 13 a!a truefalse true