Transcript File

AP Java
10/1/2015
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.
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 help files for
JOptionPane
// 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 thei age
String ageString =
JOptionPane.showInputDialog( "What is your age?" );
int age = Integer.parseInt(ageString);
// create the message
String message =
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
String
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 ‘boardfoot’ volume of the standing tree.