Archive for April, 2007

68 Introduction to (Web site design) Java Applications Chapter 2 Executing

Monday, April 30th, 2007

68 Introduction to Java Applications Chapter 2 Executing the statement at lines 12 13 displays the dialog box in Fig. 2.8. The title bar of the dialog contains the string Message, to indicate that the dialog is presenting a message to the user. The dialog box automatically includes an OK button that allows the user to dismiss (hide) the dialog by pressing the button. This is accomplished by positioning the mouse cursor (also called the mouse pointer) over the OK button and clicking the left mouse button. Remember that all statements in Java end with a semicolon (;). Therefore, lines 12 13 represent one statement. Java allows large statements to be split over many lines. However, you cannot split a statement in the middle of an identifier or in the middle of a string. Common Programming Error 2.8 Splitting a statement in the middle of an identifier or a string is a syntax error. Line 15, System.exit( 0 ); // terminate application uses staticmethod exitof class Systemto terminate the application. In any application that displays a graphical user interface, this line is required in order to terminate the application. Notice once again the syntax used to call the method the class name (System), a dot (.) and the method name (exit). Remember that identifiers starting with capital letters normally represent class names. So, you can assume that System is a class. Class System is part of the package java.lang. Notice that class System is not imported with an import statement at the beginning of the program. By default, package java.langis imported in every Java program. Package java.langis the only package in the Java API for which you are not required to provide an importstatement. The argument 0to method exitindicates that the application has terminated successfully. (A nonzero value normally indicates that an error has occurred.) This value is passed to the command window that executed the program. The argument is useful if the program is executed from a batch file (on Windows 95/98/ME/NT/2000 systems) or a shell script (on UNIX/Linux systems). Batch files and shell scripts often execute several programs in sequence. When the first program ends, the next program begins execution automatically. It is possible to use the argument to method exitin a batch file or shell script to determine whether other programs should execute. For more information on batch files or shell scripts, see your operating system s documentation. Title bar The OK button allows the user to dismiss the dialog box. The dialog box is automatically sized to accommodate the string. Mouse cursor Fig. 2.8 Message dialog box. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

Chapter 2 (Free web space) Introduction to Java Applications 67 menu

Monday, April 30th, 2007

Chapter 2 Introduction to Java Applications 67 menu menu barbutton text field Fig. 2.7A sample Netscape Navigator window with GUI components. Fig. In method mainof Fig. 2.6, lines 12 13, JOptionPane.showMessageDialog( null, “WelcomentonJavanProgramming!” ); indicate a call to method showMessageDialog of class JOptionPane. The method requires two arguments. When a method requires multiple arguments, the arguments are separated with commas (,). Until we discuss JOptionPanein detail in Chapter 13, the first argument will always be the keyword null. The second argument is the string to display. The first argument helps the Java application determine where to position the dialog box. When the first argument is null, the dialog box appears in the center of the computer screen. Most applications you use on your computer execute in their own window (e.g., email programs, Web browsers and word processors). When such an application displays a dialog box, it normally appears in the center of the application window, which is not necessarily the center of the screen. Later in this book, you will see more elaborate applications in which the first argument to method showMessageDialogwill cause the dialog box to appear in the center of the application window, rather than the center of the screen. Good Programming Practice 2.11 Place a space after each comma (,) in an argument list, to make programs more readable. Method JOptionPane.showMessageDialogis a special method of class JOptionPanecalled a staticmethod. Such methods are always called by using their class name followed by a dot operator (.) and the method name, as in ClassName.methodName( arguments ) Many of the predefined methods we introduce early in this book are staticmethods. We ask you to mimic this syntax for calling staticmethods until we discuss them in detail in Chapter 8, Object-Based Programming. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Web hosting - 66 Introduction to Java Applications Chapter 2 1

Monday, April 30th, 2007

