Presentation is loading. Please wait.

Presentation is loading. Please wait.

Truth tables: Ways to organize results of Boolean expressions.

Similar presentations


Presentation on theme: "Truth tables: Ways to organize results of Boolean expressions."— Presentation transcript:

1 Truth tables: Ways to organize results of Boolean expressions.
truth and while Today While Truth tables: Ways to organize results of Boolean expressions. DeMorgan’s Law Code

2 Learning Objectives Be able to use the while loop in Java.
Be able to use a truth table to evaluate boolean expressions.

3 SEMANTICS OF A WHILE LOOP!
When to use it Repeating something an unknown number of times, but might not occur It’s a sentinel-controlled loop. Semantics Get Variable while (variable !=flag) Squiggly line end Syntax while (condition) { Commands repeated } SEMANTICS OF A WHILE LOOP! Put it in your notes.

4 Read the program to see how the syntax of the while looks.
Example Read the program to see how the syntax of the while looks. // Example of a while loop class LoopExample { public static void main (String[] args ) int count = 1; // start count out at one while ( count <= 3 ) // loop while count is <= 3 System.out.println( "count is:" + count ); count = count + 1; // add one to count } System.out.println( "Done with the loop" );

5 Example getting input from the User
import java.util.Scanner; public class LoopTotal { public static void main(String [] args) int total = 0, score, count = 0; Scanner input = new Scanner(System.in); System.out.println("Enter a score, -1 to quit"); score = input.nextInt(); while (score != -1) total += score; count++; } System.out.println("The total score = " + total);

6 What loop should you use for the following?
Finding the highest of 25 test scores. Finding the lowest of an unknown number of test scores. Gathering 100 air qualify sample readings. Now time to take a look at a tool for evaluating conditions.

7 Truth Tables: A truth table is another way to show the evaluation of a boolean expression.
Add columns for conjunctions using the order of operations to help determine what to include Use a variable to represent each condition A B A || B F T A B A && B F T List all possible combinations of the conditions Calculate the result of the conjunction

8 More Truth: ! (A && B), (!A) || (!B)
F T "It is not the case that Tom is rich and famous." is true if and only if "Tom is not rich or Tom is not famous." A B !A !B (!A)||(!B) F T DeMorgan’s Law Complete these tables in your notes.

9 Add your answers to the online text to your notes.
Introduction to Computer Science using Java Truth Tables A truth table is another way to show the evaluation of a boolean expression. DeMorgan !(A && B) = (!A) || (!B) "It is not the case that Tom is rich and famous." is true if and only if "Tom is not rich or Tom is not famous." !( A || B ) = (!A) && (!B) "It is not the case that Tom is rich or famous." is true if and only if "Tom is not rich and Tom is not famous." Add your answers to the online text to your notes.

10 Truth Tables Reinforcement
Use the button on the class website for Truth Table and DeMorgan’s Law Answer the 18 questions that go with the tutorial.

11 Program Options: Complete one of the following
You can use swing to create a windows application for one of the options. Program Options: Complete one of the following Input: An unknown number of integers. Output: The total , average, high and low values. Input: None Process: Roll a pair of six-sided dice and count how many rolls it takes for you to roll three sevens in a row. Output: Each roll and the count for the number of rolls it takes to roll three sevens in a row. Input: An unknown number of integers representing light sensor readings. Output: A running average of the three most recent readings. Determine how you will handle the first two readings and include your approach in your header notes. Push: Modify the program so the user can enter how many readings to consider when calculating the average.

12 BlackJack: Complete one or more of the following
Level I Write a program that allows a human user to play "blackjack" against a dealer. Pick two values from 1-10 for the player. These are the player's "cards". Pick two more values from 1-10 for the dealer. Whoever has the highest total is the winner. Ties go to the dealer There is no betting, no busting, and no hitting, but the user can ‘Play again’ Level II Don't worry about suits or face cards; "cards" will have values from 2-11, and all values are equally likely (that is, unlike a real blackjack game, there's no greater chance of drawing a card with value 10). Draw two cards for the player and display them. Draw two cards for the "dealer" and display one of them, keeping the other one hidden. Allow the player to "hit" as many times as he would like. If the player "busts" (gets a total over 21), the dealer automatically wins. Allow the dealer to hit as many times as he would like. Dealer should probably hit on sixteen or lower. If the dealer busts, the player automatically wins. Assuming no one has busted, the player with the highest total wins. Dealer wins all tie. Level III Use realistic card values, with suits and faces from ace to king. Incorporate wagering. Display some sort of graphical cards. Anything else interesting you can think of. BlackJack: Complete one or more of the following


Download ppt "Truth tables: Ways to organize results of Boolean expressions."

Similar presentations


Ads by Google