0% found this document useful (0 votes)
358 views

Text Pad Tutorial

The document provides instructions for obtaining and installing the TextPad text editor and Java Development Kit (JDK) software in order to write, compile, and run Java programs using TextPad. Key steps include downloading TextPad from its website, downloading and installing the JDK from Sun's website, and adjusting TextPad preferences such as setting the default file extension to .java and displaying line numbers.

Uploaded by

Peter Hazops
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
358 views

Text Pad Tutorial

The document provides instructions for obtaining and installing the TextPad text editor and Java Development Kit (JDK) software in order to write, compile, and run Java programs using TextPad. Key steps include downloading TextPad from its website, downloading and installing the JDK from Sun's website, and adjusting TextPad preferences such as setting the default file extension to .java and displaying line numbers.

Uploaded by

Peter Hazops
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

GETTING STARTED WITH TEXTPAD (updated: 12/27/09)

OBTAINING AND INSTALLING TEXTPAD FOR YOUR HOME PC: Since TextPad is installed in the lab, obtaining it for your home PC is recommended, but not required. TextPad is a simple no-frills tool that allows you to enter, compile, and run programs. The programs can be written in one of many different languages. In order to make a particular language work in TextPad, the language's compiler must be installed. The compiler installation is separate from TextPad's installation. To install Sun's Java compiler, go to this Web site: http://java.sun.com/javase/downloads On that Web site, you'll see various options for downloading Sun's Java Standard Edition (SE) software. Feel free to read about all the download options and download the ones you want, but the only one you'll really need is the JDK 6 Update 17 option (JDK stands for Java Development Kit, 6 is the current version, and 17 refers to the 17th update). Click the JDK 6 Update 17 link. Using the Web site's prompts as a guide, you might have to follow a series of instructions and links in order to find the link for the jdk-6u17 file. Click on that link and save the file by downloading it to your computer. After the download completes, install the JDK as directed by the Web site's documentation. Important: You must install the Java compiler before installing TextPad. By installing in that order, the compiler commands will be automatically added to the TextPad Tools menu the first time TextPad is run on your PC. To obtain TextPad, go to this Web site and follow the download instructions: http://www.textpad.com/ After the download is done, use Windows Explorer to find the downloaded file. Double click the file and follow the installation instructions. Later on, when youre working through the steps in this tutorial, if you cant find TextPad's Compile and Run commands in the Tools / External Tools menu, then you probably either forgot to install Suns JDK or you installed TextPad before you installed Suns JDK. If you installed TextPad before you installed Suns JDK, then TextPad wont detect the JDK's presence, and it wont add the JDK to its Tools area. To correct this problem, try manually adding the JDK tools to TextPad by doing this from within TextPad: Configure Preferences Tools Add JDK Commands If that doesnt work, read TextPad's FAQ (frequently asked questions) page at http://www.textpad.com/support/faq/java.html. In going through the FAQ, you may find that you have to reinstall TextPad. Prior to reinstalling, make sure that you remove TextPad completely from your computer. If reinstalling TextPad doesnt work, try removing both the JDK and TextPad and then reinstalling both the JDK and TextPad, in that order.

SAVING FILES:

Naturally, you are required to save your program files. In the lab, you are required to save your files on a USB flash drive storage device. If you're working at home, you may save your save your files on your computers hard disk, but you're still required to have a flash drive so that you can transport your files to and from school. If you're at the school's lab, do not use the hard disks ever! To buy a flash drive, go to a computer store, Wal-mart, or shop online. If you want to shop online, you can find good prices by going to http://www.tigerdirect.com/ and clicking on the USB Flash Drives link.

2.

Typically, to install your USB flash drive, you simply have to plug your flash drive cable into your USB port. To copy to and from your USB flash drive, open Windows Explorer and drag files with your mouse. Typically, to remove your USB flash drive, you should click an unplug icon in your system tray prior to physically unplugging your flash drive from your USB port. See your USB flash drive's product packaging for details.

