1Lecture 1 Elementary Applications By tradition, the first program in many programming language is named HelloWorld. It is designed to show the user the elements of syntax, compiling, and displaying a very simple program. In addition there are various issues about word processors and directories that need to be considered. The program we shall write will output the string Hello World!!!!! The directory structure on the computer that is being used to prepare these notes is on my Z drive with subdirectory 11Pic20 which, in turn will have subdirectories Lecture_1, Lecture_2, etc. The structure is shown below: You need to create a directory structure on your Z or C drive; the particular one does not matter. For the sake of simplicity, mimic the one being used here. Create a directory 11Pic20 on your Z drive and then create a subdirectory of 11Pic20 called Lecture_1. Our program is: public class HelloWorld { public static void main(String args[]) { System.out.println(" Hello World!!!!!"); } } Using a simple editor such as Notepad type this into a file called HelloWorld.java. Do not use Word or any other fancy word processor. Such processors have various commands imbedded in 2their files which confuse the compiler. When you save it to a directory use the "All files" option of the editor The complete address of the program is Z:\11Pic20\Lecture_1\HelloWorld.java The most important point of syntax at this time is that the name of the class HelloWorld, in the first line, be identical to the name used in the .java file HelloWorld.java. There is only one instruction in this program System.out.println(" Hello World!!!!!"); Note that it ends with a semi-colon. All instructions in java end with a semi-colon. As for the remainder, just type in what is here. Explanations for the code will come later. To compile the program, assuming that the HelloWorld.java file is in the directory mentioned above, open the DOS command window change, your directory to Z:\11Pic20\Lecture_1\HelloWorld, and then execute the command javac HelloWorld.java. See below: After you have typed in "javac HelloWorld.java" and pressed the Enter key you will get something similar to the above screen. The "Symantic Java.." message may or may not appear; it depends on your enviroment. If you have not made any errors the appearance of the last line,Z:\11Pic20\Lecture_1>_, means that the program has compiled. If you have made an error, the compiler will print out a message, giving you a clue as to where the error might be. To run the program execute the command java HelloWorld. The string " Hello World!!!!!", will appear: 3If you look at your final directory at this point (see below) you will see two files of importance with respect to java. One is the text file HelloWorld.java. The other, HelloWorld.class, was produced at compile time when you executed the java HelloWorld command. The other files listed are connected to the Lecture_1.doc file. Some Generalities Several generalities are in order at this point. Java programs are classes; a program has to be defined within a class. The program itself is typed in as a NAME.java text file, using a simple word processor, such as Notepad. The program is compiled using the command javac NAME.java. It is run with the command java NAME. The compiler is case sensitive. 4Obtaining the Compiler If you wish to obtain the free java compiler for use on your own computer go to the location indicated below, scroll down to the "Download Java 2 SDK .." part, and then press the "continue" button. Once you download and "unpack" it, the jdk system will be placed, by default, in C:\jdk1.3. The compiler,javac, and similar programs will be in C:\jdk1.3\bin. You will need to add to PATH variable the autoexec.bat file to use the programs javac and java in any directory on your computer. I find that working with autoexec.bat files is a gift given to us by Bill Gates. Therefore, I do not guarantee that the following will work for you. It did work for me. I did this on my computer as follows: First, the original autoexec.bat file was saved as backupautoexec.bat; make sure you make a backup file. Next, the autoexec.bat file, which can be opened with Notepad, contained the line PATH=C:\DOS Notice that there is no semi-colon at the end of the line. Change this line to PATH=C:\DOS;C:\jdk1.3\bin 5Note again that there is no semi-colon at the end of the line. Save and close the file. Then go to the DOS command line and run the command autoexec.bat from the C drive. If the prompt line C:\>_ appears as the last line on your screen, all is well. If it doesn't you will be glad you made the backup file earlier. Exercises The exercises below are designed to give you practice in reading and interpreting the error messages that arise at compile time. You are to type in each class as given (as a Notepad NAME.java file), compile (using javac NAME.java), go back and correct your text file, and then run the program (using java NAME). 1) public class First { public static void main(String, args[]) { System.out.println("Now is the time.."); } } 2) class Second { public void main(String args[]) { System.out.println("What is wrong now?"); } 3) public class Third { public void static main(String args[]) { System.out.println(); System.out.println(" Adair Plumbing"); System.out.println() System.out.println("Don't sleep with a drip tonight"); System.out.println("Call Adair); } } 6Lab 1 Write a program called Design that will produce the following output: The program should produce the above array of X's. There is one blank line between the command line and the first line of Xs. There are 10 blank spaces before the first X in the first line with Xs. The text file for your program must be called Design.java. Keep in mind that the java compiler is case senstive. DETAILS ON FILE NAMES DIRECTORIES When your account in the student lab is created it is created with a directory called SUBMIT. I suggest you create a second directory whose name can be anything you please, say Mystuff. Do all your initial work in Mystuff. Then when your program is working correctly, copy and paste the files of the lab assignment (Design.java and Design.class for this assignment) to your SUBMIT directory. The name for the .java text file will be specified for each lab assignment. The grader will run another program that will run your program. If your SUBMIT file does not contain the files with the exact specifed name you will not be credited for that assignment.