STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Similar documents
[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

STUDENT LESSON A12 Iterations

COMP combinational logic 1 Jan. 18, 2016

Introduction to Computer Architecture

Integers and Rational Numbers

STUDENT LESSON A17 Quadratic Sorting Algorithms

STUDENT LESSON A9 Recursion

Course: AP Computer Science A Description and Syllabus Description of Course:

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Information Science 1

Information Science 1

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Computational Expression

Propositional Calculus. Math Foundations of Computer Science

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

EAS230: Programming for Engineers Lab 1 Fall 2004

Lecture 19. Topics: Chapter 9. Simulation and Design Moving to graphics library Unit Testing 9.5 Other Design Techniques

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Logic, Words, and Integers

Dr. Chuck Cartledge. 10 June 2015

SYNERGY INSTITUTE OF ENGINEERING & TECHNOLOGY,DHENKANAL LECTURE NOTES ON DIGITAL ELECTRONICS CIRCUIT(SUBJECT CODE:PCEC4202)

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

8.3 Common Loop Patterns

APCS Semester #1 Final Exam Practice Problems

Function Call Stack and Activation Records

Introduction to Boolean Algebra

Information Science 1

LAB: WHILE LOOPS IN C++

Introduction to Boolean Algebra

Basics of Programming with Python

2. BOOLEAN ALGEBRA 2.1 INTRODUCTION

LECTURE 2 An Introduction to Boolean Algebra

2.6 BOOLEAN FUNCTIONS

ENGIN 112 Intro to Electrical and Computer Engineering

Unit-IV Boolean Algebra

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

Python Programming: An Introduction To Computer Science

Computer Organization and Levels of Abstraction

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

Objectives: 1- Bolean Algebra. Eng. Ayman Metwali

Adapted from Code.org curriculum

Bits, Words, and Integers

CSC101 - BMCC - Spring /22/2019. Lab 07

Introduction to Boolean logic and Logical Gates

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Combinational Devices and Boolean Algebra

Coefficient Constant Equivalent expressions Equation. 3 A mathematical sentence containing an equal sign

Combinational Circuits Digital Logic (Materials taken primarily from:

CPSC 427: Object-Oriented Programming

Chapter 2 Boolean algebra and Logic Gates

Lesson Plan. Preparation

Discrete structures - CS Fall 2017 Questions for chapter 2.1 and 2.2

AP Computer Science A Syllabus

Lesson 38: Conditionals #2 (W11D3)

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Computer Organization and Levels of Abstraction

Chapter 3. Boolean Algebra and Digital Logic

1. Boolean algebra. [6] 2. Constructing a circuit. [4] 3. Number representation [4] 4. Adders [4] 5. ALU [2] 6. Software [4]

Henry Lin, Department of Electrical and Computer Engineering, California State University, Bakersfield Lecture 7 (Digital Logic) July 24 th, 2012

Logic Design: Part 2

Honors Geometry Chapter 2 Test * Required

LSN 4 Boolean Algebra & Logic Simplification. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CONTROL FLOW CREATING ALTERNATE PATHS OF EXECUTION USING: BRANCHING WHILE LOOPS FOR LOOPS

STUDENT LESSON A7 Simple I/O

Assignment (3-6) Boolean Algebra and Logic Simplification - General Questions

Student Outcomes. Lesson Notes. Classwork. Example 2 (3 minutes)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Starting Boolean Algebra

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP)

Lecture (04) Boolean Algebra and Logic Gates

Lecture (04) Boolean Algebra and Logic Gates By: Dr. Ahmed ElShafee

Random Numbers. Chapter 14

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Designing Computer Systems Boolean Algebra

Introduction to Java Programs for Packet #4: Classes and Objects

AP Computer Science A

Programming Assignment #4 Arrays and Pointers

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

CS40-S13: Functional Completeness

Operators. Java operators are classified into three categories:

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Binary Values. CSE 410 Lecture 02

Chapter 3. Set Theory. 3.1 What is a Set?

Chapter 4. Operations on Data

Propositional Logic. Application of Propositional logic

Algebra of Sets. Aditya Ghosh. April 6, 2018 It is recommended that while reading it, sit with a pen and a paper.

Decisions: Logic Java Programming 2 Lesson 7

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Introduction to Computer Programming for Non-Majors

QUESTION BANK FOR TEST

LAB #1 BASIC DIGITAL CIRCUIT

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday)

Transcription:

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Java Curriculum for AP Computer Science, Student Lesson A14 1 STUDENT LESSON A14 Boolean Algebra and Loop Boundaries INTRODUCTION: Conditional loops often prove to be one of the most difficult control structures to work with. This lesson will give you more strategies that can be used for defining the beginning and ending conditions for loops in your programs. The key topics for this lesson are: A. Negations of Boolean Assertions B. Boolean Algebra and DeMorgan's Laws C. Application of DeMorgan's Laws VOCABULARY: ASSERTION BOOLEAN ASSERTIONS BOOLEAN ALGEBRA DE MORGAN'S LAWS DISCUSSION: A. Negations of Boolean Assertions 1. A Boolean assertion is simply an expression that results in a true or false answer. For example, a > 5 0 == b a <= b are all statements that will result in a true or false answer. 2. To negate a Boolean assertion means to write the opposite of a given Boolean assertion. For example, given the following Boolean assertions noted as A, the corresponding negated statements are the result of applying the! operator to A. A!A 5 == x 5!= x x < 5 x >= 5 x >= 5 x < 5 3. Notice that negations of Boolean assertions can be used to re-write code. For example: if (!(x < 5)) // do something... can be rewritten as

