Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
Advertisements

PAC Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Computers and Programming Lecture 7:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Computer Science I Arrays Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1 Lecture 3 Part 1 Functions with math and randomness.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods.
1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
Review for Nested loops & Math class methods & User defined methods.
Creating and Using Class Methods. Definition Class Object.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Introduction to Methods
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Presentation transcript:

Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University

Road Map Math.random Reading: –Liang 4: Chapter 4: Section –Liang 5: Chapter 4: Section 4.8.5

review What is a method? What information can you learn about a method from its header? What does it mean to invoke a method? What is the difference between a formal parameter and an actual parameter? Why don’t we have to import the Math class? What is abstraction in computer science?

random numbers Often we want our programs to generate random numbers. –games of chance –testing without user interaction java.lang.Math provides a method which can be used to generate random numbers

 2003 Prentice Hall, Inc. All rights reserved. 5 Random-Number Generation Java random-number generators –Math.random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. –What if we want to generate random integers?

 2003 Prentice Hall, Inc. All rights reserved. 6 Random-Number Generation –( int ) ( Math.random() * 6 ) Produces integers from 0 - 5

 2003 Prentice Hall, Inc. All rights reserved. Outline 7 RandomIntegers. java Line 16 Produce integers in range 1-6 Line 16 Math.random returns double s. We cast the double as an int 1 // Fig. 6.7: RandomIntegers.java 2 // Shifted, scaled random integers. 3 import javax.swing.JOptionPane; 4 5 public class RandomIntegers { 6 7 public static void main( String args[] ) 8 { 9 int value; 10 String output = ""; // loop 20 times 13 for ( int counter = 1; counter <= 20; counter++ ) { // pick random integer between 1 and 6 16 value = 1 + ( int ) ( Math.random() * 6 ); output += value + " "; // append value to output // if counter divisible by 5, append newline to String output 21 if ( counter % 5 == 0 ) 22 output += "\n"; } // end for 25 Produce integers in range 1-6 Math.random returns double s. We cast the double as an int

 2003 Prentice Hall, Inc. All rights reserved. Outline 8 RandomIntegers. java 26 JOptionPane.showMessageDialog( null, output, 27 "20 Random Numbers from 1 to 6", 28 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class RandomIntegers

 2003 Prentice Hall, Inc. All rights reserved. Outline 9 RollDie.java Line 14 Produce integers in range 1-6 Lines Increment appropriate frequency counter, depending on randomly generated number 1 // Fig. 6.8: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency1 = 0, frequency2 = 0, frequency3 = 0, 10 frequency4 = 0, frequency5 = 0, frequency6 = 0, face; // summarize results 13 for ( int roll = 1; roll <= 6000; roll++ ) { 14 face = 1 + ( int ) ( Math.random() * 6 ); // determine roll value and increment appropriate counter 17 switch ( face ) { case 1: 20 ++frequency1; 21 break; case 2: 24 ++frequency2; 25 break; case 3: 28 ++frequency3; 29 break; 30 Produce integers in range 1-6 Increment appropriate frequency counter, depending on randomly generated number

 2003 Prentice Hall, Inc. All rights reserved. Outline 10 RollDie.java 31 case 4: 32 ++frequency4; 33 break; case 5: 36 ++frequency5; 37 break; case 6: 40 ++frequency6; 41 break; } // end switch } // end for JTextArea outputArea = new JTextArea(); outputArea.setText( "Face\tFrequency" + "\n1\t" + frequency "\n2\t" + frequency2 + "\n3\t" + frequency "\n4\t" + frequency4 + "\n5\t" + frequency "\n6\t" + frequency6 ); JOptionPane.showMessageDialog( null, outputArea, 55 "Rolling a Die 6000 Times", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class RollDie This is different from the example I showed in the compiler. I left it this way to show you that showMessageDialog() can take other objects besides String for the message. Remember the API. You do not need to know JTextArea for this class!

 2003 Prentice Hall, Inc. All rights reserved. 11