66 Introduction to Java Applications Chapter 2 1 // Fig. 2.6: Welcome4.java 2 // Printing multiple lines in a dialog box 3 4 // Java extension packages 5 import javax.swing.JOptionPane; // import class JOptionPane 6 7 public class Welcome4 { 8 9 // main method begins execution of Java application 10 public static void main( String args[] ) 11 { 12 JOptionPane.showMessageDialog( 13 null, “WelcomentonJavanProgramming!” ); 14 15 System.exit( 0 ); // terminate application 16 17 } // end method main 18 19 } // end class Welcome4 Fig. 2.6Displaying multiple lines in a dialog box. Fig. 2. We will provide an overview of the use of this documentation with the downloads and resources for Java How to Program, Fourth Edition on our Web site, www.deitel.com. Packages are discussed in detail in Chapter 8, Object-Based Programming. Common Programming Error 2.7 All importstatements must appear before the class definition. Placing an importstatement inside a class definition s body or after a class definition is a syntax error. Line 5 tells the compiler to load the JOptionPane class from the javax.swing package. This package contains many classes that help Java programmers define graphical user interfaces (GUIs) for their application. GUI components facilitate data entry by the user of your program and formatting or presentation of data outputs to the user of your program. For example, Fig. 2.7 contains a Netscape Navigator window. In the window, there is a bar containing menus (File, Edit, View, etc.), called a menu bar. Below the menu bar is a set of buttons that each have a defined task in Netscape Navigator. Below the buttons there is a text field in which the user can type the name of the World Wide Web site to visit. The menus, buttons and text fields are part of Netscape Navigator s GUI. They enable you to interact with the Navigator program. Java contains classes that implement the GUI components described here and others that will be described in Chapter 12, Basic Graphical User Interface Components, and Chapter 13, Advanced Graphical User Interface Components. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

Chapter 2 Introduction to Java Applications 65 2.4 (Web site development)

Monday, April 30th, 2007

Chapter 2 Introduction to Java Applications 65 2.4 Displaying Text in a Dialog Box Although the first several programs presented in this chapter display output in the command window, many Java applications use windows or dialog boxes (also called dialogs) to display output. For example, World Wide Web browsers such as Netscape Navigator or Microsoft Internet Explorer display Web pages in their own windows. Email programs typically allow you to type and read messages in a window provided by the email program. Typically, dialog boxes are windows in which programs display important messages to the user of the program. Java s class JOptionPane provides prepackaged dialog boxes that enable programs to display messages to users. Figure 2.6 displays the same string as in Fig. 2.4 in a predefined dialog box known as a message dialog. One of the great strengths of Java is its rich set of predefined classes that programmers can reuse rather than reinventing the wheel. We use many of these classes throughout the book. Java s numerous predefined classes are grouped into categories of related classes called packages. The packages are referred to collectively as the Java class library, or the Java applications programming interface (Java API). The packages of the Java API are split into core packages and extension packages. The names of the packages begin with either java (core packages) or javax (extension packages). Many of the core and extension packages are included as part of the Java 2 Software Development Kit. We overview these included packages in Chapter 6. As Java continues to evolve, new packages are developed as extension packages. These extensions often can be downloaded from java.sun.com and used to enhance Java s capabilities. In this example, we use class JOptionPane, which Java defines for us in package javax.swing. Line 4, // Java extension packages is a single-line comment indicating the section of the program in which we specify import statements for classes in Java s extension packages. In every program that specifies import statements, we separate the import statements into the following groups: Java core packages (for package names starting with java), Java extension packages (for package names starting with javax) and Deitel packages (for our own packages defined later in the book). Line 5, import javax.swing.JOptionPane; // import class JOptionPane is an import statement. The compiler uses importstatements to identify and load classes used in a Java program. When you use classes from the Java API, the compiler attempts to ensure that you use them correctly. The import statements help the compiler locate the classes you intend to use. For each new class we use from the Java API, we indicate the package in which you can find that class. This package information is important. It helps you locate descriptions of each package and class in the Java API documentation. A Web- based version of this documentation can be found at java.sun.com/j2se/1.3/docs/api/index.html Also, you can download this documentation to your own computer from java.sun.com/j2se/1.3/docs.html Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

