truth and while Today While Truth tables Ways
truth and while Today While Truth tables: Ways to organize results of Boolean expressions. De. Morgan’s Law Code
Learning Objectives • Be able to use the while loop in Java. • Be able to use a truth table to evaluate boolean expressions.
n When to use it n Repeating something an unknown number of times, but might not occur n It’s n While loop a sentinel-controlled loop. Semantics n n Get Variable while (variable !=flag) Squiggly line n Get Variable n end Syntax n n while (condition) { n n } Commands repeated SEMANTICS OF A WHILE LOOP! Put it in your notes.
Example // Example of a while loop class Loop. Example { Read the program to see how the syntax of the while looks. 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" ); } }
Example getting input from the User import java. util. Scanner; public class Loop. Total { 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. next. Int(); while (score != -1) { total += score; count++; System. out. println("Enter a score, -1 to quit"); score = input. next. Int(); } System. out. println("The total score = " + total); } }
What loop should you use for the following? n n 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.
Truth Tables: A truth table is another way to show the evaluation of a boolean expression. Add columns for conjunctions using the Use a variable to represent each condition List all possible combinations of the conditions order of operations to help determine what to include A F F T T B F T A && B Calculate the result of the conjunction A B A || B F F F T T T
More Truth: ! (A && B), (!A) || (!B) A F F T T B F T (A&&B) !(A&&B) De. Morgan’s Law Complete these tables in your notes. A F F T T 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)
Truth Introduction to Computer Science using Java • Tables – A truth table is another way to show the evaluation of a boolean expression. • De. Morgan Add your answers to the online text to your notes. – !(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. "
Truth Tables Reinforcement Use the button on the class website for Truth Table and De. Morgan’s Law Answer the 18 questions that go with the tutorial. http: //chortle. ccsu. edu/java 5/Notes/chap 40 B/ch 40 B_1. html
Program Options: Complete one of the following You can use swing to create a windows application for one of the options. 1. Input: An unknown number of integers. – Output: The total , average, high and low values. 2. 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. 3. 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.
• – – – • 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 – – – – • Black. Jack: Complete Write a program that allows a human user to play "blackjack" against a dealer. one or more of the 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. following Level I Don't worry about suits or face cards; "cards" will have values from 211, 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.
- Slides: 12