Chapter - 4: Understanding How Java |
||||
49 views
Programs Work:An important distinction to make in Java programming is where your program is supposed to be running. Some programs are intended to work on your computer; you type in a command or click an icon to start them up. Other programs are intended to run as part of a World Wide Web page. You encountered several examples of this type of program during the previous hour’s whirlwind vacation. Java programs that run locally on your own computer are called applications. Programs that run on Web pages are called applets. During this hour, you’ll learn why that distinction is important, and the following topics will be covered:
Creating an Application:Although Java has become well-known because it can be used in conjunction with World Wide Web pages, you can also use it to write any type of computer program. The Saluton program you wrote during Hour 2, “Writing Your First Program,” is an example of a Java application. To try out another program, use your word processor to open up a new file and enter everything from Listing 4.1. Remember not to enter the line numbers and colons along the left side of the listing; these items are used to make parts of programs easier to describe in the book. When you’re done, save the file as Root.java, making sure to save it in text-only or plain ASCII text format. Example 4.1. The Full Text of Root.java1: class Root {
2: public static void main(String[] arguments) {
3: int number = 225;
4: System.out.println("The square root of "
5: + number
6: + " is "
7: + Math.sqrt(number) );
8: }
9: }
The Root application accomplishes the following tasks:
Before you can test out this application, you need to compile it using the Software Development Kit’s javac compiler or the compiler included with another Java development environment. If you’re using the SDK, go to a command line, open the folder that contains the Root.java file, then compile Root.java by entering the following at a command line: javac Root.java If you have entered Listing 4.1 without any typos, including all punctuation and every word capitalized exactly as shown, it should compile without any errors. The compiler responds to a successful compilation by not responding with any message at all. Java applications are compiled into a class file that can be run by a Java interpreter. If you’re using the SDK, you can run the compiled Root.class file by typing this command: java Root The output should resemble the following: The square root of 225 is 15.0
When you run a Java application, the interpreter looks for a main() block and starts handling Java statements at that point. If your program does not have a main() block, the interpreter will respond with an error. Sending Arguments to Applications:Because Java applications are run from a command line, you can send information to applications at the same time you run them. The following example uses the java interpreter to run an application called DisplayTextFile.class, and it sends two extra items of information to the application, readme.txt and /p: java DisplayTextFile readme.txt /p Extra information you can send to a program is called an argument. The first argument, if there is one, is provided one space after the name of the application. Each additional argument is also separated by a space. If you want to include a space inside an argument, you must put quotation marks around the argument, as in the following: java DisplayTextFile readme.txt /p "Page Title" This example runs the DisplayTextFile program with three arguments: readme.txt, /p, and Page Title. The quote marks prevent Page and Title from being treated as separate arguments. You can send as many arguments as you want to a Java application. In order to do something with them, however, you have to write some statements in the application to handle them. To see how arguments work in an application, create a new file in your word processor called Blanks.java. Enter the text of Listing 4.2 into the file and save it when you’re done. Compile the program, correcting any errors that are caused by typos. Example 4.2. The Full Text of Blanks.java1: class Blanks {
2: public static void main(String[] arguments) {
3: System.out.println("The " + arguments[0]
4: + " " + arguments[1] + " fox "
5: + "jumped over the "
6: + arguments[2] + " dog.");
7: }
8: }
To try out the Blanks application, run it with a Java interpreter such as the SDK’s java tool. Give it three adjectives of your own choosing as arguments, as in the following example: java Blanks retromingent purple lactose-intolerant The application uses the adjectives to fill out a sentence. Here’s the one produced by the preceding three arguments: The retromingent purple fox jumped over the lactose-intolerant dog.
Try it with some of your own adjectives, making sure to always include at least three of them. Arguments are a useful way to customize the performance of a program. They are often used to configure a program so it runs a specific way. Java stores arguments in arrays, groups of related variables that all hold the same information. You’ll learn about arrays during Hour 9, “Storing Information with Arrays.” Applet Basics:When the Java language was introduced in 1995, the language feature that got the most attention was applets, Java programs that run on a World Wide Web page. Before Java, Web pages were a combination of text, images, and forms that used gateway programs running on the computer that hosted the pages. These gateway programs required special access to the Web server presenting the page, so most Web users did not have the ability to use them. Writing them required even more expertise. In contrast, programmers of all skill levels can write Java applets, and you’ll write several during the span of these 24 hours. You can test them with any Web browser that handles Java programs, and put one on a Web page without requiring any special access from a Web provider. The Java programs you toured during the previous hour were all applets. Their structure differs from applications in several important ways, and they are designed specifically for presentation on the World Wide Web. Unlike applications, applets do not have a main() block. Instead, they have several different sections that are handled depending on what is happening in the applet, as detailed fully during Hour 17. Two of the sections are the init() block statement and the paint() block. init() is short for initialization, and it is used to take care of anything that needs to be set up as an applet first runs. The paint() block is used to display anything that should be displayed. To see an applet version of the Root application, create a new file in your word processor and call it RootApplet.java. Enter the code in Listing 4.3 and save it when you’re done. Example 4.3. The Full Text of RootApplet.java 1: import java.awt.*;
2:
3: public class RootApplet extends javax.swing.JApplet {
4: int number;
5:
6: public void init() {
7: number = 225;
8: }
9:
10: public void paint(Graphics screen) {
11: Graphics2D screen2D = (Graphics2D) screen;
12: screen2D.drawString("The square root of " +
13: number +
14: " is " +
15: Math.sqrt(number), 5, 50);
16: }
17: }
Compile this file using your Java development software. If you are using the javac compiler tool in the SDK, type the following at a command line: javac RootApplet.java This program contains a lot of the same statements as the Root application that did the same thing. The main difference is in how it is organized—the main() block has been replaced with an init() block and a paint() block. Unlike applications, compiled Java applets cannot be tested using a Java interpreter. You have to put them on a Web page and view that page in one of two ways:
To create a Web page that can display the RootApplet program, return to your word processor and create a new file. Enter Listing 4.4 in that file and save it as RootApplet.html. Example 4.4. The Full Text of RootApplet.html1: <applet code="RootApplet.class" height=100 width=300> 2: </applet> This Web page contains the bare minimum needed to display a Java applet on a Web page. The <APPLET> tag is used to specify that a Java program is being put on the page, the code attribute provides the name of the applet, and the height and width attributes describe the size of the applet’s display area. These items will be described in detail during Hour 17. To see this applet using the appletviewer tool included with Software Development Kit, type the following at a command line: appletviewer RootApplet.html To see it using your computer’s default Web browser, type the following instead: RootApplet.html You can also load this page in your browser: Choose File, Open, then navigate to the folder that contains RootApplet.html and select the file. Figure 4.1 shows what the applet looks like when loaded with Internet Explorer.
|
| « Chapter - 3: Vacationing in Java | Free PHP RSS to HTML » |
| Posted on Wednesday, January 28th, 2009 at 1:35 pm under Java 2 (simpler way-Old version) | RSS 2.0 Feed | |