64 Introduction to (Web hosting domain) Java Applications Chapter 2 two

Sunday, April 29th, 2007

64 Introduction to Java Applications Chapter 2 two characters and nare not printed on the screen. The backslash () is called an escape character. It indicates that a special character is to be output. When a backslash appears in a string of characters, Java combines the next character with the backslash to form an escape sequence. The escape sequence nis the newline character. When a newline character appears in a string being output with System.out, the newline character causes the screen s output cursor to move to the beginning of the next line in the command window. Some other common escape sequences are listed in Fig. 2.5. 1 // Fig. 2.4: Welcome3.java 2 // Printing multiple lines of text with a single statement. 3 4 public class Welcome3 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( “WelcomentonJavanProgramming!” ); 10 11 } // end method main 12 13 } // end class Welcome3 Welcome to Java Programming! Fig. 2.4Printing multiple lines of text with a single statement. Fig. 2. Escape sequence Description n t r \ ” Newline. Position the screen cursor to the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. Backslash. Used to print a backslash character. Double quote. Used to print a double-quote character. For example, System.out.println( “”in quotes”" ); displays “in quotes” Fig. 2.5Some common escape sequences. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Chapter 2 Introduction (Web design software) to Java Applications 63 1

Sunday, April 29th, 2007

Chapter 2 Introduction to Java Applications 63 1 // Fig. 2.3: Welcome2.java 2 // Printing a line of text with multiple statements. 3 4 public class Welcome2 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.print( “Welcome to ” ); 10 System.out.println( “Java Programming!” ); 11 12 } // end method main 13 14 } // end class Welcome2 Welcome to Java Programming Fig. 2.3Printing a line of text with multiple statements. Fig. 2. display one line of text in the command window. The first statement uses System.out s method print to display a string. The difference between printand printlnis that, after displaying its argument, print does not position the output cursor at the beginning of the next line in the command window; the next character the program displays in the command window will appear immediately after the last character that print displays. Thus, line 10 positions the first character in its argument, J, immediately after the last character that line 9 displays (the space character at the end of the string on line 9). Each printor printlnstatement resumes displaying characters from where the last print or printlnstatement stopped displaying characters. 2.3.2 Displaying Multiple Lines of Text with a Single Statement A single statement can display multiple lines by using newline characters. Newline characters are special characters that indicate to System.out s print and println methods when they should position the output cursor to the beginning of the next line in the command window. Figure 2.4 outputs four lines of text, using newline characters to determine when to begin each new line. Most of the program is identical to those of Fig. 2.1 and Fig. 2.3, so we discuss only the changes here. Line 2, // Printing multiple lines of text with a single statement. is a single-line comment stating the purpose of this program. Line 4 begins the definition of class Welcome3. Line 9, System.out.println( “WelcomentonJavanProgramming!” ); displays four separate lines of text in the command window. Normally, the characters in a string are displayed exactly as they appear in the double quotes. Notice, however, that the Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

62 Introduction to Java Applications (Top ten web hosting) Chapter 2 Fig.

Sunday, April 29th, 2007

62 Introduction to Java Applications Chapter 2 Fig. 2.2 Executing Welcome1in a Microsoft Windows 2000 Command Prompt. In the command prompt of Figure 2.2, we typed java Welcome1 to launch the javainterpreter and indicate that it should load the .class file for class Welcome1. Note that the .class file-name extension is omitted from the preceding command; otherwise the interpreter will not execute the program. The interpreter automatically calls method main. Next, the statement on line 7 of maindisplays Welcome to JavaProgramming! Testing and Debugging Tip 2.2 The Java compiler generates syntax error messages when the syntax of a program is incorrect. When you are learning how to program, sometimes it is helpful to break a working program so you can see the error messages produced by the compiler. Then, when you encounter that error message again, you will have an idea of the error s cause. Try removing a semicolon or curly brace from the program of Fig. 2.1, then recompile the program to see the error messages generated by the omission. 2.3 Modifying Our First Java Program This section continues our introduction to Java programming with two examples that modify the example in Fig. 2.1 to print text on one line by using multiple statements and to print text on several lines by using a single statement. 2.3.1 Displaying a Single Line of Text with Multiple Statements Welcome to Java Programming! can be displayed using several methods. Class Welcome2, shown in Fig. 2.3, uses two statements to produce the same output as that shown in Fig. 2.1. Most of the program is identical to that of Fig. 2.1, so we discuss only the changes here. Line 2, // Printing a line of text with multiple statements. is a single-line comment stating the purpose of this program. Line 4 begins the definition of class Welcome2. Lines 9 10 of method main, System.out.print( “Welcome to ” ); System.out.println( “Java Programming!” ); Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

