Random Numbers while loop

Slides:



Advertisements
Similar presentations
Java Syntax. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); }
Advertisements

10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
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
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
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:
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Java Review if Online Time For loop Quiz on Thursday.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
int [] scores = new int [10];
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
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.
User Input ICS2O.
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
AP Java 10/4/2016.
Repetition-Counter control Loop
Repetition.
TK1114 Computer Programming
Java Methods Making Subprograms.
Building Java Programs
Building Java Programs
Building Java Programs
Review Finish program Graphics Programming
Java Fix a program that has if Online time for Monday’s Program
The for-loop and Nested loops
Java Methods Making Subprograms.
int [] scores = new int [10];
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Sorts on the AP Exam Insertion Sort.
Control Statements Loops.
AP Java 10/4/2016.
Dry run Fix Random Numbers
Truth tables: Ways to organize results of Boolean expressions.
Java Fix a program that has if Online time for Monday’s Program
Java Methods Making Subprograms.
int [] scores = new int [10];
Introduction to Java Brief history of Java Sample Java Program
Truth tables: Ways to organize results of Boolean expressions.
Computer Science 1 Online time for Graphics Program Random review
Computer Science 1 while
Building Java Programs
Control Statements Loops.
Building Java Programs
Computer Science I: Get out your notes.
Array Review Selection Sort
int [] scores = new int [10];
Computer Science 1 while
CSS161: Fundamentals of Computing
How do you do the following?
Presentation transcript:

Random Numbers while loop AP CS Random Numbers while loop

Learning Targets Be able to read and write code needed for creating and using random numbers Be able to use while loop

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(); Order of Operations () (int) Type cast * / % + - 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; What is wrong with … 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

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. Widget walk. Put a widget (any graphic, circle, dot,…) on the screen. Have it move randomly for 100 steps. Push: Look up JOptionPane in Java Docs and incorporate a method not described in today’s presentation.

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.