TEXTPAD TUTORIAL: This tutorial assumes that you are working in the lab. Since you are not allowed to save anything on the hard disks in the lab, the tutorial asks you to create all your files on your USB flash drive, not on your hard disk. Flash drives often use the f: drive, so the tutorial uses the f: drive. If your flash drive uses a drive letter other than f, replace f with your flash drive letter throughout the tutorial. If you do this tutorial at home and you'd like to use your hard drive, replace f with your hard drive letter throughout the tutorial. When working on homework assignments, use TextPad to enter, debug, and run your programs. If you forget how to do something in TextPad, it is up to you to carefully reread this TextPad tutorial and figure out how to do things. To reduce the number of rereads, go through this tutorial slowly and try to remember what you're doing. Whenever you are asked to perform an action (left column below) that you've done before, I will refrain from providing the supplemental information (right column below). If you don't remember the details of how to do something, look for it earlier in the tutorial. I expect you to have to look up a lot of previously covered material. This is my attempt to force you to start memorizing how to do things. Actions Insert your flash drive. Load TextPad. Supplemental Information The flash drive inserts into a USB port. If you can't find your computer's USB port, ask someone for help. Click on the Start menu. Select Programs. If you don't see a selection for TextPad yet, drill down further with an appropriate selection(s). Select TextPad. By default, TextPad saves files with an extension of .txt. Thus, if you enter Hello as your filename (which you will do later on), then your file will be saved with the name Hello.txt . If you enter Hello.java as your filename, then your file will be saved with the name Hello.java.txt . Since Java requires its source files to use the .java extension, neither of these filenames is acceptable. As a workaround, you could use quotes to explicitly enter the filename. For example, if you enter "Hello.java" as your filename, then your file will be saved with the name Hello.java . Although the workaround works, I recommend that you change the default file extension to .java. That way, you won't have to use quotes around your filenames.

Understand TextPad's default file extension.

