Download presentation
Presentation is loading. Please wait.
Published byΟκυροη Εὔα Γιάνναρης Modified over 6 years ago
1
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
2
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
3
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;
4
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;
5
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
6
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.
7
Learning Objectives Be able to use a truth table to evaluate boolean expressions. Be able to use the while loop in Java.
8
Truth Tables: A truth table is another way to show the evaluation of a boolean expression.
9
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.
10
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.
11
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.
12
while Learning Objectives
Be able to write a program that uses the while loop.
13
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.
14
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" );
15
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);
16
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.
17
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;
18
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
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.