Monkeys at Keyboards: APE Antics
© Michael James Heron and Pauline Belford
Topic: Java Programming
Level: 1
Version: alpha

It's really weird how your life changes. Tonight I'm drinking water. Four years ago? Opium. Night and day, you know?
Bill Hicks

1 - Getting Started!

PreviousTable of ContentsNext
Forum


Chapter Objectives

By the end of this chapter, the reader will be able to:



    1.1

    Introduction

    Welcome to the Abstract Programming Environment (APE), a tool designed to make learning how to write computer programs as simple as possible by abstracting much of the real world complexity away so we can concentrate purely on the syntax of the language itself. In the first few chapters of this book, we will look at what APE is and how to use it. We'll also look at some simple example programs and exercises that will outline the important aspects of the Java programming language. Later on in the book, once we are secure in how these various parts of the language work, we can move away from APE and instead start looking at how to write 'proper' Java programs.

    It should be stressed at this point that this tool is not a tool about Java. Not really - we'll be using Java as a delivery language, but the techniques and concepts that are covered are applicable to a wide variety of programming languages. We'll talk about that a little more in coming chapters - before we can have a meaningful discussion about that, we need to look at some terminology that we will be making use of in the coming chapters.

    The assumption through this book is that you are new to computer programming, but that you have some familiarity with computers in general. As such, we will spend no time on common computing concepts so that we can concentrate fully on the task of learning how to be programmers. If you have no previous experience with computers, then students would be well advised to seek out a different course to provide them with the necessary foundation of knowledge.

    1.2

    Terminology

    Programming is difficult - there's no getting away from that. One of the things that make it difficult are the many pieces of jargon that are used when discussing the topic. In this book, as far as is possible, we will highlight new pieces of jargon and detail what they mean in cut-away boxes and in the text of the book itself:


    Terminology Alert!

    Jargon is a very particular way of referring to concepts within a particular sphere of interest. Jargon is a kind of linguistical shorthand that allows people to communicate complex ideas succinctly to other experts in the field of study.


    The terminology used may seem to be an additional level of difficulty to learning something that is already difficult, but the words are important - they are what you use (or will use) to meaningfully express yourself to other programmers. It's like when playing chess... 'Move your horse' may mean the same thing as 'move your knight', but more complex ideas require a more formal terminology (such as recording the moves made on a chessboard). Programming is no different, and we will see a lot of terminology in the course of this book.

    Don't just ignore the terminology - make an effort to understand it, because it is fundamental to understanding the concepts discussed in this and other chapters of the APE teaching materials.

    And so, with that little aside, let's look at a new terminology term - syntax.


    Terminology Alert!

    Syntax in programming refers to the words and symbols that make up a computer program, and the order in which they are put together. Computers are very stupid and don't understand things quite as easily as humans, and so they need things to be put together in a very special, and strict way. Computers can't resolve ambiguity, and so there are formal rules as to how a computer program fits together. We refer to these words and these rules as a language's syntax.


    In the coming chapters, we will be looking at Java's syntax and how to write programs that do certain things. The syntax Java uses is a very well known syntax that is based on that of an old programming language called C. C++ and C# are other programming languages that use the same syntax, and so it is sometimes referred to as c-type syntax.

    Java is also an object-oriented language, which puts it in a particular category of programming languages. At the moment, we are not going to worry about what object orientation means (although we will spend a lot of time in later chapters of this and other books discussing it), but it will, even at this stage, have an impact on the way we write programs.


    Terminology Alert!

    Object-Orientation is the process of writing a computer program as a series of interconnected objects. This is a very complicated idea and one that we're not going to spend a lot of time on at the moment, so just put it out of your head.


    We will introduce a lot more terminology as we go through this material - it's important that you commit these terms to memory as they will be used without mercy as we go through the book!

    1.3

    How Do We Write A Computer Program?

    So, that's a little bit about what Java is - but how do we write a computer program using Java? Well, we use a computer application that has already been written for us. In this case we are going to use what is known as an IDE.


    Terminology Alert!

    An IDE is shorthand for an integrated development environment. In the Olden Days, people wrote Java programs from the command line in DOS using a couple of special command line programs (they were javac and java)... but before that could be done the program had to be written in a text editor program and then compiled using javac. Nowadays we use a single program that provides a text editor and a compiler in one neat package.



    Terminology Alert!

    A compiler is a piece of software that turns a java program (or indeed, almost any program) into something a computer can understand. Java is a high level programming language, and a computer does not understand the words and symbols used. A compiler takes the java program, and turns each of the words in that program into something the computer can understand.



    Terminology Alert!

    Java is a high-level language, which means that it is written using (mostly) human readable syntax. Computers do not understand high-level language syntax - they only understand the ones and zeros that the CPU itself uses (this is known as machine code). High-level languages are turned into low-level languages through the process of compilation.


    The IDE we are going to use for this material is called JCreator, and a freeware version of this is available from the link indicated in the resources section of the book. There are many other IDEs that are of varying quality, but JCreator provides a nice balance of ease of use with power over the language and therefore is the baseline IDE for our explorations into Java.

    In the resources section of the web-site we have a series of tutorials about JCreator, but during the course of these chapters we will also cover what we need to know in this book also. We do not need to ask much of JCreator at this point, so we can build incrementally until we are developing programs of Astounding Power and Brilliance!

    Before we start using APE (which needs us to setup a special part of the IDE), we will make sure we have Java installed and functioning on our computers with a simple (indeed, the simplest) starting program.

    Installing Java is a multi-step process that involves installing the development kit, then the documentation, and then finally the JCreator IDE. All of these are available for download in the resources section of the website, and detailed instructions for installing Java are also available here. If you don't currently have Java installed on your computer, go to the installation page and follow the instructions there. Don't worry, I'll wait for you.

    All done? Splendid!

    Now we're ready to start off our very first Java program. Are you ready? Then let's begin!

    The Jcreator Interface
    Fig 1.1: The Jcreator Interface

    The picture above is the default JCreator interface. It is empty, bereft of code, and needs us to breathe life into it with our first Java program. Exciting stuff, eh?

    Java files are contained in JCreator as part of project, so we go to the file menu and choose new:

    The new dialog box
    Fig 1.2: The new dialog box

    Choose the projects tab, and click on empty project:

    An empty project
    Fig 1.3: An empty project

    Your project needs to be given a name, and a location (where it will be stored on your hard-drive). Your location will vary depending on your own computer (or your own course if you are doing this at a college or university). If it's your own computer, store it wherever you like (although you would be best to have a folder somewhere setup for all your java programs - there will be a lot of them!). If you are doing this as part of a formal course, then consult your lecturer to find out where things should be stored.

    The name of your project is unimportant at this point, so we're going to call it HelloWorld. Once you've done this, click the OK button.

    Now, back to file > new, and add a new Java File:

    Adding a new Java file
    Fig 1.4: Adding a new Java file

    The Filename you give this file is going to be very important, so type it in exactly as it's shown below, including Capital Letters:

    HelloWorld.java

    Your filename is important in Java - I'll explain why in a few moments. Your filename must end with .java, and it cannot contain spaces. Once you've done this, you'll get a blank file into which you can enter all your Java goodness.

    Now comes the Java code. Just type this out to begin with, and then we'll go over it line by line explaining what each line is doing:


    public class HelloWorld {

    public static void main (String args[]) {
    System.out.println ("Argh, it burns! Arrr!");
    }

    }

    Java is Case Sensitive, which means that you have to make sure your capitalization is correct. If you make any errors in copying out the program, it will cause a syntax error when you try to run the program. If this happens, double-check to make sure your program is written exactly as is outlined above. As we go through the book, we'll talk about how to recognize these problems properly in more complicated programs.

    To run a program in JCreator, first you compile it, and then you execute it using the buttons below:

    The compile and execute buttons
    Fig 1.5: The compile and execute buttons

    First press the compile button, and if all has gone well it will show Process completed at the bottom of the IDE. If you have made a mistake in copying out the program (or if you have made a mistake in typing out the filename), you will see errors appear instead of Process completed.

    This is why the filename is important in Java - the name of your class must match the name of your Java file (minus the .java bit at the end). If your file is HelloWorld.java, then your program must begin with public class HelloWorld {

    If you have changed the casing then you must change the first line of your program accordingly. Let's say for example you called your file helloworld.java. Your program would then be as follows:


    public class helloworld {

    public static void main (String args[]) {
    System.out.println ("Argh, it burns! Arrr!");
    }

    }

    A successful compilation
    Fig 1.6: A successful compilation

    Assuming all goes well, what you should see is the following (this is the console window):

    Program output
    Fig 1.7: Program output

    And that's your very first Java program! It may not look like much, but it's the first step you'll take into a larger world.

    Now that we've seen what our program does, let's look at how it does it, line by line.

    The first line of the program gives the name of the class for the program. This is an artifact of the object oriented programming system - all programs must be written in a class. For now, we won't worry too much about it - we'll just think of it as the name you've given your program. All programs need to have a name which identifies them... the filename contains the code for the program, and the first line of code gives the name of that program.

    At the end of line one of the code is a funny squiggly bracket (it's known as a brace). It indicates ownership of code - specficially in this case, the code that follows it belongs to the HelloWorld class. The programs you write will be full of these braces, so it's a good idea to get used to them early.

    Line two of the program looks very complicated - that's because it is very complicated. We will not talk about much of this in this chapter. The first two words in the line (public and static) are known as qualifiers, but we don't need to know that for now. The third word (void) represents a return type. We won't worry about that either for now.

    Classes in Java are made up of methods, which are little chunks of program code that can be executed whenever the developer (that's you) desires. This line of code is setting up a method for Java.


    Terminology Alert!

    A method is the basic building block of a java program. It's a little collection of code that can be made use of at various points in a program's lifetime. Some of these methods are special methods that are used by Java itself to make things work.


    The word main is important - it's the name of this method - all programs in Java must contain a main method, and this is where Java begins executing code in a program.

    The name of the method always preceeds the opening and closing brackets of the method - these brackets indicate parameters that are going to be made available within the body of the method.


    Terminology Alert!

    Parameters are pieces of information that are provided to a method to allow it to fulfil its function. Some information that a method needs is provided within the method itself, while some is passed into a method from an external source. More on this later.


    This is the most complicated line of code in the program, and you don't need to worry about what any of it means for now - you won't be changing any of it for quite some time, you'll just be copying it word for word.

    Notice at the end of this line we also have a brace - that too indicates ownership - specifically that the code that follows belongs to the main method (which itself belongs to the HelloWorld class).

    The third line of code is what actually does the program itself - it tells Java to print out a line of text to the console (the display window that you see when you run this program). Again, this is something you don't need to worry about too much for now. System.out.println is a method call - remember how we said above that methods can be used at various points in a program's lifetime? This is an example of this. The developers of Java wrote this method for you (they wrote a lot of methods for you), and by using it you can cause a line of code to appear on the console. The bit between the brackets is the parameter to this method - in this case, a piece of text that you want to be displayed.

    Notice how it's contained in quotation marks - this is important as it marks the text between the quotations marks as a single piece of text. Remember how we said that computers are stupid and they can't deal with ambiguity? They need formal rules to be able to tell when it should treat something as a piece of text and when it should treat something as a piece of a computer program. The quotation marks tell Java that anything between them should be treated as a single piece of text and not as part of the computer code.

    Also notice that this line of code ends with a semi-colon. This is also important - it's used to tell Java when you've finished telling it to do something. Every time you give it an actual instruction (known as a statement in Java terminology) you end the line with a semi-colon. That's not something you need to worry about too much at the moment... for the time being you'll mostly be copying the programs as they are presented and adding in your own statements telling Pacman to do things. For now, just put a semi-colon after every line of code you write yourself. We'll point out in later chapters when you don't need to do that.

    The casing in Java code is important, but it's not important in the text that is passed to the System.out.println method. We can change it without a problem:


    public class HelloWorld {

    public static void main (String args[]) {
    System.out.println ("argh, it burns! arrr!");
    }

    }

    Indeed, we can change the text to whatever we'd like to be displayed:


    public class HelloWorld {

    public static void main (String args[]) {
    System.out.println ("Sum druids is bare!!");
    }

    }

    The rest of the program is a finely balanced organism - change anything else and it will fall apart. It is only in the parameter here that you have any kind of freedom, and that freedom extends only to what is between the quotation marks themselves.

    1.4

    Conclusion

    This chapter is presented as a nice, easy introduction to the topic before the Back Breaking Labour of actual programming comes. Nonetheless, by the end of this chapter you will have accomplished quite a few things:

    1. (Optional) Installing Java and JCreator
    2. Creating a new project in JCreator
    3. Creating a new Java file in JCreator
    4. Creating your first Java program.

    From little acorns like these, great oaks will grow! Stay tuned for the next exciting installments of your book!

    Further Reading

    The following table details further reading on the topic in this chapter, and also any external resources that you may find useful.

    ResourceDescription
    Teach Yourself Programming in Ten YearsWhile I don't agree with everything that's said in the article, it's still worth reading to get a perception of the task in front of you!

    PreviousTable of ContentsNext

    © 2004-2006 Michael James Heron and Pauline Belford