Programming with Java standard classes Java API Application

  • Slides: 19
Download presentation
Programming with Java standard classes

Programming with Java standard classes

Java API • Application Programming Interface • Provides hundreds of standard classes that can

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/j 2 se/1. 5. 0/docs/api/index. html

JOption. Pane: an I/O class • Output: display something on an output device (i.

JOption. Pane: 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

JOption. Pane output example JOption. Pane. show. Message. Dialog(null, “Look at me!”); • Displays

JOption. Pane output example JOption. Pane. show. Message. Dialog(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 JOption. Pane class – we are not making a request to an object, but rather to the class itself

JOption. Pane. show. Message. Dialog example • Arguments to the method: – the first

JOption. Pane. show. Message. Dialog 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 my. Window; … // review: what goes here? JOption. Pane. show. Message. Dialog (my. Window, “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 JOption. Pane. show. Message.

Example program // Sample program #2: displaying messages // using JOption. Pane. show. Message. Dialog import javax. swing. *; class Example 2 { public static void main (String [ ] args) { JFrame my. Window; my. Window = new JFrame(); my. Window. set. Size (300, 200); my. Window. set. Visible(true); JOption. Pane. show. Message. Dialog(my. Window, “It’s my window”); JOption. Pane. show. Message. Dialog(null, “and I’ll cry n if I want to”); } }

Some notes on the example • Displaying multiple lines of text: the special character

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 JOption. Pane 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

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

The String class • We can declare and create a String object just like we do other types of objects, for example: String big. River = 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 big. River = “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 big. River = new String (“Amazon”); … // some other stuff happens big. River = “Mississippi”;

Operations on String objects • The Java API defines many operations on String objects:

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 big. River contains the literal value

Examples using substring • If the String object big. River contains the literal value “Mississippi”: big. River. substring(6, 8); // returns “sip” big. River. substring (0, 2); // returns “Mis” big. River. substring (4, 5); // returns “is”

Examples using substring • The method calls in the example would return the literal

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 (big. River. substring(6, 8)); // returns “sipp” and assigns it to new object sub JOption. Pane. show. Message. Dialog(big. River. 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

More String methods • The length method returns the length (in characters) of the String object; for example, if String big. River contains the value “Mississippi” then big. River. length() // returns 11 • The index. Of method returns a number indicating the position of the beginning of the first occurrence of the substring specified in the message’s argument; examples: big. River. index. Of(“Miss”) big. River. index. Of(“is”) big. River. index. Of(“sis”) // returns 0 // returns 1 // returns 3

String concatenation • String concatenation: an operation that makes one String from two, using

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 Ch 2 String. Processing { public static

Program example import javax. swing. *; class Ch 2 String. Processing { public static void main( String[] args ) { String full. Name, first. Name, last. Name, space; full. Name = new String("Decafe Latte"); space = new String(" "); first. Name = full. Name. substring(0, full. Name. index. Of(space)); last. Name = full. Name. substring(full. Name. index. Of(space) + 1, full. Name. length()); JOption. Pane. show. Message. Dialog(null, "Full Name: " + full. Name); JOption. Pane. show. Message. Dialog(null, "First: " + first. Name); JOption. Pane. show. Message. Dialog(null, "Last: " + last. Name); JOption. Pane. show. Message. Dialog(null, "Your last name has "+last. Name. length( )+" characters. "); } }

Reading input • In order to receive input data from a user, we need

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, show. Input. Dialog, of the JOption. Pane class for the first part; we can use a String object for the second part.

Using show. Input. Dialog for reading input • The syntax for show. Input. Dialog

Using show. Input. Dialog for reading input • The syntax for show. Input. Dialog is almost identical to the previous JOption. Pane method we looked at, show. Message. Dialog. You may recall from a previous example program the following lines of code: JOption. Pane. show. Message. Dialog(my. Window, “It’s my window”); JOption. Pane. show. Message. Dialog(null, “and I’ll cry n if I want to”); • In general, the syntax for both show. Message. Dialog and show. Input. Dialog is: JOption. Pane. method. Name (Window. Object, Message. Object);

I/O dialog windows • We know from the examples that the first argument, the

I/O dialog windows • We know from the examples that the first argument, the Window. Object, 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 show. Message. Dialog merely displays a window containing the specified message, but show. Input. Dialog method displays the message as a prompt, followed by a space for the user to enter input.

Example: show. Input. Dialog The following code fragment produces a dialog window like the

Example: show. Input. Dialog The following code fragment produces a dialog window like the one shown below: String daddy; daddy = JOption. Pane. show. Input. Dialog (null, “Who’s your daddy? ”);