Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)"— Presentation transcript:

1 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

2 (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)

3 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

4 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

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

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

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

8 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

9 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

10 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…

11 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.


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

Similar presentations


Ads by Google