Step 8.2: Create HelloWorld2


Step 8.2: Create HelloWorld2

click to expand

The original HelloWorld program doesn't do much; it just prints a literal string. That doesn't leave a lot of room for debugging. In this next program, you'll initialize a counter that you'll use to control a loop. Your goal will be to run it without debugging, then later, using the debugger, go in and modify the counter to make the program run differently. But first, you need to copy the program. Once again, I'll provide two options: source entry and import. And once again, I suggest you do both, just for practice. But if you're in a huge hurry, skip to Option 2.

Option 1: Source Entry

q 8.2(a) Right-click on HelloWorld.java and select Copy.

click to expand
Figure 8.5: Copying the program using the popup menu.

q 8.2(b) Right-click on your default package and select Paste.

click to expand
Figure 8.6: Getting ready to paste the source back into the default package.

This causes a name conflict, because HelloWorld already exists, so you have to address that in the Name Conflict window:

q 8.2(c) Enter 'HelloWorld2' and click OK.

click to expand
Figure 8.7: Change the name to resolve the conflict.

And that's all it takes to copy a source file!

Caution  

Copying a file does not automatically open it! In Figure 8.8, you will see that the new file, HelloWorld2.java, is not open. Only the old file, HelloWorld.java, is open. I have on occasion forgotten this and modified the old program without realizing it! After copying a file, you must open it to edit it. The next steps will open the file.

click to expand
Figure 8.8: After copying the file, the old file is open, but the new file is not.

Use the popup menu to open the new file (right-click on the file to bring up the popup menu). You can also double-click on a file to use the default editor.

q 8.2(d) Right-click on HelloWorld2.java and select Open.

click to expand
Figure 8.9: To open the new file, simply right-click on it and select Open (or double-click the file).

Now the new file is open.

click to expand
Figure 8.10: HelloWorld2 is now open in the editor, along with HelloWorld.

q 8.2(e) Modify the main method with the following source.

 int count = 3; for (int i = 0; i < count; i++) { System.out.println("Hello" + i); } System.exit(0); 
click to expand
Figure 8.11: Modify the code to use a counter.

This code will use a counter (count) to loop three times, printing a slightly different message each time.

q 8.2(f) Right-click in the editor and select Save.

You can also press Ctrl-S to save the source.

click to expand
Figure 8.12: Saving the source using the editor's popup menu.

Option 2: Importing source from CD

If you've already performed Option 1, you can skip this section and go directly to Step 8.3. You could also execute this step; it won't hurt anything.

q 8.2(g) Right-click on the Hello project and select Import...

click to expand
Figure 8.13: Import into the Hello project.

q 8.2(h) Select File system and click Next.

click to expand
Figure 8.14: Import from a File system.

q 8.2(i) Use the dropdown for the directory field to select D:\Source\Step 7.

click to expand
Figure 8.15: Using the directory dropdown.
Note  

If you haven't already imported Step 7, you may not see anything in your dropdown, so instead just type "D:\Source\Step 7" into the Directory field and press the Tab key.

q 8.2(j) Left-click on the Step 7 folder.

click to expand
Figure 8.16: Select the Step 7 folder by left-clicking on it.

q 8.2(k) Check the checkbox next to HelloWorld2.java.

click to expand
Figure 8.17: Click on the checkbox next to HelloWorld2.java to select it.

q 8.2(l) Click Finish.

click to expand
Figure 8.18: Click Finish to import the file.

If you have already followed the actions in Option 1, you'll get a confirmation dialog.

q 8.2(m) Select Yes to overwrite the existing HelloWorld2.java file.

click to expand
Figure 8.19: Overwrite the existing source.

If you didn't follow Option 1, you'll need to open the file.

q 8.2(n) Right-click on HelloWorld2.java and select Open.

click to expand
Figure 8.20: Open HelloWorld2.java with the popup menu.

And here is your open file!

click to expand
Figure 8.21: The imported source code.



Eclipse
Eclipse: Step by Step (Step-by-Step series)
ISBN: 1583470441
EAN: 2147483647
Year: 2003
Pages: 90
Authors: Joe Pluta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net