Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 AP Java 10/1/2015

2 public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for (int count = 1; count < 10; count++) for (int count = 1; count < 10; count++) { roll = (int)(6*Math.random())+1; roll = (int)(6*Math.random())+1; System.out.print(roll); System.out.print(roll); } } // end main } // end main} What do you think this program does? Type it in BlueJ and test it.

3 Math.random(); Returns a double value in the range 0<=Math.random() < 1. Returns a double value in the range 0<=Math.random() < 1. You can change its type from double to int by type casting. You can change its type from double to int by type casting. roll = (int)(6*Math.random())+1; roll = (int)(6*Math.random())+1;

4 Random Practice Describe the range of random values generated from the following. Describe the range of random values generated from the following. int one = (int)(6*Math.random()) + 1; int one = (int)(6*Math.random()) + 1; int roll = (int)(10*Math.random()) + 6; int roll = (int)(10*Math.random()) + 6; int roll = (int)(20*Math.random()) - 3; int roll = (int)(20*Math.random()) - 3; int roll = 2* ((int)(6*Math.random())) + 10; int roll = 2* ((int)(6*Math.random())) + 10; int roll = 2* ((int)(6*Math.random())) + 1; int roll = 2* ((int)(6*Math.random())) + 1; double roll = Math.random() + 1; double roll = Math.random() + 1; double roll = 6*Math.random() + 1; double roll = 6*Math.random() + 1;

5 More Random Practice Write the code needed to generate the following ranges of random numbers. Write the code needed to generate the following ranges of random numbers. 2 to 7 2 to 7 -5 to 5 -5 to 5 50 to 70 50 to 70 10 to 26 even 10 to 26 even A pair of 20 sided dice A pair of 20 sided dice Double values from 1 to 5 Double values from 1 to 5 Double values from 4 to 10 Double values from 4 to 10 Double values from -5 to 5 Double values from -5 to 5

6 Program options Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. 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. 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. Widget walk. Put a widget (any graphic, circle, dot,…) on the screen. Have it move randomly for 100 steps. Use Swing to create a GUI for this program. Push: Look up JOptionPane in Java Docs and incorporate a method not described in today’s presentation.

7 Java Swing 101 Basic Input and Output using Java Swing.

8 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[] ) public static void main( String args[] ) { // display a dialog with the message // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } // end main } // end main } // end class Dialog1 Look at help files for JOptionPane

9 Input with swing // Fig. 3.18: NameDialog.java // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public static void main( String args[] ) public static void main( String args[] ) { // prompt user to enter name // prompt user to enter name String name = String name = JOptionPane.showInputDialog( "What is your name?" ); JOptionPane.showInputDialog( "What is your name?" ); // create the message // create the message String message = String message = String.format( "Welcome, %s, to Java Programming!", name ); String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); JOptionPane.showMessageDialog( null, message ); } // end main } // end main } // end class NameDialog

10 Swing input stuff It only enters a string, not a double or int. It only enters a string, not a double or int. So to enter numbers you will need to convert them to integers or doubles So to enter numbers you will need to convert them to integers or doubles To convert a String into an integer. To convert a String into an integer. int answer=Integer.parseInt(name); int answer=Integer.parseInt(name); To convert to a double: To convert to a double: double answer=Double.parseDouble(name); double answer=Double.parseDouble(name);

11 import javax.swing.JOptionPane; public class NameDialog { public static void main( String [] args) public static void main( String [] args) { // prompt user to enter name String name = String name = JOptionPane.showInputDialog( "What is your name?" ); JOptionPane.showInputDialog( "What is your name?" ); //Prompt for thei age //Prompt for thei age String ageString = String ageString = JOptionPane.showInputDialog( "What is your age?" ); JOptionPane.showInputDialog( "What is your age?" ); int age = Integer.parseInt(ageString); int age = Integer.parseInt(ageString); // create the message // create the message String message = String message = String.format( "Welcome, %s, to Java Programming! \n %d", name, age ); String.format( "Welcome, %s, to Java Programming! \n %d", name, age ); // display the message to welcome the user by name // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); JOptionPane.showMessageDialog( null, message ); } // end main } // end main } // end class NameDialog String For int, Use %f for double

12 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.


Download ppt "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."

Similar presentations


Ads by Google