Recap of Class 2
- Two categories of data.
- Primitive data such as integers, floating point numbers, characters etc ..
- Objects such as Strings, Dialog Boxes, Frames, Customers, etc ..
- The data type of an object is some class
- When you install Java you get a bunch of classes you can use in your programs.
- To get input into our programs we use the class
JOptionPane
- To use the class
JOptionPane
you have to know the package where it can be found then import it.import javax.swing.JOptionPane;
- Import statements must be placed before any class definitions in the file.
- Class JOptionPane provides some methods used to pop up dialog boxes.
- To get input into your program use the method
showInputDialog
from theJOptionPane
class. - To prompt the user to enter their name, use the command
- The above command will produce a window similar to this
- Method
showInputDialog
returns back into your program a string and always a string. - Can assign this string to a variable.
- If a number is entered for computing, must convert the string that contains the digits into a number.
- If the number is an integer, must parse it to an integer. If its a double must parse it to a double.
Integer
andDouble
are class that come with java and are part of packagejava.lang
- Classes part of package
java.lang
are automatically imported. - Use any text editor to enter your java codes. I will use textpad in the class.
JOptionPane.showInputDialog("Enter your name");
String name = JOptionPane.showInputDialog("Enter your name");
String inputPrincipal = JOptionPane.showInputDialog("Enter your name"); int principal = Integer.parseInt(inputPrincipal); String inputRate = JOptionPane.showInputDialog("Enter the rate"); double rate = Double.parseDouble(inputRate);