Free web hosting music - Chapter 2 Introduction to Java Applications 61 The

Sunday, April 29th, 2007

Chapter 2 Introduction to Java Applications 61 The entire line, including System.out.println, its argument in the parentheses (the string) and the semicolon (;), is a statement. Every statement must end with a semicolon (also known as the statement terminator). When the statement on line 9 of our program executes, it displays the message Welcome to Java Programming! in the command window. Common Programming Error 2.6 Omitting the semicolon at the end of a statement is a syntax error. A syntax error occurs when the compiler cannot recognize a statement. The compiler normally issues an error message to help the programmer identify and fix the incorrect statement. Syntax errors are violations of the language rules. Syntax errors are also called compile errors, compile-time errors or compilation errors, because the compiler detects them during the compilation phase. You will be unable to execute your program until you correct all of the syntax errors in it. Testing and Debugging Tip 2.1 When the compiler reports a syntax error, the error may not be on the line number indicated by the error message. First, check the line for which the error was reported. If that line does not contain syntax errors, check the preceding several lines in the program. Some programmers find it difficult when reading and/or writing a program to match the left and right braces ({ and }) that delimit the body of a class definition or a method definition. For this reason, some programmers prefer to include a single-line comment after a closing right brace (}) that ends a method definition and after a closing right brace that ends a class definition. For example, line 11, } // end method main specifies the closing right brace (}) of method main, and line 13, } // end class Welcome1 specifies the closing right brace (}) of class Welcome1. Each comment indicates the method or class that the right brace terminates. We use such comments through Chapter 6 to help beginning programmers determine where each program component terminates. After Chapter 6, we use such comments when pairs of braces contain many statements, which makes the closing braces difficult to identify. Good Programming Practice 2.10 Some programmers prefer to follow the closing right brace (}) of a method body or class definition with a single-line comment indicating the method or class definition to which the brace belongs. This comment improves program readability. 2.2.1 Compiling and Executing your First Java Application We are now ready to compile and execute our program. To compile the program, we open a command window, change to the directory where the program is stored and type javac Welcome1.java If the program contains no syntax errors, the preceding command creates a new file called Welcome1.class containing the Java bytecodes that represent our application. These bytecodes will be interpreted by the java interpreter when we tell it to execute the program, as shown in the Microsoft Windows 2000 Command Prompt of Fig. 2.2. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Web hosting domains - 60 Introduction to Java Applications Chapter 2 Line

Saturday, April 28th, 2007

