How to be a programmer

Dr Andy Evans

[Fullscreen]

How to code

  • How to code in Java
  • General coding practice

What do I need to write Java?

  • You need a text editor and the 'Java Development Kit' (JDK). Text editors come with most OSs.
  • The JDK contains a compiler, and an interpreter, plus some files to add extras to the core language and some additional applications to help with coding.
  • It's free; you'll see where to download it in the practical.

The Interpreter

  • When you compile a Java program, it gets converted into an intermediate language called 'Java bytecode' that any java interpreter on any OS can understand. This is done once.
  • The interpreter does the final job of running the code. As it happens, this is sometimes just converting bits of it into platform dependant code that the interpreter sends to the OS. The interpretation is done each time the program is run.
  • The interpreter is part of the Java Virtual Machine: an piece of software any machine that wants to run Java needs to have.

The Java Virtual Machine (JVM)

  • The Java Virtual Machine runs on top of the normal Operating System (OS) and pretends to be a whole new computer.
  • The Java language then runs inside the JVM, oblivious to the actual OS.
  • Java is two things: a high-level programming language and a platform - the environment that actually does the work.

What we do

  • So, to run Java as a developer, we need to:
    • Write the commands in a text file.
    • Pass the text file to the compiler to get compiled.
    • Pass the compiler results (bytecode files) to the JVM for running.
  • The good thing is that the compiler will check our code matches the Java syntax, and the JVM will report problems that arise as the code runs.
  • Because the JVM is protecting the OS, you are very unlikely to crash or destroy an OS with Java, unlike languages like C++.

Debugging

  • Both the compiler and interpreter can catch problems in your code.
  • Both will give you a description of what has gone wrong and where.
  • Your job is then to fix the issue.
  • This isn't because you can't program; this is programming.
  • Programming is 50% writing code and 50% fixing it.

Before you code:
Algorithms

  • Programming is the art of describing how to do very complicated things to a very stupid computer in very simple steps.
  • The initial outline for the processing done is called an 'algorithm', it's a bit like a recipe.

How to calculate the mean of three numbers

  1. Get 3 numbers.
  2. Sum the numbers.
  3. Divide the sum by 3.
  4. Print the result.

How to code

  • First, write the algorithm into your text file as comments (these are lines starting // that the computer doesn't process).
  • Next, pick a line to work up as code.
  • Write the code a line, or couple of lines, at a time. Compile and test the code every couple of lines. Baby steps mean you won't end up with 100 errors at once and you'll know where the errors are.
  • Think 'how can I test this is working properly?' as each line goes in.

How not to code

  • Don't just start writing without thinking through how the program is roughly structured. Compile regularly.
  • Don't ignore errors - they won't go away, and they'll just make everything wrong. If you get an error you can't see, try cutting back chunks of code until you have something simple that works, then add it back in, a line at a time.
  • That said, if you get very stuck and no help is immediately forthcoming, think about cutting out the problematic code and replacing it temporarily with something simpler. For example, if you can't work out how to open a file and read in data, just write the data directly into the program and work on something clearer until you can find someone to help.

Collaboration and the Net

  • No one codes from scratch; it would be counterproductive to ignore the mass of experience available to draw on.
  • The first thing to do (if you're not being assessed for the work!) is to see if someone else has already done it and made their work available through an Open Source License.
  • Open Source Licenses make code freely available (in the sense of putting it out there for others to use) and freely available (in the sense of not costing anything). Different licenses have different requirements and protection. You must make sure you match the licensing requirements.
  • But also, there are plenty of good forums where people will post useful examples, or answer questions.
  • It's not called a coding community for nothing.

Next

One last quiz, then we get coding!