3.
Actions Change TextPad's default file extension to .java. Supplemental Information Click the Configure menu. Select Preferences. In the Preferences window, select the File option. In the Default file extension field, overlay the value txt with java. In the Preferences window, select the File Name Filters option. In the File name filters field, select the Java (*.java) option. Click the Move Up button until the Java (*.java) option is at the top of the File name filters field. Click OK. By default, TextPad is set to not display line numbers in your source code. That's OK, but it's sometimes convenient to have the line numbers displayed. Thus, I recommend that you turn on the line number feature for now. If you later find it to be a nuisance, feel free to turn it off. Turn on the line number feature as follows: Click the Configure menu. Select Preferences. In the Preferences window's left pane, select the View option. That should cause the Preference window's right pane to display View options. Make sure that the Line numbers box is checked. Click OK. Turn off TextPad's automatic indent setting. By default, TextPad is set to indent after an opening curly brace ({) is entered. This feature makes it difficult to indent properly. Turn off the feature as follows: Open the Preferences window. In the Preferences window, find the Document Classes container in the "container tree" at the left. Expand the Document Classes container by clicking on its "+" box (if there's a "-" box, skip this step). Within the Document Classes container, look for a container named Java. Click on the Java container name. That should cause the Preferences window's right pane to display a list of Document class options. Make sure that the "Automatically indent blocks" box is unchecked. Click OK. Always use spaces, not tabs. In my courses, I require that you indent with spaces, not tabs. Why? Because using tabs can lead to alignment problems. More specifically, if two people (student and teacher) view a program using different compiler environments, and the compiler environments have different tab settings, then tab-based indentations can lead to alignment problems. To avoid that situation, use space-based indentations, not tab-based indentations. Aside: In the real world, many programmers indent by pressing the tab key. That works OK as long as everyone who views a tab-based file has the same setting for tab width.

Display line numbers in your source code.

4.
Actions Adjust preferences to ensure that tabs are automatically converted to spaces. Supplemental Information To ensure that all your indentations are comprised of spaces, use the space bar, not the tab key, whenever you want to indent. Also, set your TextPad preferences so that tabs are automatically converted to spaces. Open the Preferences window. Expand the Document Classes container. Within the Document Classes container, expand the Java container. Click on the Tabulation option. That should cause the Preferences window's right pane to display tab settings. Make sure that these two check boxes are checked: Convert new tabs to spaces Convert existing tabs to spaces when saving files Set tab width to 2. If you do accidentally use the tab key, Id like you to use a relatively small tab width so that when you print your programs on paper, youll be less likely to have line wrap problems. In the Default tab spacing box, enter 2. In the Indent size box, enter 2. Click OK. To create a folder, you may use Windows Explorer or the My Computer icon. Since, in my opinion, Windows Explorer is a more elegant tool than My Computer, Ill provide an explanation of how to create a folder using Windows Explorer: Right click on the Start button. Select Explore from the pop-up menu. That should cause a Windows Explorer window to appear. In that window, a folder tree (= a directory tree) should appear in the left pane. Drag the left pane's scrollbar up or down until you see the flash drive (on most computers, the flash drive uses drive letter f, so look for f:). Click on the flash drive. Click the File menu. Select the New entry and wait for the sub-menu to appear. On the sub-menu, click Folder. A newly generated folder should appear in the right pane of your Windows Explorer window. The name of the new folder is New Folder by default. You now need to rename the folder to cs151: 1. If the name New Folder is already selected/highlighted, you should be able to just immediately enter cs151 and it should overlay New Folder. 2. If that doesnt work, then try clicking on the New Folder icon with the right mouse button. That should cause a menu to pop up. Select Rename from the pop-up menu. Immediately enter cs151 and it should overlay New Folder. Close the Windows Explorer window.

Create a cs151 folder in your flash drive's root folder.

5.
Actions Open a new Java source file. Supplemental Information After you've returned to the TextPad environment, click the File menu. Select Open. In the Open File(s) window's File name box, type f:\cs151\Hello.java (note that Java is case-sensitive; Hello.java must use an uppercase H so that it matches the Hello class name in the program below). Click the Open button. In response to the "Create the file?" prompt, click Yes. Prepare to enter source code. Enter this text: The name of your file, Hello.java , should appear in the left pane. Your cursor should appear in the right pane. The right pane is where you'll enter source code. Enter this text in the source pane such that <your name> is replaced with your actual name.
/*********************************************** * Hello.java * <your name> * * This program says hello to the world. *********************************************** public class Hello { public static void main(String[] args) { system.out.println("Hello, world!"); } // end main } // end class Hello

Understand the code.

The top section is known as the prologue. It provides documentation for human readers. Since the prologue is written using comment syntax, the compiler ignores the prologue. main methods should be enclosed in a class that is public and whose name matches the name of the enclosing file. For example, the main method above is enclosed in a public class named Hello. The class must be named Hello since the enclosing file is named Hello.java . Note that TextPad is a bit lenient in applying one part of the above rule - you can get away with the class not being public. Nevertheless, you should follow the rule so your code is portable.

Save the program. Compile the program.

Click on the save icon (the save icon looks like a diskette). Click the Tools menu. Position your mouse over the External Tools submenu. Select Compile Java.

6.
Actions Find the bug. Supplemental Information The compiler should have displayed this error message:
F:\cs151\Hello.java:1: unclosed comment /*********************************************** ^

The caret (^) points to the error. The "1" refers to the line number that contains the error. Note that you might see a second error message. If you do, it's an example of an error that's due to a prior error. If you have that type of error, you can ignore it because it will disappear when you fix the first error message's bug. Can you figure out the bug? Do not continue until you attempt to find the bug. Fix the bug and recompile. That's right, there needs to be a / at the end of the prologue section. To display the contents of your Hello.java file, click on Hello.java in the left pane. In the source pane, add a / immediately after the rightmost * in the prologue section's bottom line of *'s. Save the Hello.java file. Recompile. Find the bug. The compiler should have displayed this error message:
F:\cs151\Hello.java:12: package system does not exist system.out.println("Hello, world!"); ^

Can you figure out the error? Do not continue until you attempt to find the error. Fix the bug and recompile. That's right, the word system is misspelled - it needs to be capitalized. To quickly jump to the buggy line in your Hello.java file, double click the error message's first line (F:\cs151\Hello.java:12: package system does not exist). Change system to System. Save the Hello.java file. Recompile. After this compile, you should see "Tool completed successfully" at the bottom of the TextPad window.

7.
Actions Run the program. Supplemental Information Click the Tools menu. Position your mouse over the External Tools submenu. Select Run Java Application. A command-prompt window (formerly known as a DOS window) should be generated and this should be displayed inside it:
Hello, world! Press any key to continue ...

Exit from the commandprompt window. Create a copy of the Hello.java file.

Press a key and the command-prompt window should disappear. Using the technique explained above, load Windows Explorer. Use the scrollbar in Windows Explorer's left pane to find the f: drive. To expand the f: drive, click on the "+" (for Windows XP) or hollow triangle (for Windows Vista) at the left of f:. If there's a "-" (for Windows XP) or filled-in triangle (for Windows Vista), skip this step. Within the f: drive, look for a folder named cs151. Click on the cs151 folder. That should cause the contents of the cs151 folder to appear in Windows Explorer's right pane. Look for the file named Hello.java in the right pane. If you only see a file named Hello (with no extension), it's really the Hello.java file, but your Windows Explorer settings are set to not display file extensions. If you are so inclined (and I hope that you are so inclined), configure your Windows Explorer settings so that file extensions are always displayed. Ill let you figure out how to do that on your own. Right click on the Hello.java file and select Copy from the pop-up menu. In the left pane, right click on the cs151 folder and select Paste from the pop-up menu. The paste operation should have created a file named "Copy of Hello.java ." Using the technique explained above, rename "Copy of Hello.java " to Countdown.java . Keep the Windows Explorer window open since you'll need to use it later.

Open Countdown.java .

Return to TextPad. Click on the File menu and select Open Find the Countdown.java file in your cs151 folder. Select the Countdown.java file. Click the Open button. In the left pane, Countdown.java should be selected. In the right pane, Countdown.java 's contents should be displayed (at this point, Countdown.java 's contents is identical to Hello.java 's contents).

8.
Actions Edit Countdown.java . Supplemental Information Edit Countdown.java so that it contains this text:
/**************************************************** * Countdown.java * <your name> * * This program prints a countdown from a user-entered * starting position. ****************************************************/ import java.util.Scanner; public class Countdown { public static void main(String[] args) { int startPos; // starting position for countdown int i; // count-down value Scanner stdIn = new Scanner(System.in); System.out.print("Enter countdown starting position: "); startPos = stdIn.nextInt(); for (i=startPos; i>0;) { System.out.println(i + "..."); } System.out.println("Lift off!"); } // end main } // end class Countdown

Save the program. Compile the program. If necessary, fix compile errors. Run the program. When you run the program, you should be prompted to enter a countdown starting position. In response to the prompt, enter 30. After pressing enter, your output screen should be filled with multiple lines of 30... Why is your screen filled with multiple lines of 30... ? Your program is repeatedly printing the line 30... to your command-prompt window. That's an example of an infinite loop. Unfortunately, different operating systems produce slightly different command-prompt windows. The technique for stopping an infinite loop is different for the different command-prompt windows. On Windows 2000 computers, you can stop an infinite loop by clicking anywhere in the command-prompt window with your mouse and then clicking on the top-left corner pull-down menu and selecting Close. On Windows XP computers, you can stop an infinite loop by simply clicking on the top-left corner pull-down menu and selecting Close. Using one of the techniques described above (or possibly a different technique?), stop the infinite loop and then close the command-prompt window.

Stop the infinite loop.

9.
Actions Find the error. Supplemental Information An infinite loop is an example of a run-time error. Run-time errors are often more difficult to debug than compile errors because you do not have the benefit of a compiler-generated error message. Can you figure out the error? Do not continue until you attempt to find the error. Fix the error and recompile. That's right, the for loop heading is missing its third component. Replace the for loop heading with this line:
for (i=startPos; i>0; i--)

Recompile. Run the program. When you run the program, enter 30 in response to the countdown starting position prompt. After pressing enter, your output screen should display this:
Enter countdown starting position: 30 30 ... 29 ... <28 through 2 go here> 1 ... Lift off! Press any key to continue . . .

Note that you'll probably need to scroll up and down to see the entire output. Copy the program's output into a Word document. Next, you'll learn how to copy your program's output into a Word document. You'll need to do this for every homework project. Unfortunately, the technique for copying a command-prompt window's contents is different for different operating systems. I will not attempt to explain every copying technique. Here is how to copy on Windows 2000 and Windows XP computers: In the command-prompt window, click on the icon in the top left corner. From the dropdown menu, select Edit. Select Mark. Use your mouse to select all the output (the window scrolls down as you drag the mouse below the bottom edge). As an alternative to using the mouse for text selection, you can use Edit / Select All. Next, click the top left corner drop-down menu once again. Select Edit. Select Copy. Your output is now copied to your computer's buffer. You'll next need to paste it into a Word document. Open a new Word document. To paste the previously copied text into the Word document, make sure your cursor is within the Word document and then press Ctrl+v. Save the Word document in your f:\cs151 folder. Name the file tutorial.doc . To close the command-prompt window, click within the window and then press any key.

10.
Actions If you have long output and a small command-prompt window Supplemental Information If you're unlucky, your command-prompt window may only be able to store a small number of output lines at a time. This is not a problem for programs with short output, but it will be bothersome for programs with long output. To combat this potential problem, run your CountDown program once again. In the command-prompt window, look for a properties option on the top left corner pull-down menu and then try finding a screen buffer height field. Try bumping up the screen buffer height so that a sufficient number of output lines can be buffered (300?). Save your new screen height setting and close down the command-prompt window. The next time you run a program, you'll be able to store your newly specified number of lines in the command-prompt window. By now, you might be pretty tired of using the command-prompt window. TextPad provides a way to bypass the command-prompt window and direct the output to a TextPad output pane, but you should refrain from doing that. As an informational aside, if you check the Capture Output option for the Configure / Preferences / Tools / Run Java Application command, it causes your programs' output to be directed to a TextPad output pane. That works OK, but only for programs that don't use standard input. For programs that use standard input, the program crashes (terminates with an error message). Since almost all of your programs will use standard input, do not use the Capture Output option. Copy the program's source code into a Word document. Make sure your cursor is in the TextPad window. Press Ctrl+a to highlight your entire program. Press Ctrl+c to copy the highlighted text. Go to your tutorial.doc Word document. Insert your cursor just prior to your output. Press Ctrl+v to paste your source code just prior to your output. Press the Enter key several times in order to separate your source code from your output. For every homework project, you're required to use monospace font for your project source code and project output. Font refers to the appearance of characters. Monospace fonts are fonts such that each character is the same width. Using monospace font ensures that text that's supposed to be aligned stays aligned when it's printed. In your homework projects, if your source code or output is not aligned properly, you will lose style points. To apply monospace font to your Word document's source code, drag your mouse across the source code so that it becomes highlighted. Right click on the highlighted text, and then do one of the following. 1. For Word 2003: Select Font from the pop-up menu. In the Font window, search for Courier New (a popular monospace font), select it, and click OK to apply your font selection. 2. For Word 2007: Select the font down arrow in the bottom pop-up menu. In the Font pop-up submenu, search for Courier New (a popular monospace font) and select it. Apply monospace font to your output. Save your Word document and Exit from Word.

Aside - do not bypass the command-prompt window.

Apply monospace font to your source code.

11.
Actions Exit from TextPad. Supplemental Information Click the File menu. Select Exit.

You might also like