Truth tables: Ways to organize results of Boolean expressions.

Slides:



Advertisements
Similar presentations
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Lecture 2: Static Methods, if statements, homework uploader.
Java Programming: From the Ground Up
Chapter 5 Loops.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Java Review if Online Time For loop Quiz on Thursday.
Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
AP Java Java’s version of Repeat until.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
CompSci 230 S Programming Techniques
CSC111 Quick Revision.
Lecture 11.
AP Java 10/4/2016.
Chapter 5: Control Structures II
Java for Beginners.
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Java Methods Making Subprograms.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Java Fix a program that has if Online time for Monday’s Program
Java for Beginners University Greenwich Computing At School DASCO
The for-loop and Nested loops
Java Variables, Types, and Math Getting Started
Java Methods Making Subprograms.
int [] scores = new int [10];
Truth tables: Ways to organize results of Boolean expressions.
AP Java Review If else.
Control Statements Loops.
AP Java 10/4/2016.
Dry run Fix Random Numbers
Java Fix a program that has if Online time for Monday’s Program
Java Methods Making Subprograms.
int [] scores = new int [10];
Truth tables: Ways to organize results of Boolean expressions.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Computer Science 1 Online time for Graphics Program Random review
Computer Science 1 while
Control Statements Loops.
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Building Java Programs
Building Java Programs
Computer Science I: Get out your notes.
Conditionals and Loops
Random Numbers while loop
Computer Science 1 while
CSS161: Fundamentals of Computing
How do you do the following?
Selection Sort Insertion Sort if time
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Presentation transcript:

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

public static void main( String [] args) int roll; What do you think this program does? Type it in BlueJ and test it. public class Rolling { public static void main( String [] args) int roll; for (int count = 1; count < 10; count++) roll = (int)(6*Math.random())+1; System.out.print(roll); } } // end main

Math.random(); Returns a double value in the range 0<=Math.random() < 1. You can change its type from double to int by type casting. roll = (int)(6*Math.random())+1;

Random Practice Describe the range of random values generated from the following. int one = (int)(6*Math.random()) + 1; int roll = (int)(10*Math.random()) + 6; int roll = (int)(20*Math.random()) - 3; int roll = 2* ((int)(6*Math.random())) + 10; int roll = 2* ((int)(6*Math.random())) + 1; double roll = Math.random() + 1; double roll = 6*Math.random() + 1;

More Random Practice Write the code needed to generate the following ranges of random numbers. 2 to 7 -5 to 5 50 to 70 10 to 26 even A pair of 20 sided dice Double values from 1 to 5 Double values from 4 to 10 Double values from -5 to 5

Programs from Tuesday Random Program options 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 options Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. Roll a pair of 6 sided dice 100 times. Find out which occurs most often, 3 or 11. Show how often each of these rolls occur 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.

More Truth: ! (A && B), (!A) || (!B) 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." A B !A !B (!A)||(!B) T F DeMorgan’s Law Complete these tables in your notes.

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 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." Add your answers to the online text to your notes.

Truth Tables Reinforcement Use the button on the class website for Truth Table and DeMorgan’s Law Answer the questions When finished put a summary of De Morgan’s law in your notes. http://chortle.ccsu.edu/java5/Notes/chap40B/ch40B_1.html

while Learning Objectives Be able to write a program that uses the while loop.

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.

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" );

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);

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.

Random Practice Describe the range of random values generated from the following. int one = (int)(10*Math.random()) + 1; int roll = (int)(3*Math.random()) + 6; int roll = (int)(22*Math.random()) - 3; int roll = 2* ((int)(5*Math.random())) + 2; int roll = 2* ((int)(8*Math.random())) - 1; double roll = 100*Math.random() + 1;

More Random Practice Write the code needed to generate the following ranges of random numbers. 6 to 12 -5 to 15 40 to 70 10 to 16 even A pair of 6 sided dice Double values from 1 to <5 Double values from 4 to <10 Double values from -5 to <5

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