Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Computer Programming Lab(7).
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CSCI 160 Midterm Review Rasanjalee DM.
Building Java Programs
Computer Programming Lab 8.
Helper Methods ITP © Ron Poet Lecture 11.
Building Java Programs
Building Java Programs Chapter 5
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Computer Programming Lab(5).
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
Mixing integer and floating point numbers in an arithmetic operation.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Random UNPREDICTABLE NUMBERS. A FEW APPLICATIONS THAT USE RANDOM…. Predictions for life expectance used in insurance Business simulations Games (Give.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers; Type boolean reading: , 5.6.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
AP Java Java’s version of Repeat until.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
TemperatureConversion
Project 1.
Building Java Programs
Building Java Programs Chapter 5
Exercise Java programming
Java Course Review.
Maha AlSaif Maryam AlQattan
Building Java Programs
The Random Class and its Methods
Writing Methods.
Building Java Programs
Introduction to Classes and Methods
Java Lesson 36 Mr. Kalmes.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Building Java Programs
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Lecture 22: Number Systems
Presentation transcript:

Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following line in the top of the source file to import Random class import java.util.Random;

Random (2) Create an object of Random class Random rand = new Random(); Random class contains following methods nextInt( int num ); This method randomly returns one of integers between 0 to num-1 nextDouble( ); This method randomly returns one of doubles between 0.0 to 1.0

Random (3) Example import java.util.Random; public class ExampleRandom { public static void main (String[] args) { Random rand = new Random( ); System.out.println( r.nextInt( 10 ) ); System.out.println( r.nextDouble( ) ); } What is the output from this program? What if run again?

Exercise Let’s create a game –Guessing Number Game ( download GuessNumberGame.java ) A player will guess the number that Java randomly generates –Java randomly generates one number (let’s call it ‘answer’) –If a player guesses the number which is smaller or larger than the answer, then print out the message to instruct the player –If a player correctly guess the number, then print out how many times the player guesses.