Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | shanon-warren |
View: | 217 times |
Download: | 0 times |
truth and whileToday
15 Minutes online time to finish the random/Swing programs.
Truth tables: Ways to organize results of Boolean expressions.
Note Taking: DeMorgan’s law using online text
While
Code
Programs from Tuesday
• Tree height calculator:– Input the distance you are from a
tree and the angle you look up to the top of the tree.
– Output: The height of the tree.– Calculation: tree height = the
height of your eyes + (Distance from the tree) * tan(angle)
– Look up the Math.tan() method to determine if the angle is in degrees or radians.
– Push: Research to find out how to use this information to estimate the number of ‘board-foot’ volume of the standing tree.
• Random Program optionsRandom Program options• Flip a coin 100 times, show the Flip a coin 100 times, show the
total number of heads and tails total number of heads and tails AND which occurred the most AND which occurred the most often.often.
• Roll a pair of 6 sided dice 100 Roll a pair of 6 sided dice 100 times. Find out which occurs times. Find out which occurs most often, 3 or 11. Show how most often, 3 or 11. Show how often each of these rolls occur often each of these rolls occur and which occurs most often.and which occurs most often.
Learning Objectives
• Be able to use a truth table to evaluate boolean expressions.
• Be able to use the while loop in Java.
Truth Tables: A truth table is another way to show the evaluation of a boolean expression.
A B A && B
T T
T F
F T
F F
A B A || B
T T
T F
F T
F F
Complete these tables in your notes.
More Truth: ! (A && B), (!A) || (!B)A B (A&&B
)!(A&&B)
T T
T F
F T
F F A B !A !B (!A)||(!B)
T T
T F
F T
F F
"It is not the case that Tom is rich and famous." is true if and only if "Tom is not rich or he is not famous."
DeMorgan’s Law
Complete these tables in your notes.
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 he 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 he is not famous."
Introduction to Computer Science using Java
Add your answers to the online text to your notes.
More on Truth Tables
Read through Chapter 40B and record your answers in your notes, then
complete the reviewhttp://chortle.ccsu.edu/java5/Notes/chap40B/ch40B_1.html
Example of notes:
1) False 2) …
whileLearning Objectives
• Be able to write a program that uses the while loop.
While loopWhile loop When to use itWhen to use it Repeating something an unknown number of times, but Repeating something an unknown number of times, but
might not occurmight not occur It’s a sentinel-controlled loop. It’s a sentinel-controlled loop.
SemanticsSemantics Get VariableGet Variable while (variable !=flag)while (variable !=flag)
Squiggly lineSquiggly line Get VariableGet Variable
endend SyntaxSyntax
while (condition)while (condition) {{
Commands repeatedCommands repeated }}
SEMANTICS OF A WHILE LOOP!
Put it in your notes.
ExampleExample// Example of a while loop // Example of a while loop class LoopExample class LoopExample { {
public static void main (String[] args ) public static void main (String[] args ) { {
int count = 1; // start count out at one int count = 1; // start count out at one while ( count <= 3 ) // loop while count is <= 3 while ( count <= 3 ) // loop while count is <= 3 { {
System.out.println( "count is:" + count ); System.out.println( "count is:" + count ); count = count + 1; // add one to count count = count + 1; // add one to count
} } System.out.println( "Done with the loop" ); System.out.println( "Done with the loop" );
} } } }
Read the program to
see how the syntax of the while looks.
Example getting input from the UserExample getting input from the Userimport java.util.Scanner;import java.util.Scanner;public class LoopTotalpublic class LoopTotal{{ public static void main(String [] args)public static void main(String [] args) {{ int total = 0, score, count = 0;int total = 0, score, count = 0; Scanner input = new Scanner(System.in);Scanner input = new Scanner(System.in); System.out.println("Enter a score, -1 to quit");System.out.println("Enter a score, -1 to quit"); score = input.nextInt();score = input.nextInt(); while (score != -1)while (score != -1) {{ total += score;total += score; count++;count++; System.out.println("Enter a score, -1 to quit");System.out.println("Enter a score, -1 to quit"); score = input.nextInt();score = input.nextInt(); }} System.out.println("The total score = " + total);System.out.println("The total score = " + total); }}}}
Program Options1. 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.
Use swing to create a windows application for one
of the options.