Your First ProgramThere comes a time in every programming course where you've got to stop talking in general about things and cut straight to the code. But before we do that, we've got to choose a particular programming language to use. There are hundreds of programming languages to choose from (maybe more) - the more popular ones that you might have heard of are C, C++, Smalltalk, Squeak, Pascal, Delphi, Basic, Visual Basic, Eiffel, Ada, Cobol, Perl, Python, Fortran, Logo, Lisp, Scheme, Prolog and Haskell. The list goes on into ever more surreally chosen words and acronyms, each with different strengths and weaknesses and its own crowd of fanatical supporters. The one we're going to use to illustrate the concepts in this course is called Java. It's caused quite a stir (no coffee-related pun intended) in the programming community recently because it has several very nice features that make it ideal for writing programs for the internet. However, we won't bore you with those, suffice it to say that it's:
Hello WorldIn a tradition that goes back many tens of years, your first program will be called "Hello World". Its requirements are simple - it must be simple. Its specification is simple - when run, it will display the words "Hello World!". Its design is simple - BEGIN, PRINT "Hello World!", END. And here's the code:
Well it's almost simple... You can think of the first line as telling the computer what you're going to call your program. In actual fact it does a little bit more than that - but again, we'll come back to that. The second line is just a squiggly bracket, or brace to give it it's technical name. Braces always come in pairs: an open and a close brace; a brace of braces. They mark the beginning and end of a block of code of some sort. In this case the closing brace of this pair is the one on the last line of the code and they mark the beginning and end of the program. The third line is rather more complicated to look at, but actually, it's simply a marker to tell the computer where to start from - the main entry point. They could have used something like 'Hey Computer, Start HERE!' instead, but that's programmers for you. The fourth line is another brace that marks where to begin obeying the programs instructions. The computer will start at that brace and do everything you tell it to until it reaches the corresponding close brace (on the second to last line) and then it will stop. This set of instructions is referred to as the main method (or sometimes as main function or main procedure) because it's where everything happens. The fifth line contains the meat of our program - it's a line that tells the computer 'System' to display a line of text 'out.println'. The bit in the round brackets (or parentheses) and the double quotation marks tells it what to display: 'Hello World!'. And then finally the semi-colon (;) tells the computer that that's the end of that instruction and that it should look for another. Java uses semi-colons like we use full-stops. The next line though is the closing brace of the main method, and so the program will end. The final line just ends the program definition. You're now going to have a go at compiling and running it - bet hey, at least testing whether it works should be simple. Compiling and RunningAt this point I'm afraid I'm going to have to apologise in advance about not necessarily being able to give you accurate information about how to do this, as it will depend greatly on what sort of computer system you are using and who set it up. However, I'll give it a bash and if you get stuck ask somebody who looks like they know what they're doing to help you - but watch everything they do and take careful notes. Then next time, and every time afterwards, you'll be able to look like you know what you're doing too! There are four stages to this process:
I'm going to give three different explanations, one for using the Java Development Kit on a Unix workstation in the Computer Science Department, one for using the Java Development Kit on a PC running Windows 95 or NT, and one very vague one about how to use a Java Development Environment like Java Workshop, Visual Cafe or Visual J++. Setting Up Your Java EnvironmentThe first thing you will need to do is set up your Java environment - this step basically involves telling your computer where it can find the Java compiler and other bits and bobs. You usually only have to do this once per programming session.
Saving the Program in a Source FileThe second thing you need to do is to create a text file with your program in it. This is where you need to use a text processor - this is a bit like a word processor in that you can type and edit words in it, but it won't let you do things like use different fonts or centre stuff. For Java, you need to note that the source file needs to have the same name as your program, except with ".java" tagged on the end. So for our HelloWorld program, we need to save the file as "HelloWorld.java".
Compiling the Source FileThis is the point at which you submit your program to the compiler to translate it into computer-ese for you. When the compiler has finished running it should create you a file with the same name as your program, except with ".class" on the end. If it finds any errors, the compiler will print them on the screen and expect you to change the code and run it again. If you're feeling brave, try deleting things like the semi-colon on the third line or one of the braces to see what it does. Don't forget to put them back afterwards though.
Running the Compiled ProgramYou should now have a compiled program sitting in a ".class" file somewhere - you can try loading it into a text editor to see what it looks like if you're curious. Now what we need to do is tell the computer to run this code. With most programming languages, this would be simply a case of typing in the name of the program and the computer would load up the program, point its silicon chip at the main entry point and everything would just run. Java, however, is a platform independent language - which means it is able to run on any sort of computer with any sort of chip in it - a kind of Esperanto for computers. To pull off this trick, Java programs actually run on a virtual machine - a pretend silicon chip which is the same everywhere. You can think of it as a program that hypnotises the real silicon chip so that it can speak a different, standard, language. So when you run a Java program, you have to point the real silicon chip at the Java virtual machine, and the Java virtual machine at your class file.
basic structure of a program [- data in -> do stuff with data -> data out] calculator |