Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

3. Entering, compiling, and executing your Java program in TextPad

  1. Type or paste your Java program source code into the large text pane.
  2. Create new directory. Putting the programs directly on your desktop is not a good idea. Create a directory (folder) for all of your course work, and a separate directory within that for each assignment. The tiny extra work to create separate directories prevents unwanted interactions between old and new programs. It can save endless hours of frustration. This is not just an academic neatness obsession - it comes from seeing students who couldn't complete their assignments because of old-file pollution.
  3. Save the source in a file with EXACTLY the same name as the "class" with an extension of ".java". Assume the source code defines this class.
        public class InitialTest {
    . . .
    }

    It must be saved in a file called InitialTest.java . Put it in the directory you created previously.

  4. Compile (translate from source code to machine code) by selecting the Tools > Compile Java menu item. If there are errors (and there usually will be at first), go back and forth between the "Command Results" and the source code by clicking on each of them in the top left pane. Continue with this process until it compiles correctly.
  5. Run the program by choosing Tools > Run Java Application menu item. You will be using one of these to styles of interaction (Input-Output) at the beginning.
    • Console Output (System.out.println(...)). This will open a DOS Command Window and you will see your text output, followed by "Press any key to continue . . .". Your program is done!
    • Dialogbox Output (JOptionPane.showMessageDialog). Program output will appear in dialog boxes. The only weird thing is that TextPad creates an unnecessary DOS window -- that isn't something that the Java system does.

Optional, but useful, TextPad preferences

There no critical preferences that need changing, but they can be set by selecting the Configure > Preferences menu item. Some things you might wish to set:

Tabs
To conform to the Java indentation standards, you should set your tabs as follows. Go to
Configure menu > Preferences... > Document Classes > Java > Tabulation
Set the "Default tab spacing" and "Indent size" fields to 4.
Check both the "Convert new tabs to spaces" and "Convert existing tabs to spaces when saving new files".
Click OK.
Folders
Set the Startup directory to where your files are normally located.
Document Classes > Java > Tabulation
Set the default tab spacing to 4, indent size to 4, and check both the Convert new tabs to spaces and Convert existing tabs to spaces when saving files. Tabs should generally not be used in programming because the size of tabs is not universal so someone else looking at your program will often see a mess
Assertions
To turn runtime assertion checking on in TextPad go to Configure > Preferences... > Tools > Run Java Application. Set the value in the Parameters text field to -ea $BaseName . "-ea" is the abbreviation for Enable Assertions. If you don't know what assertions are yet, don't bother with this.
Line numbers
This isn't in the Preferences area, altho it should be. TextPad will show line numbers if you select View menu then choose Line Numbers. This is useful in coordinating error messages with your source code.

1 comment:

Archives