60 Introduction to Java Applications Chapter 2 Line 5 is a blank line, inserted for program readability. Line 6, // main method begins execution of Java application is a single-line comment indicating the purpose of lines 6 11 of the program. Line 7, public static void main( String args[] ) is a part of every Java application. Java applications begin executing at main. The parentheses after main indicate that main is a program building block called a method. Java class definitions normally contain one or more methods. For a Java application class, exactly one of those methods must be called main and must be defined as shown on line 7; otherwise, the java interpreter will not execute the application. Methods are able to perform tasks and return information when they complete their tasks. The void keyword indicates that this method will perform a task (displaying a line of text, in this program), but will not return any information when it completes its task. Later, we will see that many methods return information when they complete their task. Methods are explained in detail in Chapter 6. For now, simply mimic main s first line in your Java applications. The left brace, {, on line 8 begins the body of the method definition. A corresponding right brace, }, must end the method definition s body (line 11 of the program). Notice that the line in the body of the method is indented between the braces. Good Programming Practice 2.9 Indent the entire body of each method definition one level of indentation between the left brace, {, and the right brace, }, that define the body of the method. This format makes the structure of the method stand out and helps make the method definition easier to read. Line 9, System.out.println( “Welcome to Java Programming!” ); instructs the computer to perform an action, namely to print the string of characters contained between the double quotation marks. A string is sometimes called a character string, a message or a string literal. We refer to characters between double quotation marks generically as strings. White-space characters in strings are not ignored by the compiler. System.out is known as the standard output object. System.out allows Java applications to display strings and other types of information in the command window from which the Java application executes. In Microsoft Windows 95/98/ME, the command window is the MS-DOS prompt. In Microsoft Windows NT/2000, the command window is the Command Prompt (cmd.exe). In UNIX, the command window is normally called a command window, a command tool, a shell tool or a shell. On computers running an operating system that does not have a command window (such as a Macintosh), the java interpreter normally displays a window containing the information the program displays. Method System.out.printlndisplays (or prints) a line of text in the command window. When System.out.println completes its task, it automatically positions the output cursor (the location where the next character will be displayed) to the beginning of the next line in the command window. (This move of the cursor is similar to you pressing the Enter key when typing in a text editor the cursor appears at the beginning of the next line in your file.) Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

Web server certificate - Chapter 2 Introduction to Java Applications 59 Software

Saturday, April 28th, 2007

Chapter 2 Introduction to Java Applications 59 Software Engineering Observation 2.1 Avoid using identifiers that contain dollar signs ($), as the compiler often uses dollar signs to create identifier names. In Chapter 2 through Chapter 7, every class we define begins with the public keyword. For now, we will simply require this keyword. The public keyword is discussed in detail in Chapter 8. Also in that chapter, we discuss classes that do not begin with keyword public. [Note: Several times early in this text, we ask you to mimic certain Java features we introduce as you write your own Java programs. We specifically do this when it is not yet important for you to know all of the details of a feature in order for you to use that feature in Java. All programmers initially learn how to program by mimicking what other programmers have done before them. For each detail we ask you to mimic, we indicate where the full discussion will be presented later in the text.] When you save your public class definition in a file, the file name must be the class name followed by the .java file-name extension. For our application, the file name is Welcome1.java. All Java class definitions are stored in files ending with the file-name extension .java. Common Programming Error 2.3 It is an error for a public class if the file name is not identical to the class name (plus the .java extension) in terms of both spelling and capitalization. Therefore, it is also an error for a file to contain two or more public classes. Common Programming Error 2.4 It is an error not to end a file name with the .java extension for a file containing an application s class definition. If the extension is missing, the Java compiler will not be able to compile the class definition. A left brace (at the end of line 4), {, begins the body of every class definition. A corresponding right brace (in line 13 in this program), }, must end each class definition. Notice that lines 6 11 are indented. This indentation is one of the spacing conventions mentioned earlier. We define each spacing convention as a Good Programming Practice. Good Programming Practice 2.6 Whenever you type an opening left brace, {, in your program, immediately type the closing right brace, }, then reposition the cursor between the braces to begin typing the body. This practice helps prevent errors due to missing braces. Good Programming Practice 2.7 Indent the entire body of each class definition one level of indentation between the left brace, {, and the right brace, }, that define the body of the class. This format emphasizes the structure of the class definition and helps make the class definition easier to read. Good Programming Practice 2.8 Set a convention for the indent size you prefer, and then uniformly apply that convention. The Tab key may be used to create indents, but tab stops may vary between editors. We recommend using three spaces to form a level of indent. Common Programming Error 2.5 If braces do not occur in matching pairs, the compiler indicates an error. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services