Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)

Slides:



Advertisements
Similar presentations
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Advertisements

1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CS101 Computer Programming I Chapter 4 Extra Examples.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random.
int [] scores = new int [10];
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
ADVANCED EV3 PROGRAMMING LESSON By Seshan Brothers Random Block.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
FOP: While Loops.
Topic 2 Elementary Programming
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
COMP 14 Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
Agenda Warmup Finish 2.4 Assignments
Agenda Warmup AP Exam Review: Litvin A2
Java for Beginners University Greenwich Computing At School DASCO
Programming Mehdi Bukhari.
IF statements.
While Loops in Python.
Type Conversion, Constants, and the String Object
Lecture 07 More Repetition Richard Gesick.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Engineering Innovation Center
Control Statement Examples
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Conditions and Ifs BIS1523 – Lecture 8.
Compound Assignment Operators in C++
Lesson Objectives Aims You should be able to:
int [] scores = new int [10];
Truth tables: Ways to organize results of Boolean expressions.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
By Sanjay and Arvind Seshan
Do While (condition is true) … Loop
Module 4 Loops.
Truth tables: Ways to organize results of Boolean expressions.
int [] scores = new int [10];
Conditional Logic Presentation Name Course Name
Truth tables: Ways to organize results of Boolean expressions.
Welcome to AP Computer Science A!
Welcome to AP Computer Science A!
CS2011 Introduction to Programming I Selections (I)
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Life is Full of Alternatives
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Other types of variables
Agenda Warmup Lesson 2.2 (parameters, etc)
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Random Numbers while loop
Dry Run Fix it Write a program
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
While Loops in Python.
How do you do the following?
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Variables and Constants
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc) Guided Practice (1.9 Assignments) Closure Activity Students will be able to: Understand what a random number is, and why they are useful in computer science Create a random number based on a given minimum and maximum Understand what a Boolean variable is Create Boolean variables and use them to solve challenging problems See how today's lesson fits into the unit and the course as a whole

(int)(Math.random()*(max-min+1) + minimum) Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random integers: (int)(Math.random()*(max-min+1) + minimum)

examples Creates a random integer between… x = (int)(Math.random()*6+1); // 1 to 6 y = (int)(Math.random()*7+50); // 50 to 56 z = (int)(Math.random()*21+100); // 100 to 120 a = (int)(Math.random()*4); // 0 to 4 Demo: RandomNumberDemo

Notice that in the previous examples, we have been type casting the random #s into ints. If you do not cast, then a random # is, by default, a double. x = (Math.random()*3+0.5); // 0.5 to 3.5 y = (Math.random()*4.2+7); // 7.0 to 11.2 z = (Math.random()*9.8); // 0.0 to 9.8

Boolean variables A boolean variable is a variable that can only have 2 possible values: true or false demo: BooleanDemo

To check to see if a Boolean variable is true, the ==true is often used, but not necessary: if (x == true) // is the same as… if (x)

Also, the ! symbol can be used instead of “== false” in this way: if (x == false) // is the same as… if (!x)

Boolean expressions A boolean expression means something very different – it is simply a segment of code that is either true or false, such as: (score > 90) (name.equals(“Peppers”)) (letter == ‘a’) So, essentially any expression you might see after if or while or for is a boolean expression

int a = 9; int b = 7; boolean m = (a < b); This code is completely valid, since (a < b) is a boolean expression, which is either true or false. (In this case, it is false.) What would display here? System.out.println( !m ); // show demo

Assignments 1. ListRandom Use a for loop to create and display 11 random #s between 1 and 40. Then display the following: How many odd #s, how many evens How many #s in the 30s How many 37s The average The highest #, the lowest # How many prime #s (read the following!) do not do this by simply using an if that checks all the primes less than 40. Instead, use a for-loop and a boolean variable to determine if the number is prime. I recommend planning this code before typing it! Run the program and make sure your results are correct.*** see next slide…

2) p. 185 # 15 Think about it – how can you use random numbers to decide the computer’s “choice”? Each round, make sure to display what the computer chose, as well as who won or lost (or if they tied). The user should be able to play as many rounds as they like. p. 185 # 16 Add a betting feature, where the player begins with $500, and can bet money on each turn. They double their bet if 2 numbers match, they quintuple their bet if all 3 numbers match, they lose their bet if no numbers match. Always display the player’s current amount of money. The player cannot keep playing if they’re out of money.