Writing Your First JAVA Program

Writing Your First Program

As you learned during Hour 1, "Becoming a Programmer," a computer program is a set of instructions that tell a computer what to do. These instructions are prepared in the same way instructions could be given to a person: You type them into a word processor. However, that's where the similarity ends. Instructions given to a computer must be written using a programming language. Dozens of computer programming languages have been created; you might have heard of some of them, such as BASIC or Pascal.

During this hour, you will create your first Java program by entering it using any word processor you like. When that's done, you will save the program, compile it, and test it out. The following topics will be covered during this hour:

·         Entering a program into a word processor

·         Naming a Java program with the class statement

·         Organizing a program with bracket marks

·         Storing information in a variable

·         Changing the value of a variable

·         Displaying the information stored in a variable

·         Saving a program

·         Compiling a program

·         Running a program

·         Fixing errors

·         Modifying a program

What You Need to Write Programs

As explained in Hour 1, you should have installed the current version of the Java Developer's Kit on your system. The kit contains tools that enable you to compile and test Java programs. You also need a word processor to write programs.

With most programming languages, computer programs are written by entering text into a word processor (also called a text editor). Some programming languages, such as Visual C++ from Microsoft, come with their own word processor. SunSoft Java WorkShop, an advanced programming tool from Java's developers, also comes with its own editor.

Java programs are simple text files without any special features such as centered text, boldface text, or other enhancements. They can be written with any word processing program that can create text files. Microsoft Windows systems have several word processors you can use, including Notepad, WordPad, and the DOS program edit. Apple Macintosh users can create programs with Simple Text or other editors such as BBEdit Lite. Any of these programs will work fine.

Some word-processing programs are available on the CD-ROM that accompanies this book, including BBEdit Lite.

You also can use more sophisticated word processors, such as Microsoft Word, if you remember to save the programs as text. This option has different names depending on the program you are using. In Word, the file should be saved as a file of type Text Only. Other programs call these files DOS text, ASCII text, or something similar.

 

Caution: If you're in doubt as to whether a word processor can save files as text files, you can always use one of the simple programs that comes with your operating system. Windows users can use Notepad to create Java programs because text files created with Notepad are always saved as text-only files.

 

After you have decided which word processor you will use to write Java programs, go ahead and load it so you can start writing a program.

Windows 95 users must take one extra step before saving any Java-related files to disk: The .java file extension must be associated with the selected word processor. To do this, you need to create a new .java file and load it into the word processor you'll be using during this book.

To create the file, open up any file folder on your system and press the right-click button on your mouse. A pop-up menu will appear that will enable you to choose the New | Text Document option. Choose this option and a file named New Text Document.txt will appear. Change the name of this file to Anything.java and confirm that you really want to choose the new name.

If you have never written Java programs on your computer, the .java file extension should not be associated with any programs yet. Double-click the Anything.java file with the left mouse button and a dialog box will appear, enabling you to choose a program for opening the .java file. Choose your preferred word processor, first making sure to click the option Always use this program to open this file.

Post a Comment

Previous Post Next Post