Java Random Number Generator

Download Report

Transcript Java Random Number Generator

AP Java
10/4/2016
Learning Objectives
Be able to use Java’s Swing class to make
windows applications
 Be able to convert from String to int and
double.
 Be able to use Math.random() and type
casting to generate ranges of random
numbers.

string a = "infinity";
1.) int b = a.length() - 4;
b=
2.) int c = a.indexOf("ty") + b;
c=
3.) boolean d = !a.equals("infinity");
d=
4.) String e = a.substring(4);
e=
5.) String f = a.substring(1, 8);
f=
6.) boolean g = a.equals("infInity");
g=
7.) int h = a.indexOf("z");
h=
8.) int i = a.length() - b/g;
i=
9.) int j = a.substring(2);
j=
10.) int k = a.indexOf("ini") * c;
k=
Java Swing 101
Basic Input and Output using Java
Swing.
GUI 101 with javax.swing
// Fig. 3.17: Dialog1.java
// Printing multiple lines in dialog box.
import javax.swing.JOptionPane;
public class Dialog1
{
public static void main( String args[] )
{
// display a dialog with the message
JOptionPane.showMessageDialog(
null, "Welcome\nto\nJava" );
} // end main
} // end class Dialog1
Look at java docs help files for
JOptionPane to find out
additional methods.
// Fig. 3.18: NameDialog.java
// Basic input with a dialog box.
import javax.swing.JOptionPane;
Input with swing
public class NameDialog
{
public static void main( String args[] )
{
// prompt user to enter name
String name =
JOptionPane.showInputDialog( "What is your name?" );
// create the message
String message =
String.format( "Welcome, %s, to Java Programming!", name );
// display the message to welcome the user by name
JOptionPane.showMessageDialog( null, message );
} // end main
} // end class NameDialog
Swing input stuff
It only enters a String, not a double or int.
 So to enter numbers you will need to
convert them to integers or doubles
 To convert a String into an integer.


int answer=Integer.parseInt(name);

To convert to a double:

double answer=Double.parseDouble(name);
import javax.swing.JOptionPane;
public class NameDialog
{
public static void main( String [] args)
{
// prompt user to enter name
String name = JOptionPane.showInputDialog( "What is your name?" );
//Prompt for the age
String ageString =JOptionPane.showInputDialog( "What is your age?" );
int age = Integer.parseInt(ageString);
// create the message
String message =
String
String.format( "Welcome, %s, to Java Programming! \n %d", name, age );
// display the message to welcome the user by name
JOptionPane.showMessageDialog( null, message );
} // end main
} // end class NameDialog
For int,
Use %f for double
Swing Program
• Tree height calculator:
• Input the distance you are from a tree and the angle you look
up to the top of the tree.
• Output: The height of the tree.
• Calculation: tree height = the height of your eyes + (Distance
from the tree) * tan(angle)
• Look up the Math.tan() method to determine if the angle is
in degrees or radians.
• Push: Research to find out how to use this information to
estimate the number of ‘board-foot’ volume of the standing
tree. Will you need any other information?
• Resisting Change
• Input: The resistance in Ohms of two resistors wired in
parallel (r1 and r2).
• Output: The net resistance.
• NetResistance = r1*r2 / (r1 + r2)
• Push: Be able to find the resistance given three resisters connected
in parallel.
• Push: Be able to find the resistance of n resistors connected in
parallel. Let the user enter the number of resisters.
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();
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;

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



Use Swing to create a GUI
for this program.
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.