Show
What if you heard a rumor about a senior at your high school? And then you heard that the rumor wasn’t true - it wasn’t a senior at your high school. Which part of “a senior at your high school” wasn’t true? Maybe they weren’t a senior? Or maybe they didn’t go to your high school? You could write this as a logic statement like below using negation (!) and the and (&&) operator since both parts have to be true for the whole statement to be true. (Thank you to Kevin Saxton from Kent School, CT for this example.) !(a && b) a = "senior" b = "at our high school" // This means it is not true that (a) it is a senior // and (b) someone at our high school. In this lesson, you will learn about De Morgan’s Laws which simplify statements like this. We know that !(a senior at our high school) could mean !(a senior) or !(at our high school). Let’s learn more about De Morgan’s Laws. 3.6.1. De Morgan’s Laws¶De Morgan’s Laws were developed by Augustus De Morgan in the 1800s. They show how to simplify the negation of a
complex boolean expression, which is when there are multiple expressions joined by an AND (&&) or OR (||), such as Figure 1: De Morgan’s Laws to simplify complex expressions¶ In Java, De Morgan’s Laws are written with the following operators:
Going back to our example above, !(a senior && at our high school) is equivalent to !(a senior) or !(at our high school) using De Morgan’s Laws: !(a && b) is equivalent to !a || !b a = "senior" b = "at our high school" You can also simplify negated boolean expressions that have relational operators like <, >, ==. You can move the negation inside the parentheses by flipping the relational operator to its opposite sign. For example, not (c equals d) is the same as saying c does not equal d. An easy way to remember this is To move the NOT, flip the sign. Notice that == becomes !=, but < becomes >=, > becomes <=, <= becomes >, and >= becomes < where the sign is flipped and an equal sign may also be added or removed.
3.6.2. Truth Tables¶Although you do not have to memorize De Morgan’s Laws for the CS A Exam, you should be able to show that two boolean expressions are equivalent. One way to do this is by using truth tables. For example, we can show that !(a && b) is equivalent to !a || !b by constructing the truth table below and seeing that they give identical results for the 2 expressions (the last 2 columns in the table below are identical!).
3.6.3. Simplifying Boolean Expressions¶Often, you can simplify boolean expressions to create equivalent expressions. For example, applying De Morgan’s Laws to Figure 2: An example boolean expression simplified¶ For what values of x and y will the code below print true? Try out different values of x and y to check your answer. 3-6-2: What is printed when the following code executes and x equals 4 and y equals 3? int x = 4, y = 3; if (!(x < 3 || y > 2)) { System.out.println("first case"); } else { System.out.println("second case"); } 3-6-3: What is printed when the following code executes and x equals 4 and y equals 3? int x = 4, y = 3; if (!(x < 3 && y > 2)) { System.out.println("first case"); } else { System.out.println("second case"); } 3.6.4. Programming Challenge : Truth Tables POGIL¶We encourage you to do this activity as a POGIL (Process Oriented Guided Inquiry Learning) group activity. POGIL groups are self-managed teams of up to 4 students where everyone has a POGIL role and works together to solve the problems, making sure that everyone in the team participates and learns. Explore the following problems with your group. You may use this worksheet to complete your truth tables. Assume that x is an integer value, for example -1, 0, or 1.
Are these 3 boolean expressions equivalent? 1. !(x == 0 || x >= 1) , 2. !(x == 0) && !(x >= 1) , 3. (x != 0) && (x < 1)
3-6-5: Which of the following is the same as the code below?
3-6-6: Which of the following is the same as the code below?
3-6-7: Which of the following is the same as the code below?
3-6-8: Which of the following is the same as the code below? 3.6.5. Summary¶
3.6.6. AP Practice¶
3-6-9: Which of the following best describes the value of the Boolean expression: a && !(b || a) You have attempted of activities on this page Which of the following correctly describes the relationship between the transformed number and the original number?Which of the following correctly describes the relationship between the transformed number and the original number? The transformed number is 8 times the value of the original number.
Which of the following best explains the ability to solve problems algorithmically?Which of the following best explains the ability to solve problems algorithmically? Any problem can be solved algorithmically, though some algorithmic solutions may require humans to validate the results.
Which of the following is a true statement about the student's use of the computer scientist's search procedure?Which of the following is a true statement about the student's use of the computer scientist's search procedure? The student is reusing the computer scientist's procedural abstraction by knowing what the procedure does without knowing how it does it.
Which of the following best explains how messages are typically transmitted over the Internet?10. Which of the following best explains how messages are typically transmitted over the Internet? The message is broken into packets that are transmitted in a specified order. Each packet must be received in the order it was sent for the message to be correctly reassembled by the recipient's device.
|