Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Java 10/4/2016.

Similar presentations


Presentation on theme: "AP Java 10/4/2016."— Presentation transcript:

1 AP Java 10/4/2016

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

3 string a = "infinity"; 1. ) int b = a. length() - 4;. b = 2
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 = ) int k = a.indexOf("ini") * c; k =

4 Basic Input and Output using Java Swing.

5 GUI 101 with javax.swing // Fig. 3.17: Dialog1.java
Look at java docs help files for JOptionPane to find out additional methods. // 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

6 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[] ) // 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 Input with swing

7 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);

8 String For int, 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.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

9 Swing Program Tree height calculator: Resisting Change
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.


Download ppt "AP Java 10/4/2016."

Similar presentations


Ads by Google