Java Curriculum for AP Computer Science, Student Lesson A14 2 if (x >= 5) // do something... This is important because we understand positive statements much more easily than statements that contain one or more!s. B. Boolean Algebra and DeMorgan's Laws 1. Boolean Algebra is a branch of mathematics devoted to the study of Boolean values and operators. Boolean Algebra consists of these fundamental operands and operators: operands (values): true, false operators: and (&&), or ( ), not (!) (Note: Java has other Boolean operators, such as ^ (XOR exclusive or ) and equivalence. This curriculum does not cover these other operators because they are not part of the AP subset.) 2. There are many identities that have been developed to use with compound Boolean expressions. Two of the more useful identities are DeMorgan's Laws, which are used to negate compound Boolean expressions. DeMorgan's Laws:!(A B) ->! A &&! B!(A && B) ->! A! B The symbols A and B represent the Boolean values, true or false. 3. Here is the truth table that proves the first DeMorgan's Law. A B!(A B)!A!B!A&&!B true true false false false false true false false false true false false true false true false false false false true true true true Notice that columns with the titles! (A B) and! A &&! B result in the same answers.

Java Curriculum for AP Computer Science, Student Lesson A14 3 4. Following is the truth table that proves the second DeMorgan's Law. A B!(A&&B)!A!B!A!B true true false false false false true false true false true true false true true true false true false false true true true true Notice that columns with the titles! (A && B) and! A! B result in the same answers. 5. Here is a good way to think about both of DeMorgan's Laws. Notice that it is similar to the distributive postulate in mathematics. The not operator is distributed through both terms inside of the parentheses, except that the operator switches from and to or, or vice versa.!(a && B) ->! A! B!(A B) ->! A &&! B C. Application of DeMorgan's Laws 1. The casino game of craps involves rolling a pair of dice. The rules of the game are as follows. (Refer to Lesson A6, Libraries and APIs if you need to review the Random class) If you roll a 7 or 11 on the first roll, you win. If you roll a 2, 3, or 12 on the first roll, you lose. Otherwise, rolling a 4, 5, 6, 8, 9, or 10 establishes what is called the point value. If you roll the point value before you roll a 7, you win. If you roll a 7 before you match the point value, you lose. After the point value has been matched, or a 7 terminates the game, play resumes from the top. 2. The following sequences of dice rolls give these results. 7 player wins 4 5 3 7 player loses 8 6 2 8 player wins 3 player loses 3. The rules of the game are set so that the house (casino) always wins a higher percentage of the games. Based on probability calculations, the actual winning percentage of a player is 49.29%.

Java Curriculum for AP Computer Science, Student Lesson A14 4 See Handout A14.1, Craps.java 4. A complete program, Craps.java, is provided in Handout A14.1. The application of DeMorgan's Laws occurs in the getpoint() method. The do-while loop has a compound exit condition. do{ sum = rolldice(); } while ((sum!= point) && (sum!= 7)); 5. When developing a conditional loop, it is very helpful to think about what assertions are true when the loop will be finished. In other words, when the loop is done, what will be true? 6. When the loop in getpoint is done, one of two things will be true: a. the point will be matched (sum == point). b. or a seven has been rolled. These two statements can be combined into one summary assertion statement: ((sum == point) (sum == 7)) 7. The loop assertion states what will be true when the loop is done. Once you have established the loop assertion, writing the boundary condition involves a simple negation of the loop assertion. 8. Taking the assertion developed in Part 6 above, the negation of the assertion follows.!((sum == point) (sum == 7)) 9. Applying DeMorgan's law results in (!(sum == point)) && (!(sum == 7)) Rewriting each half of the expression gives (sum!= point) && (sum!= 7) 10. Looking at the first half of the developing boundary condition, the statement (sum!= point) means that we have not yet matched the point. In other words, we haven t won yet. 11. The second half of the boundary condition (value!= 7) means we have not yet "crapped" out (i.e., rolled a 7). In other words, we also haven t lost yet, so we must keep rolling the dice. 12. You may use the equivalent Boolean expressions in Parts 8 and 9 interchangeably. Choose the one that you think makes your code easier to read.

Java Curriculum for AP Computer Science, Student Lesson A14 5 SUMMARY/ REVIEW: Conditional loops are some of the hardest pieces of code to write correctly. The goal of this lesson is to have a structured approach for the construction of conditional loops. Thinking in the positive sense is typically easier. Determine what will be true when the loop is done. Negate this condition to stay in the loop. ASSIGNMENT: Lab Assignment A14.1, Rolling Handout A14.1, Craps.java Worksheet A14.1, Boolean Algebra