Programming with Java standard classes

Download Report

Transcript Programming with Java standard classes

Programming with Java standard
classes
Java API
• Application Programming Interface
• Provides hundreds of standard classes that can
be incorporated into your programs –
readymade solutions for a variety of tasks
• Detailed documentation of Java standard
classes freely available on web:
http://java.sun.com/j2se/1.5.0/docs/api/index.html
JOptionPane: an I/O class
• Output: display something on an output device
(i.e. the computer’s monitor)
– display results of computation
– communicate with the user
• Most Java programs employ a graphical user
interface (GUI) with two types of windows:
– frame window: the type of window created by our first
example program
– dialog window: allows communication with user; most
commonly used to prompt for & receive input, but
we’ll begin by looking at just output
JOptionPane output example
JOptionPane.showMessageDialog(null, “Look at me!”);
• Displays a small window in the center of
the screen containing the words in the string
literal and a button labeled “OK”
• This is an example of a message that calls a
class method of the JOptionPane class – we
are not making a request to an object, but
rather to the class itself
JOptionPane.showMessageDialog
example
• Arguments to the method:
– the first argument, null, indicates that there is no existing
frame object upon which this dialog should appear; if we
want the dialog to appear in an existing frame window, we
would pass the name of the frame object as the first
argument, instead of null – for example:
JFrame myWindow;
… // review: what goes here?
JOptionPane.showMessageDialog (myWindow, “It’s my window”);
– the second argument, a string literal, indicates the text we
want to display in the window
Example program
// Sample program #2: displaying messages
// using JOptionPane.showMessageDialog
import javax.swing.*;
class Example2 {
public static void main (String [ ] args) {
JFrame myWindow;
myWindow = new JFrame();
myWindow.setSize (300, 200);
myWindow.setVisible(true);
JOptionPane.showMessageDialog(myWindow, “It’s my window”);
JOptionPane.showMessageDialog(null, “and I’ll cry \n if I want to”);
}
}
Some notes on the example
• Displaying multiple lines of text: the special
character ‘\n’ represents the control
character you get when you press the Enter
key on the keyboard (n = new line)
• Later on, we’ll introduce another
JOptionPane class method that allows us to
take keyboard input from the user
The String class
• We have already seen several instances of
string literal values – a.k.a. string constants
– that is, sets of characters enclosed within
double quotation marks
• The Java API contains the String class,
which allows us to manipulate string data
through the use of String objects
The String class
• We can declare and create a String object just like we do other types of
objects, for example:
String bigRiver = new String (“Mississippi”);
• In fact, the String class is an exception to the “new” rule – you can
create a String object without it, as in this example:
String bigRiver = “Amazon”;
• But you may find it less confusing to stick to the form you know for
other classes, like the first example, above
• Bear in mind, though, that it is quite common to assign a new string
literal to a String object without using the new operator, as in the
following lines of code:
String bigRiver = new String (“Amazon”);
… // some other stuff happens
bigRiver = “Mississippi”;
Operations on String objects
• The Java API defines many operations on String
objects: we will review just a few of them here:
• substring: takes 2 arguments representing the
beginning and ending positions of a String within
a String – returns the resulting substring
– Note that the first position in a String in Java is
designated position 0 – so a 4-letter word would start at
0 and end at 3
– An error will result if you attempt to call the method
using positions that don’t exist within the String object
Examples using substring
• If the String object bigRiver contains the
literal value “Mississippi”:
bigRiver.substring(6, 8); // returns “sip”
bigRiver.substring (0, 2); // returns “Mis”
bigRiver.substring (4, 5); // returns “is”
Examples using substring
• The method calls in the example would return the literal
values indicated, but they would neither be stored nor
displayed anywhere – therefore, these method calls
would usually occur within the context of an
assignment statement or another method call; for
example:
String sub = new String (bigRiver.substring(6, 8));
// returns “sipp” and assigns it to new object sub
JOptionPane.showMessageDialog(bigRiver.substring (4, 5));
// displays “is” in a small window with an OK button
More String methods
• The length method returns the length (in
characters) of the String object; for example, if
String bigRiver contains the value “Mississippi”
then
bigRiver.length() // returns 11
• The indexOf method returns a number indicating
the position of the beginning of the first
occurrence of the substring specified in the
message’s argument; examples:
bigRiver.indexOf(“Miss”)
bigRiver.indexOf(“is”)
bigRiver.indexOf(“sis”)
// returns 0
// returns 1
// returns 3
String concatenation
• String concatenation: an operation that makes one
String from two, using the ‘+’ operator; for example:
String phrase, word;
phrase = new String (“old man river”);
word = new String (“ that ”);
phrase = phrase + word + phrase;
// phrase now contains “old man river that old man river”
Program example
import javax.swing.*;
class Ch2StringProcessing {
public static void main( String[] args ) {
String fullName, firstName, lastName, space;
fullName = new String("Decafe Latte");
space = new String(" ");
firstName = fullName.substring(0, fullName.indexOf(space));
lastName = fullName.substring(fullName.indexOf(space) + 1,
fullName.length());
JOptionPane.showMessageDialog(null, "Full Name: " + fullName);
JOptionPane.showMessageDialog(null, "First: " + firstName);
JOptionPane.showMessageDialog(null, "Last: " + lastName);
JOptionPane.showMessageDialog(null,
"Your last name has "+lastName.length( )+" characters.");
}
}
Reading input
• In order to receive input data from a user,
we need two things:
– a mechanism by which to read the data
– a place to store the data
• We can use another method,
showInputDialog, of the JOptionPane class
for the first part; we can use a String object
for the second part.
Using showInputDialog for
reading input
• The syntax for showInputDialog is almost identical to
the previous JOptionPane method we looked at,
showMessageDialog. You may recall from a previous
example program the following lines of code:
JOptionPane.showMessageDialog(myWindow, “It’s my window”);
JOptionPane.showMessageDialog(null, “and I’ll cry \n if I want to”);
• In general, the syntax for both showMessageDialog and
showInputDialog is:
JOptionPane.methodName (WindowObject, MessageObject);
I/O dialog windows
• We know from the examples that the first
argument, the WindowObject, can either be a
JFrame object we have declared and initialized or
null; we also know that the second argument can
be a String literal value.
• The difference between the two methods is that
showMessageDialog merely displays a window
containing the specified message, but
showInputDialog method displays the message as
a prompt, followed by a space for the user to
enter input.
Example: showInputDialog
The following code fragment produces a dialog window like
the one shown below:
String daddy;
daddy = JOptionPane.showInputDialog (null,
“Who’s your daddy?”);